summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-04-07 19:47:55 +0200
committerMatthias Beyer <mail@beyermatthias.de>2021-04-07 19:47:55 +0200
commit3f8c8c1e5da70b1c43d0fcea225330ab290fdd0c (patch)
tree2b990fb88c1e37fe434bebd7497af93fb31f8f4f
parented3af2c9e305e7d693f8961f01279b084511fbde (diff)
Rewrite Request API to not return Response type, but actual queried objects
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--librepology/src/v1/api/client.rs2
-rw-r--r--librepology/src/v1/api/mod.rs3
-rw-r--r--librepology/src/v1/api/request.rs38
-rw-r--r--librepology/src/v1/api/response.rs13
4 files changed, 29 insertions, 27 deletions
diff --git a/librepology/src/v1/api/client.rs b/librepology/src/v1/api/client.rs
index 0682566..554aa86 100644
--- a/librepology/src/v1/api/client.rs
+++ b/librepology/src/v1/api/client.rs
@@ -10,6 +10,8 @@ static APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_VERSION"),
);
+/// Api Client for repology
+///
#[derive(Debug)]
pub struct ApiClient {
pub(super) endpoint_url: &'static str,
diff --git a/librepology/src/v1/api/mod.rs b/librepology/src/v1/api/mod.rs
index 34fa9a2..6f7b12d 100644
--- a/librepology/src/v1/api/mod.rs
+++ b/librepology/src/v1/api/mod.rs
@@ -7,6 +7,3 @@ pub use request_builder::*;
mod request;
pub use request::*;
-mod response;
-pub use response::*;
-
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,
}
}
}
diff --git a/librepology/src/v1/api/response.rs b/librepology/src/v1/api/response.rs
deleted file mode 100644
index 9f52ab8..0000000
--- a/librepology/src/v1/api/response.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-use crate::v1::error::Result;
-use crate::v1::error::RepologyError;
-
-#[derive(Debug)]
-pub struct Response(pub(super) String);
-
-impl Response {
- pub fn deserialize<'de, T: serde::Deserialize<'de>>(&'de self) -> Result<T> {
- serde_json::from_str(&self.0).map_err(RepologyError::from)
- }
-}
-
-