summaryrefslogtreecommitdiffstats
path: root/librepology/src/v1/api.rs
blob: a0902490d47811518d4d05c1fcbc5ebc5f1803a7 (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
use crate::endpoint::EndpointUrl;
use reqwest::Client;

pub struct ApiClient {
    endpoint_url: &'static str,
}

impl ApiClient {
    pub fn new<EP: EndpointUrl>() -> Self {
        ApiClient {
            endpoint_url: EP::endpoint_url(),
        }
    }

    pub fn projects<'a>(&'a self) -> ProjectRequestBuilder<'a> {
        ProjectRequestBuilder(self)
    }

}

pub struct ProjectRequestBuilder<'a>(&'a ApiClient);

impl<'a> ProjectRequestBuilder<'a> {
    pub fn with_name(self, name: String) -> ProjectRequestBuilderWithName<'a> {
        ProjectRequestBuilderWithName(self.0, name)
    }

    pub fn filtered(self) -> ProjectRequestFilteredBuilder<'a> {
        ProjectRequestFilteredBuilder {
            client: self.0,
            search: None,
            category: None,
            in_repo_filter: None,
            not_in_repo_filter: None,
            repos_filter: None,
            families_filter: None,
            repos_newest: None,
            families_newest: None,
            newest: None,
            outdated: None,
            problematic: None,
        }
    }
}

pub trait ToRequest<'a> {
    fn to_request(self) -> Request<'a>;
}

pub struct Request<'a> {
    client: &'a ApiClient,
    request_string: String
}

pub struct ProjectRequestBuilderWithName<'a>(&'a ApiClient, String);

impl<'a> ToRequest<'a> for ProjectRequestBuilderWithName<'a> {
    fn to_request(self) -> Request<'a> {
        Request {
            client: self.0,
            request_string: format!("projects/{}", self.1),
        }
    }
}

pub struct ProjectRequestFilteredBuilder<'a> {
    client: &'a ApiClient,

    // From the API documentation

    search: Option<String>,
    category: Option<String>,
    in_repo_filter: Option<String>,
    not_in_repo_filter: Option<String>,
    repos_filter: Option<NumberOrRange>,
    families_filter: Option<usize>,
    repos_newest: Option<bool>,
    families_newest: Option<bool>,
    newest: Option<bool>,
    outdated: Option<bool>,
    problematic: Option<bool>,
}

impl<'a> ProjectRequestFilteredBuilder<'a> {
    pub fn with_search(mut self, opt: Option<String>) -> Self {
        self.search = opt;
        self
    }

    pub fn with_category(mut self, opt: Option<String>) -> Self {
        self.category = opt;
        self
    }

    pub fn with_in_repo_filter(mut self, opt: Option<String>) -> Self {
        self.in_repo_filter = opt;
        self
    }

    pub fn with_not_in_repo_filter(mut self, opt: Option<String>) -> Self {
        self.not_in_repo_filter = opt;
        self
    }

    pub fn with_repos_filter(mut self, opt: Option<NumberOrRange>) -> Self {
        self.repos_filter = opt;
        self
    }

    pub fn with_families_filter(mut self, opt: Option<usize>) -> Self {
        self.families_filter = opt;
        self
    }

    pub fn with_repos_newest(mut self, opt: Option<bool>) -> Self {
        self.repos_newest = opt;
        self
    }

    pub fn with_families_newest(mut self, opt: Option<bool>) -> Self {
        self.families_newest = opt;
        self
    }

    pub fn with_newest(mut self, opt: Option<bool>) -> Self {
        self.newest = opt;
        self
    }

    pub fn with_outdated(mut self, opt: Option<bool>) -> Self {
        self.outdated = opt;
        self
    }

    pub fn with_problematic(mut self, opt: Option<bool>) -> Self {
        self.problematic = opt;
        self
    }

}

pub enum NumberOrRange {
    Number(usize),
    Range(Option<usize>, Option<usize>),
}