apache_dubbo/
invocation.rs1use 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 {
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#[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}