Skip to content

feat: Add support for proxy config #229

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
25 changes: 22 additions & 3 deletions library/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,39 @@ impl Default for NetworkHooks {
}
}

fn create_client_with_proxy() -> anyhow::Result<reqwest::blocking::Client> {
let config = read_config()?;
let mut client_builder = reqwest::blocking::Client::builder();

if let Some(proxy_url) = config.proxy_url {
let proxy = reqwest::Proxy::http(&proxy_url)?;
client_builder = client_builder.proxy(proxy);
}

let client = client_builder.build()?;
Ok(client)
}

fn read_config() -> anyhow::Result<YamlConfig> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, looks like the request callbacks would now need to be config-specifc. 🤔

let config_str = std::fs::read_to_string("shorebird.yaml")?;
let config: YamlConfig = serde_yaml::from_str(&config_str)?;
Ok(config)
}

pub fn patch_check_request_default(
url: &str,
request: PatchCheckRequest,
) -> anyhow::Result<PatchCheckResponse> {
shorebird_info!("Sending patch check request: {:?}", request);
let client = reqwest::blocking::Client::new();
let client = create_client_with_proxy()?;
let result = client.post(url).json(&request).send();
let response = handle_network_result(result)?.json()?;
shorebird_debug!("Patch check response: {:?}", response);
Ok(response)
}

pub fn download_file_default(url: &str) -> anyhow::Result<Vec<u8>> {
let client = reqwest::blocking::Client::new();
let client = create_client_with_proxy()?;
let result = client.get(url).send();
let response = handle_network_result(result)?;
let bytes = response.bytes()?;
Expand All @@ -78,7 +97,7 @@ pub fn download_file_default(url: &str) -> anyhow::Result<Vec<u8>> {
}

pub fn report_event_default(url: &str, request: CreatePatchEventRequest) -> anyhow::Result<()> {
let client = reqwest::blocking::Client::new();
let client = create_client_with_proxy()?;
let result = client.post(url).json(&request).send();
handle_network_result(result)?;
Ok(())
Expand Down
2 changes: 2 additions & 0 deletions library/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct YamlConfig {
pub auto_update: Option<bool>,
/// Base64-encoded public key for verifying patch hash signatures.
pub patch_public_key: Option<String>,
/// Proxy URL. Remains unused if not set by user.
pub proxy_url: Option<String>,
}

impl YamlConfig {
Expand Down
Loading