1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::{ffi::http_request_v1 as ffi, Error, ErrorCode};
use std::{
borrow::Cow,
future::Future,
pin::Pin,
string::FromUtf8Error,
task::{Context, Poll},
};
#[doc(hidden)]
pub use ffi::API as FFI_API;
pub use ffi::Method;
pub struct Response {
pub status_code: u16,
bytes: Vec<u8>,
}
impl Response {
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn as_text(&self) -> Result<String, FromUtf8Error> {
String::from_utf8(self.bytes.clone())
}
pub fn as_text_lossy(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.bytes)
}
}
struct RequestFuture(Result<ffi::RequestHandle, ErrorCode>);
pub type ResponseResult = Result<Response, Error>;
impl Future for RequestFuture {
type Output = ResponseResult;
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.0 {
Ok(handle) => {
let mut status: u32 = 0;
if let Err(error_code) = ffi::is_ready(handle, &mut status) {
return Poll::Ready(Err(Error::from(error_code)));
}
if status != ffi::STATUS_PENDING {
match ffi::retrieve(handle) {
Ok(bytes) => {
assert!(u16::try_from(status).is_ok());
let response = Response {
bytes,
status_code: status as u16,
};
Poll::Ready(Ok(response))
}
Err(error_code) => Poll::Ready(Err(Error::from(error_code))),
}
} else {
Poll::Pending
}
}
Err(error_code) => Poll::Ready(Err(Error::from(error_code))),
}
}
}
pub fn http_request(
method: Method,
url: &str,
body: &[u8],
) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(method, url, body))
}
pub fn http_get(url: &str) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Get, url, &[]))
}
pub fn http_post(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Post, url, body))
}
pub fn http_put(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Put, url, body))
}
pub fn http_delete(url: &str) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Delete, url, &[]))
}
pub fn http_patch(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Patch, url, body))
}
pub fn http_head(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Head, url, body))
}
pub fn http_options(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Options, url, body))
}
pub fn http_trace(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Trace, url, body))
}
pub fn http_connect(url: &str, body: &[u8]) -> impl Future<Output = ResponseResult> {
RequestFuture(ffi::request(ffi::Method::Connect, url, body))
}