Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions waki/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct IncomingBodyStream {
}

impl From<IncomingBody> for IncomingBodyStream {
#[inline]
fn from(body: IncomingBody) -> Self {
Self {
// The stream() method can only be called once
Expand All @@ -37,6 +38,7 @@ pub enum Body {
}

impl Body {
#[inline]
pub fn chunk(&self, len: u64) -> Result<Option<Vec<u8>>> {
match &self {
Body::Bytes(_) => Ok(None),
Expand Down
8 changes: 8 additions & 0 deletions waki/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@ use crate::{Method, RequestBuilder};
pub struct Client {}

impl Client {
#[inline]
pub fn new() -> Self {
Default::default()
}

#[inline]
pub fn get(&self, url: &str) -> RequestBuilder {
self.request(Method::Get, url)
}

#[inline]
pub fn post(&self, url: &str) -> RequestBuilder {
self.request(Method::Post, url)
}

#[inline]
pub fn put(&self, url: &str) -> RequestBuilder {
self.request(Method::Put, url)
}

#[inline]
pub fn patch(&self, url: &str) -> RequestBuilder {
self.request(Method::Patch, url)
}

#[inline]
pub fn delete(&self, url: &str) -> RequestBuilder {
self.request(Method::Delete, url)
}

#[inline]
pub fn head(&self, url: &str) -> RequestBuilder {
self.request(Method::Head, url)
}

#[inline]
pub fn request(&self, method: Method, url: &str) -> RequestBuilder {
RequestBuilder::new(method, url)
}
Expand Down
5 changes: 5 additions & 0 deletions waki/src/common/request_and_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ macro_rules! impl_common_get_methods {
($($t:ty),+ $(,)?) => ($(
impl $t {
/// Get the header.
#[inline]
pub fn header<K: AsHeaderName>(&self, key: K) -> Option<&HeaderValue> {
self.headers.get(key)
}

/// Get headers.
#[inline]
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
Expand All @@ -29,13 +31,15 @@ macro_rules! impl_common_get_methods {
///
/// NOTE: This method is only for incoming requests/responses, if you call it on an
/// outgoing request/response it will always return None.
#[inline]
pub fn chunk(&self, len: u64) -> Result<Option<Vec<u8>>> {
self.body.chunk(len)
}

/// Get the full body.
///
/// It will block until the stream is closed.
#[inline]
pub fn body(self) -> Result<Vec<u8>> {
self.body.bytes()
}
Expand Down Expand Up @@ -181,6 +185,7 @@ macro_rules! impl_common_set_methods {
/// r.body("hello");
/// # }
/// ```
#[inline]
pub fn body<V: Into<Vec<u8>>>(mut self, body: V) -> Self {
if let Ok(ref mut inner) = self.inner {
inner.body = Body::Bytes(body.into());
Expand Down
2 changes: 2 additions & 0 deletions waki/src/common/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::bindings::wasi::http::types::Scheme;

impl From<&str> for Scheme {
#[inline]
fn from(s: &str) -> Self {
match s {
"http" => Scheme::Http,
Expand All @@ -13,6 +14,7 @@ impl From<&str> for Scheme {
impl TryInto<http::uri::Scheme> for Scheme {
type Error = http::uri::InvalidUri;

#[inline]
fn try_into(self) -> Result<http::uri::Scheme, Self::Error> {
match self {
Scheme::Http => Ok(http::uri::Scheme::HTTP),
Expand Down
9 changes: 9 additions & 0 deletions waki/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct RequestBuilder {
}

impl RequestBuilder {
#[inline]
pub fn new(method: Method, uri: &str) -> Self {
Self {
inner: uri.parse::<Uri>().map_or_else(
Expand Down Expand Up @@ -86,6 +87,7 @@ impl RequestBuilder {
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
if let Ok(ref mut req) = self.inner {
req.connect_timeout = Some(timeout.as_nanos() as u64);
Expand All @@ -94,11 +96,13 @@ impl RequestBuilder {
}

/// Build the Request.
#[inline]
pub fn build(self) -> Result<Request> {
self.inner
}

/// Send the Request, returning a [`Response`].
#[inline]
pub fn send(self) -> Result<Response> {
match self.inner {
Ok(req) => req.send(),
Expand Down Expand Up @@ -162,6 +166,7 @@ impl TryFrom<IncomingRequest> for Request {
}

impl Request {
#[inline]
pub fn new(method: Method, uri: Parts) -> Self {
Self {
method,
Expand All @@ -172,16 +177,19 @@ impl Request {
}
}

#[inline]
pub fn builder(method: Method, uri: &str) -> RequestBuilder {
RequestBuilder::new(method, uri)
}

/// Get the HTTP method of the request.
#[inline]
pub fn method(&self) -> Method {
self.method.clone()
}

/// Get the path of the request.
#[inline]
pub fn path(&self) -> &str {
match &self.uri.path_and_query {
Some(path_and_query) => path_and_query.path(),
Expand All @@ -202,6 +210,7 @@ impl Request {
}

/// Get the authority of the request.
#[inline]
pub fn authority(&self) -> &Option<Authority> {
&self.uri.authority
}
Expand Down
8 changes: 8 additions & 0 deletions waki/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ pub struct ResponseBuilder {
}

impl Default for ResponseBuilder {
#[inline]
fn default() -> Self {
Self::new()
}
}

impl ResponseBuilder {
#[inline]
pub fn new() -> Self {
Self {
inner: Ok(Response::new()),
Expand All @@ -30,6 +32,7 @@ impl ResponseBuilder {
/// Set the status code for the response.
///
/// Default value: 200.
#[inline]
pub fn status_code(mut self, status_code: u16) -> Self {
if let Ok(ref mut resp) = self.inner {
resp.status_code = status_code;
Expand All @@ -38,6 +41,7 @@ impl ResponseBuilder {
}

/// Build the Response.
#[inline]
pub fn build(self) -> Result<Response, ErrorCode> {
match self.inner {
Ok(inner) => Ok(inner),
Expand All @@ -53,6 +57,7 @@ pub struct Response {
}

impl Default for Response {
#[inline]
fn default() -> Self {
Self::new()
}
Expand All @@ -77,6 +82,7 @@ impl TryFrom<IncomingResponse> for Response {
}

impl Response {
#[inline]
pub fn new() -> Self {
Self {
headers: HeaderMap::new(),
Expand All @@ -85,10 +91,12 @@ impl Response {
}
}

#[inline]
pub fn builder() -> ResponseBuilder {
ResponseBuilder::new()
}

#[inline]
/// Get the status code of the response.
pub fn status_code(&self) -> u16 {
self.status_code
Expand Down