misskey_core/
client.rs

1use crate::api::{Request, UploadFileRequest};
2use crate::model::ApiResult;
3
4use futures_core::future::BoxFuture;
5use mime::Mime;
6
7/// Abstraction over API clients.
8pub trait Client {
9    /// The error type produced by the client when an error occurs.
10    type Error: std::error::Error;
11
12    /// Dispatch an API request.
13    ///
14    /// Takes [`Request`] and returns a future that waits for the [`Response`][`Request::Response`].
15    fn request<R: Request>(
16        &self,
17        request: R,
18    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>>;
19}
20
21impl<C: Client + ?Sized> Client for &C {
22    type Error = C::Error;
23
24    fn request<R: Request>(
25        &self,
26        request: R,
27    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>> {
28        C::request(self, request)
29    }
30}
31
32impl<C: Client + ?Sized> Client for &mut C {
33    type Error = C::Error;
34
35    fn request<R: Request>(
36        &self,
37        request: R,
38    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>> {
39        C::request(self, request)
40    }
41}
42
43impl<C: Client + ?Sized> Client for Box<C> {
44    type Error = C::Error;
45
46    fn request<R: Request>(
47        &self,
48        request: R,
49    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>> {
50        C::request(self, request)
51    }
52}
53
54/// Abstraction over API clients that can upload files.
55pub trait UploadFileClient: Client {
56    /// Dispatches an API request with file.
57    ///
58    /// Takes the file to be attatched and [`UploadFileRequest`], then returns a future that waits for the [`Request::Response`].
59    fn request_with_file<R, T>(
60        &self,
61        request: R,
62        type_: Mime,
63        file_name: String,
64        content: T,
65    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>>
66    where
67        R: UploadFileRequest,
68        T: std::io::Read + Send + Sync + 'static;
69}
70
71impl<C: ?Sized> UploadFileClient for &C
72where
73    C: UploadFileClient,
74{
75    fn request_with_file<R, T>(
76        &self,
77        request: R,
78        type_: Mime,
79        file_name: String,
80        content: T,
81    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>>
82    where
83        R: UploadFileRequest,
84        T: std::io::Read + Send + Sync + 'static,
85    {
86        C::request_with_file(self, request, type_, file_name, content)
87    }
88}
89
90impl<C: ?Sized> UploadFileClient for &mut C
91where
92    C: UploadFileClient,
93{
94    fn request_with_file<R, T>(
95        &self,
96        request: R,
97        type_: Mime,
98        file_name: String,
99        content: T,
100    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>>
101    where
102        R: UploadFileRequest,
103        T: std::io::Read + Send + Sync + 'static,
104    {
105        C::request_with_file(self, request, type_, file_name, content)
106    }
107}
108
109impl<C: ?Sized> UploadFileClient for Box<C>
110where
111    C: UploadFileClient,
112{
113    fn request_with_file<R, T>(
114        &self,
115        request: R,
116        type_: Mime,
117        file_name: String,
118        content: T,
119    ) -> BoxFuture<Result<ApiResult<R::Response>, Self::Error>>
120    where
121        R: UploadFileRequest,
122        T: std::io::Read + Send + Sync + 'static,
123    {
124        C::request_with_file(self, request, type_, file_name, content)
125    }
126}