speedrun_api/api/
client.rs

1use std::error::Error;
2
3use async_trait::async_trait;
4use bytes::Bytes;
5use http::{request::Builder as RequestBuilder, Response};
6use url::Url;
7
8use super::error::ApiError;
9
10/// A trait representing a client which can communicate with speedrun.com via
11/// REST
12pub trait RestClient {
13    /// The error that may occur for this client
14    type Error: Error + Send + Sync + 'static;
15
16    /// Get the URL for the endpoint for the client.
17    ///
18    /// This method adds the hostname for the target api.
19    fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>>;
20
21    /// If the client has an API key
22    fn has_api_key(&self) -> bool;
23}
24
25/// A trait representing a client which can communicate with speedrun.com
26pub trait Client: RestClient {
27    /// Send a REST query
28    fn rest(
29        &self,
30        request: RequestBuilder,
31        body: Vec<u8>,
32    ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
33}
34
35/// A trait representing an asynchronous client which can communicate with
36/// speedrun.com
37#[async_trait]
38pub trait AsyncClient: RestClient {
39    /// Send a REST query asynchronously
40    async fn rest_async(
41        &self,
42        request: RequestBuilder,
43        body: Vec<u8>,
44    ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
45}