-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathapi_client.rs
More file actions
218 lines (199 loc) · 6.37 KB
/
api_client.rs
File metadata and controls
218 lines (199 loc) · 6.37 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::fmt::Display;
use crate::prelude::*;
use crate::{app::Cli, config::CodSpeedConfig};
use console::style;
use gql_client::{Client as GQLClient, ClientConfig};
use nestify::nest;
use serde::{Deserialize, Serialize};
pub struct CodSpeedAPIClient {
gql_client: GQLClient,
unauthenticated_gql_client: GQLClient,
}
impl TryFrom<(&Cli, &CodSpeedConfig)> for CodSpeedAPIClient {
type Error = Error;
fn try_from((args, codspeed_config): (&Cli, &CodSpeedConfig)) -> Result<Self> {
Ok(Self {
gql_client: build_gql_api_client(codspeed_config, args.api_url.clone(), true),
unauthenticated_gql_client: build_gql_api_client(
codspeed_config,
args.api_url.clone(),
false,
),
})
}
}
fn build_gql_api_client(
codspeed_config: &CodSpeedConfig,
api_url: String,
with_auth: bool,
) -> GQLClient {
let headers = if with_auth && codspeed_config.auth.token.is_some() {
let mut headers = std::collections::HashMap::new();
headers.insert(
"Authorization".to_string(),
codspeed_config.auth.token.clone().unwrap(),
);
headers
} else {
Default::default()
};
GQLClient::new_with_config(ClientConfig {
endpoint: api_url,
timeout: Some(10),
headers: Some(headers),
proxy: None,
})
}
nest! {
#[derive(Debug, Deserialize, Serialize)]*
#[serde(rename_all = "camelCase")]*
struct CreateLoginSessionData {
create_login_session: pub struct CreateLoginSessionPayload {
pub callback_url: String,
pub session_id: String,
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ConsumeLoginSessionVars {
session_id: String,
}
nest! {
#[derive(Debug, Deserialize, Serialize)]*
#[serde(rename_all = "camelCase")]*
struct ConsumeLoginSessionData {
consume_login_session: pub struct ConsumeLoginSessionPayload {
pub token: Option<String>
}
}
}
#[derive(Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FetchLocalRunReportVars {
pub owner: String,
pub name: String,
pub run_id: String,
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub enum ReportConclusion {
AcknowledgedFailure,
Failure,
MissingBaseRun,
Success,
}
impl Display for ReportConclusion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReportConclusion::AcknowledgedFailure => {
write!(f, "{}", style("Acknowledged Failure").yellow().bold())
}
ReportConclusion::Failure => write!(f, "{}", style("Failure").red().bold()),
ReportConclusion::MissingBaseRun => {
write!(f, "{}", style("Missing Base Run").yellow().bold())
}
ReportConclusion::Success => write!(f, "{}", style("Success").green().bold()),
}
}
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchLocalRunReportRun {
pub id: String,
pub status: RunStatus,
pub url: String,
pub head_reports: Vec<FetchLocalRunReportHeadReport>,
pub results: Vec<FetchLocalRunBenchmarkResult>,
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RunStatus {
Completed,
Failure,
Pending,
Processing,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FetchLocalRunReportHeadReport {
pub id: String,
pub impact: Option<f64>,
pub conclusion: ReportConclusion,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct FetchLocalRunBenchmarkResult {
pub time: f64,
pub benchmark: FetchLocalRunBenchmark,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct FetchLocalRunBenchmark {
pub name: String,
}
nest! {
#[derive(Debug, Deserialize, Serialize)]*
#[serde(rename_all = "camelCase")]*
struct FetchLocalRunReportData {
repository: pub struct FetchLocalRunReportRepository {
settings: struct FetchLocalRunReportSettings {
allowed_regression: f64,
},
run: FetchLocalRunReportRun,
}
}
}
pub struct FetchLocalRunReportResponse {
pub allowed_regression: f64,
pub run: FetchLocalRunReportRun,
}
impl CodSpeedAPIClient {
pub async fn create_login_session(&self) -> Result<CreateLoginSessionPayload> {
let response = self
.unauthenticated_gql_client
.query_unwrap::<CreateLoginSessionData>(include_str!("queries/CreateLoginSession.gql"))
.await;
match response {
Ok(response) => Ok(response.create_login_session),
Err(err) => bail!("Failed to create login session: {err}"),
}
}
pub async fn consume_login_session(
&self,
session_id: &str,
) -> Result<ConsumeLoginSessionPayload> {
let response = self
.unauthenticated_gql_client
.query_with_vars_unwrap::<ConsumeLoginSessionData, ConsumeLoginSessionVars>(
include_str!("queries/ConsumeLoginSession.gql"),
ConsumeLoginSessionVars {
session_id: session_id.to_string(),
},
)
.await;
match response {
Ok(response) => Ok(response.consume_login_session),
Err(err) => bail!("Failed to use login session: {err}"),
}
}
pub async fn fetch_local_run_report(
&self,
vars: FetchLocalRunReportVars,
) -> Result<FetchLocalRunReportResponse> {
let response = self
.gql_client
.query_with_vars_unwrap::<FetchLocalRunReportData, FetchLocalRunReportVars>(
include_str!("queries/FetchLocalRunReport.gql"),
vars.clone(),
)
.await;
match response {
Ok(response) => Ok(FetchLocalRunReportResponse {
allowed_regression: response.repository.settings.allowed_regression,
run: response.repository.run,
}),
Err(err) if err.contains_error_code("UNAUTHENTICATED") => {
bail!("Your session has expired, please login again using `codspeed auth login`")
}
Err(err) => bail!("Failed to fetch local run report: {err}"),
}
}
}