qbit_api_rs/api/
search.rs

1use super::Endpoint;
2use crate::error::ClientError;
3use crate::types;
4use async_trait::async_trait;
5use reqwest::{Method, StatusCode};
6use std::borrow::Cow;
7
8/// # `/api/v2/search/start`
9pub struct Start {
10    pub f: types::search::StartForm,
11}
12
13#[async_trait]
14impl Endpoint for Start {
15    type Query = ();
16    type Form = types::search::StartForm;
17    type Response = types::search::StartResponse;
18    fn relative_path(&self) -> Cow<str> {
19        "/api/v2/search/start".into()
20    }
21    fn form(&self) -> Option<&Self::Form> {
22        Some(&self.f)
23    }
24    fn method(&self) -> reqwest::Method {
25        Method::POST
26    }
27    fn check_status(&self, status: reqwest::StatusCode) -> Option<ClientError> {
28        match status {
29            StatusCode::OK => None,
30            StatusCode::FORBIDDEN => Some(ClientError::NeedAuthentication),
31            StatusCode::CONFLICT => Some(ClientError::Conflict(
32                "User has reached the limit of max Running searches (currently set to 5)".into(),
33            )),
34            _ => Some(ClientError::Unknown),
35        }
36    }
37    async fn de_response(&self, res: reqwest::Response) -> Result<Self::Response, ClientError> {
38        Ok(res.json::<types::search::StartResponse>().await?)
39    }
40}
41
42/// # `/api/v2/search/stop`
43pub struct Stop {
44    pub f: types::search::StopForm,
45}
46
47#[async_trait]
48impl Endpoint for Stop {
49    type Query = ();
50    type Form = types::search::StopForm;
51    type Response = String;
52    fn relative_path(&self) -> Cow<str> {
53        "/api/v2/search/stop".into()
54    }
55    fn form(&self) -> Option<&Self::Form> {
56        Some(&self.f)
57    }
58    fn method(&self) -> reqwest::Method {
59        Method::POST
60    }
61    fn check_status(&self, status: reqwest::StatusCode) -> Option<ClientError> {
62        match status {
63            StatusCode::OK => None,
64            StatusCode::FORBIDDEN => Some(ClientError::NeedAuthentication),
65            StatusCode::NOT_FOUND => Some(ClientError::SearchJobNotFound { id: self.f.id }),
66            _ => Some(ClientError::Unknown),
67        }
68    }
69    async fn de_response(&self, res: reqwest::Response) -> Result<Self::Response, ClientError> {
70        Ok(res.text().await?)
71    }
72}
73
74/// # `/api/v2/search/status`
75pub struct Status {
76    pub q: types::search::StatusQuery,
77}
78
79#[async_trait]
80impl Endpoint for Status {
81    type Query = types::search::StatusQuery;
82    type Form = ();
83    type Response = types::search::StatusResponse;
84    fn relative_path(&self) -> Cow<str> {
85        "/api/v2/search/status".into()
86    }
87    fn query(&self) -> Option<&Self::Query> {
88        Some(&self.q)
89    }
90    fn method(&self) -> reqwest::Method {
91        Method::GET
92    }
93    fn check_status(&self, status: reqwest::StatusCode) -> Option<ClientError> {
94        match status {
95            StatusCode::OK => None,
96            StatusCode::FORBIDDEN => Some(ClientError::NeedAuthentication),
97            StatusCode::NOT_FOUND => Some(ClientError::SearchJobNotFound {
98                id: self.q.id.unwrap(),
99            }),
100            _ => Some(ClientError::Unknown),
101        }
102    }
103    async fn de_response(&self, res: reqwest::Response) -> Result<Self::Response, ClientError> {
104        Ok(res.json::<types::search::StatusResponse>().await?)
105    }
106}
107
108/// # `/api/v2/search/results`
109pub struct Results {
110    pub q: types::search::ResultsQuery,
111}
112
113#[async_trait]
114impl Endpoint for Results {
115    type Query = types::search::ResultsQuery;
116    type Form = ();
117    type Response = types::search::ResultsResponse;
118    fn relative_path(&self) -> Cow<str> {
119        "/api/v2/search/results".into()
120    }
121    fn query(&self) -> Option<&Self::Query> {
122        Some(&self.q)
123    }
124    fn method(&self) -> reqwest::Method {
125        Method::GET
126    }
127    fn check_status(&self, status: reqwest::StatusCode) -> Option<ClientError> {
128        match status {
129            StatusCode::OK => None,
130            StatusCode::FORBIDDEN => Some(ClientError::NeedAuthentication),
131            StatusCode::NOT_FOUND => Some(ClientError::SearchJobNotFound { id: self.q.id }),
132            StatusCode::CONFLICT => Some(ClientError::Conflict(
133                "Offset is too large or too small".into(),
134            )),
135            _ => Some(ClientError::Unknown),
136        }
137    }
138    async fn de_response(&self, res: reqwest::Response) -> Result<Self::Response, ClientError> {
139        Ok(res.json::<types::search::ResultsResponse>().await?)
140    }
141}
142
143/// # `/api/v2/search/delete`
144pub struct Delete {
145    pub f: types::search::DeleteForm,
146}
147
148#[async_trait]
149impl Endpoint for Delete {
150    type Query = ();
151    type Form = types::search::DeleteForm;
152    type Response = String;
153    fn relative_path(&self) -> Cow<str> {
154        "/api/v2/search/delete".into()
155    }
156    fn form(&self) -> Option<&Self::Form> {
157        Some(&self.f)
158    }
159    fn method(&self) -> reqwest::Method {
160        Method::POST
161    }
162    fn check_status(&self, status: reqwest::StatusCode) -> Option<ClientError> {
163        match status {
164            StatusCode::OK => None,
165            StatusCode::FORBIDDEN => Some(ClientError::NeedAuthentication),
166            StatusCode::NOT_FOUND => Some(ClientError::SearchJobNotFound { id: self.f.id }),
167            _ => Some(ClientError::Unknown),
168        }
169    }
170    async fn de_response(&self, res: reqwest::Response) -> Result<Self::Response, ClientError> {
171        Ok(res.text().await?)
172    }
173}