pub struct ApiClient { /* private fields */ }Expand description
Before one can do any api request, an ApiClient must be constructed
Implementations§
Source§impl ApiClient
impl ApiClient
Sourcepub async fn delete(&self, uri: &str) -> Result<()>
pub async fn delete(&self, uri: &str) -> Result<()>
§Example
Try to delete a post with specific id from Json Placeholder
let base = "https://jsonplaceholder.typicode.com/";
ApiClientBuilder::new(base)
.build()?
.delete("posts/1")
.await?;
Sourcepub async fn get<R>(&self, uri: &str) -> Result<R>where
R: DeserializeOwned,
pub async fn get<R>(&self, uri: &str) -> Result<R>where
R: DeserializeOwned,
§Example 1
Try to return a list of posts from JsonPlaceholder
let base = "https://jsonplaceholder.typicode.com/";
let posts = ApiClientBuilder::new(base)
.build()?
.get::<Vec<Post>>("posts")
.await?;
§Example 2
Try to return a single post with specific id from Json Placeholder
let base = "https://jsonplaceholder.typicode.com/";
let post = ApiClientBuilder::new(base)
.build()?
.get::<Post>("posts/1")
.await?;
Sourcepub async fn post<T, R>(&self, uri: &str, object: T) -> Result<R>where
T: Serialize,
R: DeserializeOwned,
pub async fn post<T, R>(&self, uri: &str, object: T) -> Result<R>where
T: Serialize,
R: DeserializeOwned,
§Example
Try to create a new post on Json Placeholder If successful the created post is returned
let new_post = Post {
id: None,
title: "Hallo".to_owned(),
body: "Hallo".to_owned(),
user_id: Some(34),
};
let base = "https://jsonplaceholder.typicode.com/";
let post = ApiClientBuilder::new(base)
.build()?
.post::<_, Post>("posts", new_post)
.await?;
Sourcepub async fn token_request<T>(
&mut self,
uri: &str,
signature: &str,
object: T,
) -> Result<()>where
T: Serialize,
pub async fn token_request<T>(
&mut self,
uri: &str,
signature: &str,
object: T,
) -> Result<()>where
T: Serialize,
use post_validation to get a Json Web Token
Sourcepub async fn put<T, R>(&self, uri: &str, object: T) -> Result<R>where
T: Serialize,
R: DeserializeOwned,
pub async fn put<T, R>(&self, uri: &str, object: T) -> Result<R>where
T: Serialize,
R: DeserializeOwned,
§Example
Try to change a post with specific id on Json Placeholder If successful the changed post is returned
let changed_post = Post {
id: None,
title: "Hallo".to_owned(),
body: "Hallo".to_owned(),
user_id: Some(34),
};
let base = "https://jsonplaceholder.typicode.com/";
let post = ApiClientBuilder::new(base)
.build()?
.put::<_, Post>("posts/1", changed_post)
.await?;
Auto Trait Implementations§
impl !RefUnwindSafe for ApiClient
impl !UnwindSafe for ApiClient
impl Freeze for ApiClient
impl Send for ApiClient
impl Sync for ApiClient
impl Unpin for ApiClient
impl UnsafeUnpin for ApiClient
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