apache_dubbo/
invocation.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use futures_core::Stream;
19use std::{collections::HashMap, str::FromStr};
20
21pub struct Request<T> {
22    pub message: T,
23    pub metadata: Metadata,
24}
25
26impl<T> Request<T> {
27    pub fn new(message: T) -> Request<T> {
28        Self {
29            message,
30            metadata: Metadata::new(),
31        }
32    }
33
34    pub fn into_inner(self) -> T {
35        self.message
36    }
37
38    pub fn into_parts(self) -> (Metadata, T) {
39        (self.metadata, self.message)
40    }
41
42    pub fn from_parts(metadata: Metadata, message: T) -> Self {
43        Request { message, metadata }
44    }
45
46    pub fn from_http(req: http::Request<T>) -> Self {
47        let (parts, body) = req.into_parts();
48        Request {
49            metadata: Metadata::from_headers(parts.headers),
50            message: body,
51        }
52    }
53
54    pub fn into_http(self) -> http::Request<T> {
55        let mut http_req = http::Request::new(self.message);
56        *http_req.version_mut() = http::Version::HTTP_2;
57        *http_req.headers_mut() = self.metadata.into_headers();
58
59        http_req
60    }
61
62    pub fn map<F, U>(self, f: F) -> Request<U>
63    where
64        F: FnOnce(T) -> U,
65    {
66        let m = f(self.message);
67        Request {
68            message: m,
69            metadata: self.metadata,
70        }
71    }
72}
73
74pub struct Response<T> {
75    message: T,
76    metadata: Metadata,
77}
78
79impl<T> Response<T> {
80    pub fn new(message: T) -> Response<T> {
81        Self {
82            message,
83            metadata: Metadata::new(),
84        }
85    }
86
87    pub fn from_parts(metadata: Metadata, message: T) -> Self {
88        Self { message, metadata }
89    }
90
91    pub fn into_parts(self) -> (Metadata, T) {
92        (self.metadata, self.message)
93    }
94
95    pub fn into_http(self) -> http::Response<T> {
96        let mut http_resp = http::Response::new(self.message);
97        *http_resp.version_mut() = http::Version::HTTP_2;
98        *http_resp.headers_mut() = self.metadata.into_headers();
99
100        http_resp
101    }
102
103    pub fn from_http(resp: http::Response<T>) -> Self {
104        let (part, body) = resp.into_parts();
105        Response {
106            message: body,
107            metadata: Metadata::from_headers(part.headers),
108        }
109    }
110
111    pub fn map<F, U>(self, f: F) -> Response<U>
112    where
113        F: FnOnce(T) -> U,
114    {
115        let u = f(self.message);
116        Response {
117            message: u,
118            metadata: self.metadata,
119        }
120    }
121}
122
123pub trait IntoStreamingRequest {
124    type Stream: Stream<Item = Self::Message> + Send + 'static;
125    type Message;
126
127    fn into_streaming_request(self) -> Request<Self::Stream>;
128}
129
130impl<T> IntoStreamingRequest for T
131where
132    T: Stream + Send + 'static,
133    // T::Item: Result<Self::Message, std::convert::Infallible>,
134{
135    type Stream = T;
136
137    type Message = T::Item;
138
139    fn into_streaming_request(self) -> Request<Self::Stream> {
140        Request::new(self)
141    }
142}
143
144// impl<T> sealed::Sealed for T {}
145
146// pub mod sealed {
147//     pub trait Sealed {}
148// }
149
150#[derive(Debug, Clone, Default)]
151pub struct Metadata {
152    inner: HashMap<String, String>,
153}
154
155impl Metadata {
156    pub fn new() -> Self {
157        Metadata {
158            inner: HashMap::new(),
159        }
160    }
161
162    pub fn from_headers(headers: http::HeaderMap) -> Self {
163        let mut h: HashMap<String, String> = HashMap::new();
164        for (k, v) in headers.into_iter() {
165            if let Some(name) = k {
166                h.insert(name.to_string(), v.to_str().unwrap().to_string());
167            }
168        }
169
170        Metadata { inner: h }
171    }
172
173    pub fn into_headers(&self) -> http::HeaderMap {
174        let mut header = http::HeaderMap::new();
175        for (k, v) in self.inner.clone().into_iter() {
176            header.insert(
177                http::header::HeaderName::from_str(k.as_str()).unwrap(),
178                http::HeaderValue::from_str(v.as_str()).unwrap(),
179            );
180        }
181
182        header
183    }
184}