|
1 | 1 | use super::{response::IncomingBody, Body, Error, Request, Response, Result};
|
2 | 2 | use crate::io::{self, AsyncWrite};
|
3 | 3 | use crate::runtime::Reactor;
|
4 |
| -use std::time::Duration; |
5 |
| -use wasi::clocks::monotonic_clock::Duration as WasiDuration; |
| 4 | +use crate::time::Duration; |
6 | 5 | use wasi::http::types::{OutgoingBody, RequestOptions as WasiRequestOptions};
|
7 | 6 |
|
8 | 7 | /// An HTTP client.
|
@@ -46,18 +45,18 @@ impl Client {
|
46 | 45 | }
|
47 | 46 |
|
48 | 47 | /// Set timeout on connecting to HTTP server
|
49 |
| - pub fn set_connect_timeout(&mut self, d: Duration) { |
50 |
| - self.options_mut().connect_timeout = Some(d); |
| 48 | + pub fn set_connect_timeout(&mut self, d: impl Into<Duration>) { |
| 49 | + self.options_mut().connect_timeout = Some(d.into()); |
51 | 50 | }
|
52 | 51 |
|
53 | 52 | /// Set timeout on recieving first byte of the Response body
|
54 |
| - pub fn set_first_byte_timeout(&mut self, d: Duration) { |
55 |
| - self.options_mut().first_byte_timeout = Some(d); |
| 53 | + pub fn set_first_byte_timeout(&mut self, d: impl Into<Duration>) { |
| 54 | + self.options_mut().first_byte_timeout = Some(d.into()); |
56 | 55 | }
|
57 | 56 |
|
58 | 57 | /// Set timeout on recieving subsequent chunks of bytes in the Response body stream
|
59 |
| - pub fn set_between_bytes_timeout(&mut self, d: Duration) { |
60 |
| - self.options_mut().between_bytes_timeout = Some(d); |
| 58 | + pub fn set_between_bytes_timeout(&mut self, d: impl Into<Duration>) { |
| 59 | + self.options_mut().between_bytes_timeout = Some(d.into()); |
61 | 60 | }
|
62 | 61 |
|
63 | 62 | fn options_mut(&mut self) -> &mut RequestOptions {
|
@@ -113,36 +112,23 @@ impl RequestOptions {
|
113 | 112 | fn to_wasi(&self) -> Result<WasiRequestOptions> {
|
114 | 113 | let wasi = WasiRequestOptions::new();
|
115 | 114 | if let Some(timeout) = self.connect_timeout {
|
116 |
| - wasi.set_connect_timeout(Some( |
117 |
| - dur_to_wasi(timeout).map_err(|e| e.context("connect timeout"))?, |
118 |
| - )) |
119 |
| - .map_err(|()| { |
| 115 | + wasi.set_connect_timeout(Some(*timeout)).map_err(|()| { |
120 | 116 | Error::other("wasi-http implementation does not support connect timeout option")
|
121 | 117 | })?;
|
122 | 118 | }
|
123 | 119 | if let Some(timeout) = self.first_byte_timeout {
|
124 |
| - wasi.set_first_byte_timeout(Some( |
125 |
| - dur_to_wasi(timeout).map_err(|e| e.context("first byte timeout"))?, |
126 |
| - )) |
127 |
| - .map_err(|()| { |
| 120 | + wasi.set_first_byte_timeout(Some(*timeout)).map_err(|()| { |
128 | 121 | Error::other("wasi-http implementation does not support first byte timeout option")
|
129 | 122 | })?;
|
130 | 123 | }
|
131 | 124 | if let Some(timeout) = self.between_bytes_timeout {
|
132 |
| - wasi.set_between_bytes_timeout(Some( |
133 |
| - dur_to_wasi(timeout).map_err(|e| e.context("between byte timeout"))?, |
134 |
| - )) |
135 |
| - .map_err(|()| { |
136 |
| - Error::other( |
137 |
| - "wasi-http implementation does not support between byte timeout option", |
138 |
| - ) |
139 |
| - })?; |
| 125 | + wasi.set_between_bytes_timeout(Some(*timeout)) |
| 126 | + .map_err(|()| { |
| 127 | + Error::other( |
| 128 | + "wasi-http implementation does not support between byte timeout option", |
| 129 | + ) |
| 130 | + })?; |
140 | 131 | }
|
141 | 132 | Ok(wasi)
|
142 | 133 | }
|
143 | 134 | }
|
144 |
| -fn dur_to_wasi(d: Duration) -> Result<WasiDuration> { |
145 |
| - d.as_nanos() |
146 |
| - .try_into() |
147 |
| - .map_err(|_| Error::other("duration out of range supported by wasi")) |
148 |
| -} |
0 commit comments