nadeo_api_rs/
client.rs

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use reqwest::header::{HeaderValue, AUTHORIZATION, CONTENT_LENGTH, LOCATION};
pub use reqwest::{Error as RqError, RequestBuilder, Response};
pub use serde_json::Value;
use tokio::sync::{SemaphorePermit, TryLockError};

use crate::{auth::*, urls::*};

pub enum MonthlyCampaignType {
    Royal,
    TOTD,
}

pub async fn run_req<'a>(
    (req, permit): (RequestBuilder, SemaphorePermit<'a>),
) -> Result<Value, RqError> {
    let r = req.send().await?.json().await?;
    drop(permit);
    Ok(r)
}

pub trait NadeoApiClient {
    async fn get_client(&self) -> &reqwest::Client;

    async fn rate_limit(&self) -> SemaphorePermit;

    async fn get_file_size(&self, url: &str) -> Result<u64, RqError> {
        let resp = self.get_client().await.head(url).send().await?;
        Ok(resp
            .headers()
            .get(CONTENT_LENGTH)
            .map_or(0, |v| v.to_str().unwrap_or("0").parse().unwrap_or(1)))
    }

    fn get_auth_token(&self, audience: NadeoAudience) -> Result<String, TryLockError>;

    async fn aget_auth_token(&self, audience: NadeoAudience) -> String;

    fn get_auth_header(&self, audience: NadeoAudience) -> String {
        format!("nadeo_v1 t={}", &self.get_auth_token(audience).unwrap())
    }

    async fn aget_auth_header(&self, audience: NadeoAudience) -> String {
        format!("nadeo_v1 t={}", &self.aget_auth_token(audience).await)
    }

    async fn get_auth_header_value(&self, audience: NadeoAudience) -> HeaderValue {
        let mut hv = HeaderValue::from_str(&self.aget_auth_header(audience).await).unwrap();
        hv.set_sensitive(true);
        hv
    }

    async fn oauth_get<'a>(
        &self,
        path: &str,
        token: &OAuthToken,
        _permit: &SemaphorePermit<'a>,
    ) -> RequestBuilder {
        let rb = self
            .get_client()
            .await
            .get(&format!("{}{}", OAUTH_URL, path))
            .header(AUTHORIZATION, token.get_authz_header());
        rb
    }

    async fn core_get(&self, path: &str) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .get(&format!("{}{}", CORE_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Core).await),
            self.rate_limit().await,
        )
    }

    async fn run_core_get(&self, path: &str) -> Result<Value, RqError> {
        run_req(self.core_get(path).await).await
    }

    async fn core_post_bytes(&self, path: &str, body: &[u8]) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .post(&format!("{}{}", CORE_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Core).await)
                .body(body.to_vec()),
            self.rate_limit().await,
        )
    }
    async fn run_core_post_bytes(&self, path: &str, body: &[u8]) -> Result<Value, RqError> {
        run_req(self.core_post_bytes(path, body).await).await
    }

    async fn core_post(&self, path: &str, body: &Value) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .post(&format!("{}{}", CORE_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Core).await)
                .json(body),
            self.rate_limit().await,
        )
    }
    async fn run_core_post(&self, path: &str, body: &Value) -> Result<Value, RqError> {
        run_req(self.core_post(path, body).await).await
    }

    async fn live_get(&self, path: &str) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .get(&format!("{}{}", LIVE_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Live).await),
            self.rate_limit().await,
        )
    }
    async fn run_live_get(&self, path: &str) -> Result<Value, RqError> {
        run_req(self.live_get(path).await).await
    }

    async fn live_post_bytes(&self, path: &str, body: &[u8]) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .post(&format!("{}{}", LIVE_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Live).await)
                .body(body.to_vec()),
            self.rate_limit().await,
        )
    }
    async fn run_live_post_bytes(&self, path: &str, body: &[u8]) -> Result<Value, RqError> {
        run_req(self.live_post_bytes(path, body).await).await
    }

    async fn live_post(&self, path: &str, body: &Value) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .post(&format!("{}{}", LIVE_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Live).await)
                .json(body),
            self.rate_limit().await,
        )
    }
    async fn run_live_post(&self, path: &str, body: &Value) -> Result<Value, RqError> {
        run_req(self.live_post(path, body).await).await
    }

    async fn meet_get(&self, path: &str) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .get(&format!("{}{}", MEET_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Live).await),
            self.rate_limit().await,
        )
    }
    async fn run_meet_get(&self, path: &str) -> Result<Value, RqError> {
        run_req(self.meet_get(path).await).await
    }

    async fn meet_post_bytes(&self, path: &str, body: &[u8]) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .post(&format!("{}{}", MEET_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Live).await)
                .body(body.to_vec()),
            self.rate_limit().await,
        )
    }
    async fn run_meet_post_bytes(&self, path: &str, body: &[u8]) -> Result<Value, RqError> {
        run_req(self.meet_post_bytes(path, body).await).await
    }

    async fn meet_post(&self, path: &str, body: &Value) -> (RequestBuilder, SemaphorePermit) {
        (
            self.get_client()
                .await
                .post(&format!("{}{}", MEET_URL, path))
                .header(AUTHORIZATION, self.get_auth_header_value(Live).await)
                .json(body),
            self.rate_limit().await,
        )
    }
    async fn run_meet_post(&self, path: &str, body: &Value) -> Result<Value, RqError> {
        run_req(self.meet_post(path, body).await).await
    }
}