Struct rest_json_client::ApiClient
source · 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 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 Send for ApiClient
impl Sync for ApiClient
impl Unpin for ApiClient
impl !UnwindSafe 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