wakapi/endpoints/
projects.rs1use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8use crate::{WakapiClient, WakapiError};
9
10use super::commit::RepositoryDetails;
11
12#[derive(Serialize, Default)]
16pub struct ProjectsParams {
17 q: Option<String>,
19 page: Option<usize>,
21}
22
23impl ProjectsParams {
24 pub fn new() -> ProjectsParams {
26 ProjectsParams {
27 q: None,
28 page: None,
29 }
30 }
31
32 pub fn q(mut self, q: &str) -> ProjectsParams {
34 self.q = Some(q.to_string());
35 self
36 }
37
38 pub fn page(mut self, page: usize) -> ProjectsParams {
41 self.page = Some(page);
42 self
43 }
44}
45
46#[derive(Deserialize, Debug)]
50pub struct Projects {
51 pub data: Vec<ProjectsData>,
53 pub total: usize,
55 pub total_pages: usize,
57 pub page: usize,
59 pub prev_page: Option<usize>,
61 pub next_page: Option<usize>,
63}
64
65#[derive(Deserialize, Debug)]
66pub struct ProjectsData {
67 pub id: String,
69 pub name: String,
71 pub repository: Option<RepositoryDetails>,
73 pub badge: Option<ProjectBadge>,
75 pub color: Option<String>,
77 pub clients: Vec<ProjectClients>,
79 pub has_public_url: bool,
81 pub human_readable_last_heartbeat_at: String,
83 pub last_heartbeat_at: DateTime<Utc>,
85 pub first_heartbeat_at: Option<String>,
87 pub url: String,
89 pub urlencoded_name: String,
91 pub created_at: DateTime<Utc>,
93}
94
95#[derive(Deserialize, Debug)]
96pub struct ProjectBadge {
97 pub color: String,
99 pub id: String,
101 pub left_text: String,
103 pub link: String,
105 pub project_id: String,
107 pub snippets: Vec<BadgeSnippet>,
109 pub title: String,
111 pub url: String,
113}
114
115#[derive(Deserialize, Debug)]
116pub struct BadgeSnippet {
117 pub content: String,
119 pub name: String,
121}
122
123#[derive(Deserialize, Debug)]
124pub struct ProjectClients {
125 pub id: String,
127 pub name: String,
129 pub rate: f64,
131 pub timeout: usize,
133}
134
135impl Projects {
136 #[cfg(feature = "blocking")]
137 pub fn fetch(client: &WakapiClient, params: ProjectsParams) -> Result<Self, WakapiError> {
139 let url = client.build_url(
140 "/api/v1/users/current/projects",
141 Some(serde_url_params::to_string(¶ms)?),
142 );
143 println!(
145 "url: {}\nbody: {}",
146 url,
147 reqwest::blocking::Client::new()
148 .get(&url)
149 .header("Authorization", client.get_auth_header())
150 .send()?
151 .text()?
152 );
153
154 let response = reqwest::blocking::Client::new()
155 .get(&url)
156 .header("Authorization", client.get_auth_header())
157 .send()?;
158 if response.status().is_success() {
159 let body = response.json::<Projects>()?;
160 Ok(body)
161 } else {
162 let error = response.json::<crate::error::ErrorMessage>()?;
163 Err(WakapiError::ResponseError(error))
164 }
165 }
166
167 #[cfg(not(feature = "blocking"))]
168 pub async fn fetch(client: &WakapiClient, params: ProjectsParams) -> Result<Self, WakapiError> {
170 let url = client.build_url(
171 "/api/v1/users/current/projects",
172 Some(serde_url_params::to_string(¶ms)?),
173 );
174 println!(
176 "url: {}\nbody: {}",
177 url,
178 reqwest::Client::new()
179 .get(&url)
180 .header("Authorization", client.get_auth_header())
181 .send()
182 .await?
183 .text()
184 .await?
185 );
186
187 let response = reqwest::Client::new()
188 .get(&url)
189 .header("Authorization", client.get_auth_header())
190 .send()
191 .await?;
192 if response.status().is_success() {
193 let body = response.json::<Projects>().await?;
194 Ok(body)
195 } else {
196 let error = response.json::<crate::error::ErrorMessage>().await?;
197 Err(WakapiError::ResponseError(error))
198 }
199 }
200}