-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move api-client out of the cli #308
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
use std::path::Path; | ||
|
||
use crate::call_api::CallApi; | ||
use crate::message; | ||
use anyhow::Context; | ||
use api; | ||
use call_api::CallApi; | ||
use constants::{DEFAULT_ORIGIN, TRUNK_PUBLIC_API_ADDRESS_ENV}; | ||
use http::{header::HeaderMap, HeaderValue}; | ||
use reqwest::{header, Client, Response, StatusCode}; | ||
use tokio::fs; | ||
|
||
mod call_api; | ||
|
||
pub struct ApiClient { | ||
host: String, | ||
s3_client: Client, | ||
|
@@ -26,7 +24,7 @@ impl ApiClient { | |
if api_token.trim().is_empty() { | ||
return Err(anyhow::anyhow!("Trunk API token is required.")); | ||
} | ||
let api_token_header_value = HeaderValue::from_str(&api_token) | ||
let api_token_header_value = HeaderValue::from_str(api_token) | ||
.map_err(|_| anyhow::Error::msg("Trunk API token is not ASCII"))?; | ||
|
||
let host = std::env::var(TRUNK_PUBLIC_API_ADDRESS_ENV) | ||
|
@@ -69,7 +67,10 @@ impl ApiClient { | |
}) | ||
} | ||
|
||
pub async fn create_trunk_repo(&self, request: &api::CreateRepoRequest) -> anyhow::Result<()> { | ||
pub async fn create_repo( | ||
&self, | ||
request: &message::CreateRepoRequest, | ||
) -> anyhow::Result<message::CreateRepoResponse> { | ||
CallApi { | ||
action: || async { | ||
let response = self | ||
|
@@ -83,8 +84,13 @@ impl ApiClient { | |
&response, | ||
CheckUnauthorized::Check, | ||
CheckNotFound::DoNotCheck, | ||
|_| format!("Failed to create repo."), | ||
) | ||
|_| "Failed to create repo.".to_string(), | ||
)?; | ||
|
||
response | ||
.json::<message::CreateRepoResponse>() | ||
.await | ||
.context("Failed to get response body as json.") | ||
}, | ||
log_progress_message: |time_elapsed, _| { | ||
format!("Communicating with Trunk services is taking longer than expected. It has taken {} seconds so far.", time_elapsed.as_secs()) | ||
|
@@ -97,10 +103,10 @@ impl ApiClient { | |
.await | ||
} | ||
|
||
pub async fn create_bundle_upload_intent( | ||
pub async fn create_bundle_upload( | ||
&self, | ||
request: &api::CreateBundleUploadRequest, | ||
) -> anyhow::Result<api::CreateBundleUploadResponse> { | ||
request: &message::CreateBundleUploadRequest, | ||
) -> anyhow::Result<message::CreateBundleUploadResponse> { | ||
CallApi { | ||
action: || async { | ||
let response = self | ||
|
@@ -118,7 +124,7 @@ impl ApiClient { | |
)?; | ||
|
||
response | ||
.json::<api::CreateBundleUploadResponse>() | ||
.json::<message::CreateBundleUploadResponse>() | ||
.await | ||
.context("Failed to get response body as json.") | ||
}, | ||
|
@@ -135,8 +141,8 @@ impl ApiClient { | |
|
||
pub async fn get_quarantining_config( | ||
&self, | ||
request: &api::GetQuarantineBulkTestStatusRequest, | ||
) -> anyhow::Result<api::QuarantineConfig> { | ||
request: &message::GetQuarantineConfigRequest, | ||
) -> anyhow::Result<message::GetQuarantineConfigResponse> { | ||
CallApi { | ||
action: || async { | ||
let response = self | ||
|
@@ -160,7 +166,7 @@ impl ApiClient { | |
)?; | ||
|
||
response | ||
.json::<api::QuarantineConfig>() | ||
.json::<message::GetQuarantineConfigResponse>() | ||
.await | ||
.context("Failed to get response body as json.") | ||
}, | ||
|
@@ -211,10 +217,10 @@ impl ApiClient { | |
.await | ||
} | ||
|
||
pub async fn update_bundle_upload_status( | ||
pub async fn update_bundle_upload( | ||
&self, | ||
request: &api::UpdateBundleUploadRequest, | ||
) -> anyhow::Result<()> { | ||
request: &message::UpdateBundleUploadRequest, | ||
) -> anyhow::Result<message::UpdateBundleUploadResponse> { | ||
CallApi { | ||
action: || async { | ||
let response = self | ||
|
@@ -234,7 +240,11 @@ impl ApiClient { | |
request.upload_status | ||
) | ||
}, | ||
) | ||
)?; | ||
response | ||
.json::<message::UpdateBundleUploadResponse>() | ||
.await | ||
.context("Failed to get response body as json.") | ||
}, | ||
log_progress_message: |time_elapsed, _| { | ||
format!("Communicating with Trunk services is taking longer than expected. It has taken {} seconds so far.", time_elapsed.as_secs()) | ||
|
@@ -292,6 +302,7 @@ fn status_code_help<T: FnMut(&Response) -> String>( | |
mod tests { | ||
use std::time::Duration; | ||
|
||
use crate::message; | ||
use axum::{http::StatusCode, response::Response}; | ||
use tempfile::NamedTempFile; | ||
use test_utils::{mock_logger, mock_sentry, mock_server::MockServerBuilder}; | ||
|
@@ -329,8 +340,8 @@ mod tests { | |
.unwrap() | ||
.iter() | ||
.filter(|(_, message)| message.starts_with("Uploading bundle to S3")) | ||
.cloned() | ||
.take(2) | ||
.cloned() | ||
Comment on lines
-332
to
+344
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was also changed by clippy. |
||
.collect::<Vec<_>>(); | ||
assert_eq!(first_two_slow_s3_upload_logs, vec![ | ||
(log::Level::Info, String::from("Uploading bundle to S3 is taking longer than expected. It has taken 2 seconds so far.")), | ||
|
@@ -368,7 +379,7 @@ mod tests { | |
api_client.host.clone_from(&state.host); | ||
|
||
assert!(api_client | ||
.get_quarantining_config(&api::GetQuarantineBulkTestStatusRequest { | ||
.get_quarantining_config(&message::GetQuarantineConfigRequest { | ||
repo: context::repo::RepoUrlParts { | ||
host: String::from("host"), | ||
owner: String::from("owner"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,3 @@ | ||
use bundle::Test; | ||
use context::repo::RepoUrlParts; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] | ||
pub struct CreateBundleUploadRequest { | ||
pub repo: RepoUrlParts, | ||
#[serde(rename = "orgUrlSlug")] | ||
pub org_url_slug: String, | ||
#[serde(rename = "clientVersion")] | ||
pub client_version: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize)] | ||
pub struct CreateBundleUploadResponse { | ||
pub id: String, | ||
#[serde(rename = "idV2")] | ||
pub id_v2: String, | ||
pub url: String, | ||
pub key: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] | ||
pub enum BundleUploadStatus { | ||
#[serde(rename = "PENDING")] | ||
Pending, | ||
#[serde(rename = "UPLOAD_COMPLETE")] | ||
UploadComplete, | ||
#[serde(rename = "UPLOAD_FAILED")] | ||
UploadFailed, | ||
#[serde(rename = "DRY_RUN")] | ||
DryRun, | ||
} | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] | ||
pub struct UpdateBundleUploadRequest { | ||
pub id: String, | ||
#[serde(rename = "uploadStatus")] | ||
pub upload_status: BundleUploadStatus, | ||
} | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] | ||
pub struct CreateRepoRequest { | ||
pub repo: RepoUrlParts, | ||
#[serde(rename = "orgUrlSlug")] | ||
pub org_url_slug: String, | ||
#[serde(rename = "remoteUrls")] | ||
pub remote_urls: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] | ||
pub struct GetQuarantineBulkTestStatusRequest { | ||
pub repo: RepoUrlParts, | ||
#[serde(rename = "orgUrlSlug")] | ||
pub org_url_slug: String, | ||
#[serde(rename = "testIdentifiers")] | ||
pub test_identifiers: Vec<Test>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Clone, Deserialize, Default)] | ||
pub struct QuarantineConfig { | ||
#[serde(rename = "isDisabled")] | ||
pub is_disabled: bool, | ||
#[serde(rename = "testIds")] | ||
pub quarantined_tests: Vec<String>, | ||
} | ||
mod call_api; | ||
pub mod client; | ||
pub mod message; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clippy changed this