pub struct Fetch {
pub config: Option<FetchConfig>,
/* private fields */
}
Fields§
§config: Option<FetchConfig>
Implementations§
Source§impl Fetch
impl Fetch
Sourcepub fn new(base_url: &str, options: Option<FetchConfig>) -> FetchResult<Self>
pub fn new(base_url: &str, options: Option<FetchConfig>) -> FetchResult<Self>
Creates a new instance of Fetch with a set base url and optional Options
§Example
use rust_fetch::Fetch;
let client = Fetch::new("http://localhost", None);
assert_ne!(true, client.is_err());
Sourcepub fn set_default_headers(
&mut self,
headers: Option<FetchHeaders>,
) -> FetchResult<()>
pub fn set_default_headers( &mut self, headers: Option<FetchHeaders>, ) -> FetchResult<()>
Sets the default headers for this instance of Fetch.
§Example
use rust_fetch::{Fetch, map_string};
let mut client = Fetch::new("http://localhost", None).unwrap();
let set_header_result = client.set_default_headers(Some(map_string!{ header1 : "header 1 value" }));
assert_ne!(true, set_header_result.is_err());
pub fn build_url( &self, endpoint: &str, options: Option<&FetchOptions>, ) -> FetchResult<Url>
Sourcepub async fn post<T, U>(
&self,
endpoint: &str,
data: Option<U>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<T>>where
T: for<'de> Deserialize<'de>,
U: Serialize,
pub async fn post<T, U>(
&self,
endpoint: &str,
data: Option<U>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<T>>where
T: for<'de> Deserialize<'de>,
U: Serialize,
Sends an HTTP Post request to the configured remote server
endpoint
- The remote endpoint. This gets joined with the base_url configured in the ::new() methoddata
- Optional data to send to the remote endpoint (to be serialized as JSON). IfNone
, then no data is sent instead ofnull
options
- TheFetchOptions
for this call. Allows setting of headers and/or query params
§Example
use httpmock::prelude::*;
use rust_fetch::Fetch;
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct ToReturn {}
#[derive(serde::Serialize, serde::Deserialize, Debug)]
struct ToSend {
test_key: String,
}
#[tokio::main]
async fn main() {
let server = MockServer::start();
server.mock(|when, then| {
when.path("/test").method(POST);
then.status(200).json_body(serde_json::json!({}));
});
let fetch = Fetch::new(&server.base_url(), None).unwrap();
let response = fetch
.post::<ToReturn, ToSend>(
"/test",
Some(ToSend {
test_key: "Testing".to_string(),
}),
Some(rust_fetch::FetchOptions {
params: Some(rust_fetch::map_string! {param1 : "value1"}),
..Default::default()
}),
)
.await.unwrap();
assert_eq!(&200, &response.status);
assert_eq!(ToReturn {}, response.body.unwrap());
}
Sourcepub async fn get<T>(
&self,
endpoint: &str,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<T>>where
T: for<'de> Deserialize<'de>,
pub async fn get<T>(
&self,
endpoint: &str,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<T>>where
T: for<'de> Deserialize<'de>,
Sends an HTTP GET request to the configured remote server
endpoint
- The remote endpoint. This gets joined with the base_url configured in the ::new() methodoptions
- TheFetchOptions
for this call. Allows setting of headers and/or query params
§Example
use rust_fetch::Fetch;
use httpmock::prelude::*;
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct ToReturn {
}
#[tokio::main]
async fn main() {
let server = MockServer::start();
server.mock(|when, then|{
when.path("/test");
then.status(200).json_body(serde_json::json!({}));
});
let fetch = Fetch::new(&server.base_url(), None).unwrap();
let response = fetch.get::<ToReturn>("/test", Some(rust_fetch::FetchOptions
{
params: Some(rust_fetch::map_string!{param1 : "value1"}),
..Default::default()
})).await.unwrap();
assert_eq!(&200, &response.status);
assert_eq!(ToReturn{}, response.body.unwrap());
}
Sourcepub async fn delete<T, U>(
&self,
endpoint: &str,
data: Option<T>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<U>>where
T: Serialize,
U: for<'de> Deserialize<'de>,
pub async fn delete<T, U>(
&self,
endpoint: &str,
data: Option<T>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<U>>where
T: Serialize,
U: for<'de> Deserialize<'de>,
Sends an HTTP DELETE request to the configured remote server
endpoint
- The remote endpoint. This gets joined with the base_url configured in the ::new() methoddata
- The optional data to send the the remote endpointoptions
- TheFetchOptions
for this call. Allows setting of headers and/or query params
§Example
use rust_fetch::Fetch;
use httpmock::prelude::*;
#[derive(serde::Deserialize, Debug, PartialEq)]
struct ToReturn {}
#[tokio::main]
async fn main() {
let server = MockServer::start();
server.mock(| when, then | {
when.path("/test").method(DELETE);
then.status(200).json_body(serde_json::json!({}));
});
let client = Fetch::new(&server.base_url(), None).unwrap();
let res = client.delete::<(), ToReturn>("/test", None, None).await.unwrap();
assert_eq!(&200, &res.status);
assert_eq!(ToReturn {}, res.body.unwrap());
}
Sourcepub async fn put<T, U>(
&self,
endpoint: &str,
data: Option<T>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<U>>where
T: Serialize,
U: for<'de> Deserialize<'de>,
pub async fn put<T, U>(
&self,
endpoint: &str,
data: Option<T>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<U>>where
T: Serialize,
U: for<'de> Deserialize<'de>,
Sends an HTTP PUT request to the configured remote server
endpoint
- The remote endpoint. This gets joined with the base_url configured in the ::new() methoddata
- The optional data to send the the remote endpointoptions
- TheFetchOptions
for this call. Allows setting of headers and/or query params
§Example
use rust_fetch::Fetch;
use httpmock::prelude::*;
#[derive(serde::Deserialize, Debug, PartialEq)]
struct ToReturn {}
#[tokio::main]
async fn main() {
let server = MockServer::start();
server.mock(| when, then | {
when.path("/test").method(PUT);
then.status(200).json_body(serde_json::json!({}));
});
let client = Fetch::new(&server.base_url(), None).unwrap();
let res = client.put::<(), ToReturn>("/test", None, None).await.unwrap();
assert_eq!(&200, &res.status);
assert_eq!(ToReturn {}, res.body.unwrap());
}
Sourcepub async fn patch<T, U>(
&self,
endpoint: &str,
data: Option<T>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<U>>where
T: Serialize,
U: for<'de> Deserialize<'de>,
pub async fn patch<T, U>(
&self,
endpoint: &str,
data: Option<T>,
options: Option<FetchOptions>,
) -> FetchResult<FetchResponse<U>>where
T: Serialize,
U: for<'de> Deserialize<'de>,
Sends an HTTP PATCH request to the configured remote server
endpoint
- The remote endpoint. This gets joined with the base_url configured in the ::new() methoddata
- The optional data to send the the remote endpointoptions
- TheFetchOptions
for this call. Allows setting of headers and/or query params
§Example
use rust_fetch::Fetch;
use httpmock::prelude::*;
#[derive(serde::Deserialize, Debug, PartialEq)]
struct ToReturn {}
#[tokio::main]
async fn main() {
let server = MockServer::start();
server.mock(| when, then | {
when.path("/test").method(httpmock::Method::PATCH);
then.status(200).json_body(serde_json::json!({}));
});
let client = Fetch::new(&server.base_url(), None).unwrap();
let res = client.patch::<(), ToReturn>("/test", None, None).await.unwrap();
assert_eq!(&200, &res.status);
assert_eq!(ToReturn {}, res.body.unwrap());
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Fetch
impl !RefUnwindSafe for Fetch
impl Send for Fetch
impl Sync for Fetch
impl Unpin for Fetch
impl !UnwindSafe for Fetch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more