summaryrefslogtreecommitdiffstats
path: root/librepology/src/v1/api/request.rs
blob: 8b1df108b06a7e418117cad4273144364cedea9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use result_inspect::*;

use crate::v1::api::ApiClient;
use crate::v1::api::NumberOrRange;
use crate::v1::api::ProjectRequestBuilderWithName;
use crate::v1::api::ProjectRequestFilteredBuilder;
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::Package;

#[derive(Debug)]
pub struct Request<'a, Output: serde::de::DeserializeOwned> {
    client: &'a ApiClient,
    request_string: String,
    _output: std::marker::PhantomData<Output>,
}

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))
            .send()
            .await
            .inspect(|response| log::debug!("Response: {:?}", response))?
            .text()
            .await
            .inspect(|text| log::debug!("Response text: {:?}", text))
            .map_err(RepologyError::from)
            .and_then(|data| serde_json::from_str(&data).map_err(RepologyError::from))
    }
}

pub trait ToRequest<'a> {
    type Output: serde::de::DeserializeOwned;

    fn to_request(self) -> Request<'a, Self::Output>;
}

impl<'a> ToRequest<'a> for ProjectRequestBuilderWithName<'a> {
    type Output = std::collections::HashMap<String, Vec<Package>>;

    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> {
    type Output = Vec<Package>;

    fn to_request(self) -> Request<'a, Self::Output> {
        let mut buf = Vec::new();

        if let Some(search) = self.search.as_ref() {
            let s = format!("search={}", search);
            buf.push(s);
        }

        if let Some(maintainer) = self.maintainer.as_ref() {
            let s = format!("maintainer={}", maintainer);
            buf.push(s);
        }

        if let Some(category) = self.category.as_ref() {
            let s = format!("category={}", category);
            buf.push(s);
        }

        if let Some(in_repo_filter) = self.in_repo_filter.as_ref() {
            let s = format!("in_repo_filter={}", in_repo_filter);
            buf.push(s);
        }

        if let Some(not_in_repo_filter) = self.not_in_repo_filter.as_ref() {
            let s = format!("not_in_repo_filter={}", not_in_repo_filter);
            buf.push(s);
        }

        if let Some(repos_filter) = self.repos_filter.as_ref() {
            match repos_filter {
                NumberOrRange::Number(u) => {
                    let s = format!("repos={}", u);
                    buf.push(s);
                },
                NumberOrRange::Range(None, None) => {
                    // nothing, because there is no range
                },
                NumberOrRange::Range(Some(a), None) => {
                    let s = format!("repos={}-", a);
                    buf.push(s);
                },
                NumberOrRange::Range(None, Some(b)) => {
                    let s = format!("repos=-{}", b);
                    buf.push(s);
                },
                NumberOrRange::Range(Some(a), Some(b)) => {
                    let s = format!("repos={}-{}", a, b);
                    buf.push(s);
                },
            }
        }

        if let Some(families_filter) = self.families_filter.as_ref() {
            let s = format!("families={}", families_filter);
            buf.push(s);
        }

        if let Some(newest) = self.newest.as_ref() {
            if *newest {
                buf.push(String::from("newest=1"));
            } else {
                buf.push(String::from("newest=0"));
            }
        }

        if let Some(outdated) = self.outdated.as_ref() {
            if *outdated {
                buf.push(String::from("newest=1"));
            } else {
                buf.push(String::from("newest=0"));
            }
        }

        if let Some(problematic) = self.problematic.as_ref() {
            if *problematic {
                buf.push(String::from("newest=1"));
            } else {
                buf.push(String::from("newest=0"));
            }
        }

        Request {
            client: self.client,
            request_string: format!("&{}", buf.join("&")),
            _output: std::marker::PhantomData,
        }
    }
}


impl<'a> ToRequest<'a> for ProblemsForRepoRequestBuilder<'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> {
    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,
        }
    }
}