webapp_frontend/
api.rs

1//! Api related helpers and utilities
2
3use failure::Fallible;
4use yew::{format::Cbor, services::fetch::Response as FetchResponse};
5
6/// A generic response type of the API
7pub type Response<T> = FetchResponse<Cbor<Fallible<T>>>;
8
9#[macro_export]
10/// Generic API fetch macro
11macro_rules! fetch {
12    ($request:expr => $api:expr, $link:expr, $msg:expr, $succ:expr, $err:expr) => {
13        match ::yew::services::fetch::Request::post(env!("API_URL").to_owned() + $api)
14            .body(Cbor(&$request))
15        {
16            Ok(body) => {
17                $succ();
18                Some(
19                    ::yew::services::fetch::FetchService::new()
20                        .fetch_binary(body, $link.send_back($msg)),
21                )
22            }
23            Err(_) => {
24                $err();
25                None
26            }
27        };
28    };
29}