bazzar/web/src/api/public.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

2022-05-10 16:20:37 +02:00
use model::{AccessTokenString, RefreshTokenString};
2022-05-09 16:17:27 +02:00
use seed::prelude::*;
pub async fn fetch_products() -> fetch::Result<model::api::Products> {
Request::new("/api/v1/products")
.method(Method::Get)
.fetch()
.await?
.check_status()?
.json()
.await
}
2022-05-10 16:20:37 +02:00
2022-05-11 15:56:41 +02:00
pub async fn fetch_product(product_id: model::ProductId) -> fetch::Result<model::api::Product> {
Request::new(format!("/api/v1/product/{}", product_id))
.method(Method::Get)
.fetch()
.await?
.check_status()?
.json()
.await
}
2022-05-10 16:20:37 +02:00
pub async fn fetch_me(access_token: AccessTokenString) -> fetch::Result<model::Account> {
Request::new("/api/v1/me")
.header(fetch::Header::bearer(access_token.as_str()))
.method(Method::Get)
.fetch()
.await?
.check_status()?
.json()
.await
}
pub async fn sign_in(input: model::api::SignInInput) -> fetch::Result<model::api::SignInOutput> {
Request::new("/api/v1/sign-in")
.method(Method::Post)
.json(&input)?
.fetch()
.await?
.check_status()?
.json()
.await
}
pub async fn verify_token(access_token: AccessTokenString) -> fetch::Result<String> {
Request::new("/api/v1/token/verify")
.method(Method::Post)
.header(fetch::Header::bearer(access_token.as_str()))
.fetch()
.await?
.check_status()?
.json()
.await
}
pub async fn refresh_token(
access_token: RefreshTokenString,
) -> fetch::Result<model::api::SignInOutput> {
Request::new("/api/v1/token/refresh")
.method(Method::Post)
.header(fetch::Header::bearer(access_token.as_str()))
.fetch()
.await?
.check_status()?
.json()
.await
}