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
mod builder;
mod connector;
mod ratelimiter;

pub mod error;

use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};

use hyper::{
    client::ResponseFuture,
    header::{CONTENT_LENGTH, CONTENT_TYPE, USER_AGENT},
    http::HeaderValue,
    Body, Client as HyperClient, Method, Request as HyperRequest,
};

pub use self::builder::OrdrClientBuilder;
pub(crate) use self::ratelimiter::RatelimiterKind;
use self::{connector::Connector, error::ClientError, ratelimiter::Ratelimiter};

use crate::{
    model::{RenderSkinOption, Verification},
    request::{
        CommissionRender, GetRenderList, GetServerList, GetServerOnlineCount, GetSkinCustom,
        GetSkinList, OrdrFuture, Request,
    },
    util::multipart::Form,
};

const BASE_URL: &str = "https://apis.issou.best/ordr/";
const ROSU_RENDER_USER_AGENT: &str = concat!("rosu-render (", env!("CARGO_PKG_VERSION"), ")");

type HttpClient = HyperClient<Connector>;

/// Client to access the o!rdr API.
///
/// Cheap to clone.
#[derive(Clone)]
pub struct OrdrClient {
    inner: Arc<OrdrRef>,
}

struct OrdrRef {
    pub(super) http: HttpClient,
    pub(super) ratelimiter: Ratelimiter,
    // don't perform further requests when we're banned
    pub(super) banned: Arc<AtomicBool>,
    pub(super) verification: Option<Verification>,
}

impl OrdrClient {
    /// Create a new [`OrdrClient`] based on a default [`OrdrClientBuilder`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new builder to create an [`OrdrClient`].
    pub fn builder() -> OrdrClientBuilder {
        OrdrClientBuilder::new()
    }

    /// Get info of a custom skin.
    ///
    /// You must provide the ID of the custom skin.
    pub const fn custom_skin_info(&self, id: u32) -> GetSkinCustom<'_> {
        GetSkinCustom::new(self, id)
    }

    /// Send a render request to o!rdr via replay file.
    pub const fn render_with_replay_file<'a>(
        &'a self,
        replay_file: &'a [u8],
        username: &'a str,
        skin: &'a RenderSkinOption<'a>,
    ) -> CommissionRender<'a> {
        CommissionRender::with_file(self, replay_file, username, skin)
    }

    /// Send a render request to o!rdr via replay url.
    pub const fn render_with_replay_url<'a>(
        &'a self,
        url: &'a str,
        username: &'a str,
        skin: &'a RenderSkinOption<'a>,
    ) -> CommissionRender<'a> {
        CommissionRender::with_url(self, url, username, skin)
    }

    /// Get a paginated list of all renders.
    pub const fn render_list(&self) -> GetRenderList<'_> {
        GetRenderList::new(self)
    }

    /// Get a list of available servers.
    pub const fn server_list(&self) -> GetServerList<'_> {
        GetServerList::new(self)
    }

    /// Get the amount of online servers.
    pub const fn server_online_count(&self) -> GetServerOnlineCount<'_> {
        GetServerOnlineCount::new(self)
    }

    /// Get a paginated list of all available skins.
    pub const fn skin_list(&self) -> GetSkinList<'_> {
        GetSkinList::new(self)
    }

    pub(crate) fn verification(&self) -> Option<&Verification> {
        self.inner.verification.as_ref()
    }

    pub(crate) fn request<T>(&self, req: Request) -> OrdrFuture<T> {
        self.try_request::<T>(req).unwrap_or_else(OrdrFuture::error)
    }

    fn try_request<T>(&self, req: Request) -> Result<OrdrFuture<T>, ClientError> {
        let Request {
            form,
            method,
            path,
            ratelimiter,
        } = req;

        let inner = self.try_request_raw(form, method, &path)?;

        Ok(OrdrFuture::new(
            Box::pin(inner),
            Arc::clone(&self.inner.banned),
            self.inner.ratelimiter.get(ratelimiter).acquire_owned(1),
        ))
    }

    fn try_request_raw(
        &self,
        form: Option<Form>,
        method: Method,
        path: &str,
    ) -> Result<ResponseFuture, ClientError> {
        if self.inner.banned.load(Ordering::Relaxed) {
            return Err(ClientError::Unauthorized);
        }

        let mut url = String::with_capacity(BASE_URL.len() + path.len());
        url.push_str(BASE_URL);
        url.push_str(path);
        debug!(?url);

        debug_assert!(method != Method::POST || form.is_some());

        let mut builder = HyperRequest::builder().method(method).uri(&url);

        if let Some(headers) = builder.headers_mut() {
            if let Some(ref form) = form {
                headers.insert(CONTENT_LENGTH, HeaderValue::from(form.len()));

                if let Ok(content_type) = HeaderValue::try_from(form.content_type()) {
                    headers.insert(CONTENT_TYPE, content_type);
                }
            }

            headers.insert(USER_AGENT, HeaderValue::from_static(ROSU_RENDER_USER_AGENT));
        }

        let try_req = if let Some(form) = form {
            builder.body(Body::from(form.build()))
        } else {
            builder.body(Body::empty())
        };

        let req = try_req.map_err(|source| ClientError::BuildingRequest {
            source: Box::new(source),
        })?;

        Ok(self.inner.http.request(req))
    }
}

impl Default for OrdrClient {
    fn default() -> Self {
        Self::builder().build()
    }
}