1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
pub mod account;
pub mod agent;
pub mod auth;
pub mod client;
pub mod config;
pub mod credential;
pub mod friend;
pub mod profile;

use std::ops::Deref;

use reqwest::RequestBuilder;
use serde::{de::DeserializeOwned, Deserialize};
use thiserror::Error;

#[derive(Debug, Error)]
#[error(transparent)]
#[non_exhaustive]
pub enum RequestError {
    Reqwest(#[from] reqwest::Error),
    Json(#[from] serde_json::Error),
    Url(#[from] url::ParseError),
}

pub type RequestResult<T> = Result<T, RequestError>;

#[derive(Debug, Error)]
pub enum ApiError {
    #[error(transparent)]
    Request(RequestError),

    #[error("api responded with error. status: {0}")]
    Status(i32),
}

impl<T: Into<RequestError>> From<T> for ApiError {
    fn from(value: T) -> Self {
        Self::Request(value.into())
    }
}

pub type ApiResult<T> = Result<T, ApiError>;

pub(crate) async fn read_response(request: RequestBuilder) -> ApiResult<impl Deref<Target = [u8]>> {
    #[derive(Debug, Clone, Copy, Deserialize)]
    struct ApiStatus {
        pub status: i32,
    }

    let data = request.send().await?.bytes().await?;

    match serde_json::from_slice::<ApiStatus>(&data)?.status {
        0 => Ok(data),
        status => Err(ApiError::Status(status)),
    }
}

pub(crate) async fn read_structured_response<T: DeserializeOwned>(
    request: RequestBuilder,
) -> ApiResult<T> {
    Ok(serde_json::from_slice(&read_response(request).await?)?)
}