2022-05-28 14:03:14 +02:00
|
|
|
use model::api::OrderAddressInput;
|
2022-11-23 16:31:11 +01:00
|
|
|
use model::v2::ProductVariantId;
|
2022-06-07 11:36:01 +02:00
|
|
|
use model::{AccessTokenString, AddressId, PaymentMethod, RefreshTokenString};
|
2022-05-15 10:30:15 +02:00
|
|
|
use seed::fetch::{Header, Method, Request};
|
2022-05-09 16:17:27 +02:00
|
|
|
|
2022-05-15 10:30:15 +02:00
|
|
|
use crate::api::perform;
|
2022-05-19 07:47:47 +02:00
|
|
|
use crate::NetRes;
|
2022-05-15 10:30:15 +02:00
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn config() -> NetRes<model::api::Config> {
|
2022-05-18 15:40:50 +02:00
|
|
|
perform(Request::new("/config").method(Method::Get)).await
|
2022-05-17 16:04:29 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn fetch_products() -> NetRes<model::api::Products> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(Request::new("/api/v1/products").method(Method::Get)).await
|
2022-05-09 16:17:27 +02:00
|
|
|
}
|
2022-05-10 16:20:37 +02:00
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn fetch_product(product_id: model::ProductId) -> NetRes<model::api::Product> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(Request::new(format!("/api/v1/product/{}", product_id)).method(Method::Get)).await
|
2022-05-11 15:56:41 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn fetch_me(access_token: AccessTokenString) -> NetRes<model::api::Account> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/me")
|
|
|
|
.header(Header::bearer(access_token.as_str()))
|
|
|
|
.method(Method::Get),
|
|
|
|
)
|
|
|
|
.await
|
2022-05-10 16:20:37 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn sign_in(input: model::api::SignInInput) -> NetRes<model::api::SessionOutput> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/sign-in")
|
|
|
|
.method(Method::Post)
|
|
|
|
.json(&input)
|
|
|
|
.map_err(crate::api::NetRes::Http)?,
|
|
|
|
)
|
|
|
|
.await
|
2022-05-10 16:20:37 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn verify_token(access_token: AccessTokenString) -> NetRes<String> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/token/verify")
|
|
|
|
.method(Method::Post)
|
|
|
|
.header(Header::bearer(access_token.as_str())),
|
|
|
|
)
|
|
|
|
.await
|
2022-05-10 16:20:37 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn refresh_token(access_token: RefreshTokenString) -> NetRes<model::api::SessionOutput> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/token/refresh")
|
|
|
|
.method(Method::Post)
|
|
|
|
.header(Header::bearer(access_token.as_str())),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
pub async fn sign_up(input: model::api::CreateAccountInput) -> NetRes<model::api::SessionOutput> {
|
2022-05-15 10:30:15 +02:00
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/register")
|
|
|
|
.method(Method::Post)
|
|
|
|
.json(&input)
|
2022-05-19 16:13:27 +02:00
|
|
|
.map_err(NetRes::Http)?,
|
2022-05-15 10:30:15 +02:00
|
|
|
)
|
|
|
|
.await
|
2022-05-10 16:20:37 +02:00
|
|
|
}
|
2022-05-19 07:47:47 +02:00
|
|
|
|
|
|
|
pub async fn update_cart_item(
|
|
|
|
access_token: &AccessTokenString,
|
2022-11-23 16:31:11 +01:00
|
|
|
product_variant_id: ProductVariantId,
|
2022-05-19 07:47:47 +02:00
|
|
|
quantity: model::Quantity,
|
|
|
|
quantity_unit: model::QuantityUnit,
|
|
|
|
) -> NetRes<model::api::UpdateItemOutput> {
|
|
|
|
let input = model::api::UpdateItemInput {
|
2022-11-23 16:31:11 +01:00
|
|
|
product_variant_id,
|
2022-05-19 07:47:47 +02:00
|
|
|
quantity,
|
|
|
|
quantity_unit,
|
|
|
|
};
|
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/shopping-cart-item")
|
|
|
|
.method(Method::Put)
|
|
|
|
.header(Header::bearer(access_token.as_str()))
|
|
|
|
.json(&input)
|
2022-05-19 16:13:27 +02:00
|
|
|
.map_err(NetRes::Http)?,
|
2022-05-19 07:47:47 +02:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_cart(
|
|
|
|
access_token: AccessTokenString,
|
|
|
|
items: Vec<crate::shopping_cart::Item>,
|
2022-05-19 16:13:27 +02:00
|
|
|
notes: String,
|
|
|
|
payment_method: Option<model::PaymentMethod>,
|
2022-05-19 07:47:47 +02:00
|
|
|
) -> NetRes<model::api::UpdateCartOutput> {
|
|
|
|
let input = model::api::UpdateCartInput {
|
2022-05-19 16:13:27 +02:00
|
|
|
notes,
|
2022-05-19 07:47:47 +02:00
|
|
|
items: items
|
|
|
|
.into_iter()
|
|
|
|
.map(
|
|
|
|
|crate::shopping_cart::Item {
|
2022-11-23 16:31:11 +01:00
|
|
|
product_variant_id,
|
2022-05-19 07:47:47 +02:00
|
|
|
quantity,
|
|
|
|
quantity_unit,
|
|
|
|
}| model::api::UpdateItemInput {
|
2022-11-23 16:31:11 +01:00
|
|
|
product_variant_id,
|
2022-05-19 07:47:47 +02:00
|
|
|
quantity,
|
|
|
|
quantity_unit,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.collect(),
|
2022-05-19 16:13:27 +02:00
|
|
|
payment_method,
|
2022-05-19 07:47:47 +02:00
|
|
|
};
|
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/shopping-cart")
|
|
|
|
.method(Method::Put)
|
|
|
|
.header(Header::bearer(access_token.as_str()))
|
|
|
|
.json(&input)
|
2022-05-19 16:13:27 +02:00
|
|
|
.map_err(NetRes::Http)?,
|
2022-05-19 07:47:47 +02:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2022-05-23 14:11:56 +02:00
|
|
|
|
2022-05-28 14:03:14 +02:00
|
|
|
pub async fn place_account_order(
|
|
|
|
access_token: AccessTokenString,
|
|
|
|
email: String,
|
|
|
|
phone: String,
|
|
|
|
first_name: String,
|
|
|
|
last_name: String,
|
|
|
|
language: String,
|
2022-06-07 11:36:01 +02:00
|
|
|
payment_method: PaymentMethod,
|
2022-05-28 14:03:14 +02:00
|
|
|
currency: String,
|
|
|
|
address_id: Option<AddressId>,
|
|
|
|
) -> NetRes<model::api::PlaceOrderResult> {
|
2022-05-23 14:11:56 +02:00
|
|
|
let input = model::api::CreateOrderInput {
|
2022-05-28 14:03:14 +02:00
|
|
|
email,
|
|
|
|
phone,
|
|
|
|
first_name,
|
|
|
|
last_name,
|
|
|
|
language,
|
2022-06-07 11:36:01 +02:00
|
|
|
payment_method,
|
2022-05-28 14:03:14 +02:00
|
|
|
currency,
|
|
|
|
address: address_id
|
|
|
|
.map(OrderAddressInput::AccountAddress)
|
|
|
|
.unwrap_or(OrderAddressInput::DefaultAccountAddress),
|
2022-05-23 14:11:56 +02:00
|
|
|
};
|
|
|
|
perform(
|
|
|
|
Request::new("/api/v1/order")
|
|
|
|
.method(Method::Post)
|
|
|
|
.header(Header::bearer(access_token.as_str()))
|
|
|
|
.json(&input)
|
|
|
|
.map_err(NetRes::Http)?,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|