Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 22 additions & 35 deletions src/apis/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ pub struct SourceApi {
/// Cache for user permissions
permissions_cache: Arc<Cache<String, Vec<RepositoryPermission>>>,

/// Optional proxy URL for requests
proxy_url: Option<String>,
// API Client
client: reqwest::Client,
}

#[async_trait]
Expand Down Expand Up @@ -252,7 +252,6 @@ impl Api for SourceApi {
account_id: String,
user_identity: UserIdentity,
) -> Result<Account, BackendError> {
let client = self.build_req_client();
// Create headers
let mut headers = self.build_source_headers();
if user_identity.api_key.is_some() {
Expand All @@ -266,7 +265,8 @@ impl Api for SourceApi {
);
}

let response = client
let response = self
.client
.get(format!("{}/api/v1/products/{}", self.endpoint, account_id))
.headers(headers)
.send()
Expand Down Expand Up @@ -330,43 +330,33 @@ impl SourceApi {
.build(),
);

let client = {
let mut client = reqwest::Client::builder()
.user_agent(concat!("source-proxy/", env!("CARGO_PKG_VERSION")));
if let Some(proxy) = proxy_url {
client = client.proxy(reqwest::Proxy::all(proxy).unwrap());
}
client.build().unwrap()
};

SourceApi {
endpoint,
api_key,
product_cache,
data_connection_cache,
access_key_cache,
permissions_cache,
proxy_url,
client,
}
}

/// Creates a new `reqwest::Client` with the appropriate proxy settings.
///
/// # Returns
///
/// Returns a `reqwest::Client` with the appropriate proxy settings.
fn build_req_client(&self) -> reqwest::Client {
let mut client = reqwest::Client::builder();
if let Some(proxy) = &self.proxy_url {
client = client.proxy(reqwest::Proxy::all(proxy).unwrap());
}
client.build().unwrap()
}

/// Builds the headers for the Source API.
///
/// # Returns
///
/// Returns a `reqwest::header::HeaderMap` with the appropriate headers.
fn build_source_headers(&self) -> reqwest::header::HeaderMap {
const CORE_REQUEST_HEADERS: &[(&str, &str)] = &[
("accept", "application/json"),
(
"user-agent",
concat!("source-proxy/", env!("CARGO_PKG_VERSION")),
),
];
const CORE_REQUEST_HEADERS: &[(&str, &str)] = &[("accept", "application/json")];
CORE_REQUEST_HEADERS
.iter()
.map(|(name, value)| {
Expand Down Expand Up @@ -425,9 +415,8 @@ impl SourceApi {
"{}/api/v1/products/{}/{}",
self.endpoint, account_id, repository_id
);
let client = self.build_req_client();
let headers = self.build_source_headers();
let response = client.get(url).headers(headers).send().await?;
let response = self.client.get(url).headers(headers).send().await?;
let repository =
process_json_response::<SourceProduct>(response, BackendError::RepositoryNotFound)
.await?;
Expand All @@ -443,14 +432,14 @@ impl SourceApi {
&self,
data_connection_id: &str,
) -> Result<DataConnection, BackendError> {
let client = self.build_req_client();
let mut headers = self.build_source_headers();
headers.insert(
reqwest::header::AUTHORIZATION,
reqwest::header::HeaderValue::from_str(&self.api_key).unwrap(),
);

let response = client
let response = self
.client
.get(format!(
"{}/api/v1/data-connections/{}",
self.endpoint, data_connection_id
Expand Down Expand Up @@ -508,15 +497,14 @@ impl SourceApi {
}

async fn fetch_api_key(&self, access_key_id: String) -> Result<APIKey, BackendError> {
let client = self.build_req_client();

// Create headers
let mut headers = self.build_source_headers();
headers.insert(
reqwest::header::AUTHORIZATION,
reqwest::header::HeaderValue::from_str(&self.api_key).unwrap(),
);
let response = client
let response = self
.client
.get(format!(
"{}/api/v1/api-keys/{access_key_id}/auth",
self.endpoint
Expand Down Expand Up @@ -588,8 +576,6 @@ impl SourceApi {
account_id: &str,
repository_id: &str,
) -> Result<Vec<RepositoryPermission>, BackendError> {
let client = self.build_req_client();

// Create headers
let mut headers = self.build_source_headers();
if user_identity.api_key.is_some() {
Expand All @@ -603,7 +589,8 @@ impl SourceApi {
);
}

let response = client
let response = self
.client
.get(format!(
"{}/api/v1/products/{account_id}/{repository_id}/permissions",
self.endpoint
Expand Down