diff options
Diffstat (limited to 'librepology/src/v1/api/request.rs')
-rw-r--r-- | librepology/src/v1/api/request.rs | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/librepology/src/v1/api/request.rs b/librepology/src/v1/api/request.rs index b923426..7da16a2 100644 --- a/librepology/src/v1/api/request.rs +++ b/librepology/src/v1/api/request.rs @@ -2,20 +2,22 @@ use crate::v1::api::ApiClient; use crate::v1::api::NumberOrRange; use crate::v1::api::ProjectRequestBuilderWithName; use crate::v1::api::ProjectRequestFilteredBuilder; -use crate::v1::api::Response; use crate::v1::error::RepologyError; use crate::v1::error::Result; use crate::v1::api::ProblemsForMaintainerAndRepoRequestBuilder; use crate::v1::api::ProblemsForRepoRequestBuilder; +use crate::v1::types::response::Problem; +use crate::v1::types::response::Repo; #[derive(Debug)] -pub struct Request<'a> { +pub struct Request<'a, Output: serde::de::DeserializeOwned> { client: &'a ApiClient, - request_string: String + request_string: String, + _output: std::marker::PhantomData<Output>, } -impl<'a> Request<'a> { - pub async fn perform(self) -> Result<Response> { +impl<'a, Output: serde::de::DeserializeOwned> Request<'a, Output> { + pub async fn perform(self) -> Result<Output> { self.client .client .get(format!("{}{}", self.client.endpoint_url, self.request_string)) @@ -23,27 +25,34 @@ impl<'a> Request<'a> { .await? .text() .await - .map(Response) .map_err(RepologyError::from) + .and_then(|data| serde_json::from_str(&data).map_err(RepologyError::from)) } } pub trait ToRequest<'a> { - fn to_request(self) -> Request<'a>; + type Output: serde::de::DeserializeOwned; + + fn to_request(self) -> Request<'a, Self::Output>; } impl<'a> ToRequest<'a> for ProjectRequestBuilderWithName<'a> { - fn to_request(self) -> Request<'a> { + type Output = Vec<Repo>; + + fn to_request(self) -> Request<'a, Self::Output> { Request { client: self.0, request_string: format!("projects/{}", self.1), + _output: std::marker::PhantomData, } } } impl<'a> ToRequest<'a> for ProjectRequestFilteredBuilder<'a> { - fn to_request(self) -> Request<'a> { + type Output = Vec<Repo>; + + fn to_request(self) -> Request<'a, Self::Output> { let mut buf = Vec::new(); if let Some(search) = self.search.as_ref() { @@ -122,25 +131,32 @@ impl<'a> ToRequest<'a> for ProjectRequestFilteredBuilder<'a> { Request { client: self.client, request_string: format!("&{}", buf.join("&")), + _output: std::marker::PhantomData, } } } impl<'a> ToRequest<'a> for ProblemsForRepoRequestBuilder<'a> { - fn to_request(self) -> Request<'a> { + type Output = Vec<Problem>; + + fn to_request(self) -> Request<'a, Self::Output> { Request { client: self.0, request_string: format!("repository/{}/problems", self.1), + _output: std::marker::PhantomData, } } } impl<'a> ToRequest<'a> for ProblemsForMaintainerAndRepoRequestBuilder<'a> { - fn to_request(self) -> Request<'a> { + type Output = Vec<Problem>; + + fn to_request(self) -> Request<'a, Self::Output> { Request { client: self.client, request_string: format!("maintainer/{}/problems-for-repo/{}", self.maintainer, self.repo), + _output: std::marker::PhantomData, } } } |