1use crate::config::ConfigProperties;
2use crate::http::Headers;
3use std::sync::Arc;
4pub mod cicd;
5pub mod container_registry;
6pub mod gist;
7pub mod merge_request;
8pub mod project;
9pub mod release;
10pub mod trending;
11pub mod user;
12
13#[derive(Clone)]
16pub struct Gitlab<R> {
17 api_token: String,
18 domain: String,
19 path: String,
20 projects_base_url: String,
21 runner: Arc<R>,
22 base_project_url: String,
23 base_current_user_url: String,
24 base_users_url: String,
25 merge_requests_url: String,
26 base_runner_url: String,
27}
28
29impl<R> Gitlab<R> {
30 pub fn new(
31 config: Arc<dyn ConfigProperties>,
32 domain: &str,
33 path: &str,
34 runner: Arc<R>,
35 ) -> Self {
36 let api_token = config.api_token().to_string();
37 let domain = domain.to_string();
38 let encoded_path = encode_path(path);
39 let api_path = "api/v4";
40 let protocol = "https";
41 let base_api_path = format!("{protocol}://{domain}/{api_path}");
42 let base_user_url = format!("{base_api_path}/user");
43 let base_users_url = format!("{base_api_path}/users");
44 let base_runner_url = format!("{base_api_path}/runners");
45 let merge_requests_url = format!("{base_api_path}/merge_requests");
46 let base_project_url = format!("{base_api_path}/projects");
47 let projects_base_url = format!("{base_project_url}/{encoded_path}");
48 Gitlab {
49 api_token,
50 domain,
51 path: path.to_string(),
52 projects_base_url,
53 runner,
54 base_project_url,
55 base_current_user_url: base_user_url,
56 merge_requests_url,
57 base_runner_url,
58 base_users_url,
59 }
60 }
61
62 fn api_token(&self) -> &str {
63 &self.api_token
64 }
65
66 fn rest_api_basepath(&self) -> &str {
67 &self.projects_base_url
68 }
69
70 fn headers(&self) -> Headers {
71 let mut headers = Headers::new();
72 headers.set("PRIVATE-TOKEN", self.api_token());
73 headers
74 }
75}
76
77fn encode_path(path: &str) -> String {
78 path.replace('/', "%2F")
79}