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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![feature(associated_type_defaults)]
#![deny(rust_2018_idioms)]

use core::fmt;
use thiserror::Error;

use async_trait::async_trait;
pub use github_api_octocat::end_points;
use github_api_octocat::end_points::EndPoints;

#[cfg(not(target_family = "wasm"))]
use reqwest::{Body, StatusCode};

use serde::{de::DeserializeOwned, Serialize};
#[cfg(target_family = "wasm")]
use std::num::NonZeroU16;
#[cfg(target_family = "wasm")]
use worker::wasm_bindgen::JsValue;

#[cfg(feature = "builders")]
pub mod builders;
#[cfg(feature = "client")]
pub mod client;
pub mod methods;
pub mod model;

#[derive(Error, Debug)]
pub enum GithubRestError {
    #[cfg(not(target_family = "wasm"))]
    #[error(transparent)]
    ReqwestError(#[from] reqwest::Error),
    #[cfg(target_family = "wasm")]
    #[error(transparent)]
    WorkerError(#[from] worker::Error),
    #[cfg(target_family = "wasm")]
    ResponseError(NonZeroU16, String),
    #[error(transparent)]
    JsonError(#[from] serde_json::Error),
    #[cfg(not(target_family = "wasm"))]
    ResponseError(StatusCode, String),
    NotAuthorized(String),
    AnyError(),
}

impl fmt::Display for GithubRestError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Error occurred as you can see")
    }
}

#[async_trait]
pub trait Requester: Send + Sync {
    #[cfg(not(target_family = "wasm"))]
    type Body: From<String> = Body;

    #[cfg(target_family = "wasm")]
    type Body: From<String> = JsValue;

    async fn raw_req<T, V>(
        &self,
        url: EndPoints,
        query: Option<&T>,
        body: Option<V>,
    ) -> Result<String, GithubRestError>
    where
        T: Serialize + ?Sized + std::marker::Send + std::marker::Sync,
        V: Into<Self::Body> + std::marker::Send;

    async fn req<T, V, A: DeserializeOwned>(
        &self,
        url: EndPoints,
        query: Option<&T>,
        body: Option<V>,
    ) -> Result<A, GithubRestError>
    where
        T: Serialize + ?Sized + std::marker::Send + std::marker::Sync,
        V: Into<Self::Body> + std::marker::Send;
}