Skip to content

Commit

Permalink
Simplify API Header Authorization
Browse files Browse the repository at this point in the history
- use a convenience function to create headers
- use Bearer scheme for authorisation

Signed-off-by: Rahul Thakoor <[email protected]>
  • Loading branch information
rahul-thakoor committed May 8, 2024
1 parent 949ed1b commit 6d95fbe
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/stage1/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ struct DeviceContractInfoApiResponse {
}

pub(crate) fn get_os_versions(api_endpoint: &str, api_key: &str, device: &str) -> Result<Versions> {
let mut headers = header::HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(api_key)
.upstream_with_context("Failed to create auth header")?,
);
let headers = get_header(api_key)?;

// We currently default to non-ESR releases and use a percent-encoded template
// TODO: Improve in the future by percent-encoding in code here
Expand Down Expand Up @@ -141,19 +136,23 @@ pub(crate) fn get_os_versions(api_endpoint: &str, api_key: &str, device: &str) -
}
}

pub(crate) fn get_os_image(
api_endpoint: &str,
api_key: &str,
device: &str,
version: &str,
) -> Result<Box<dyn Read>> {
fn get_header(api_key: &str) -> Result<header::HeaderMap> {
let mut headers = header::HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(api_key)
header::HeaderValue::from_str(format!("Bearer {api_key}").as_str())
.upstream_with_context("Failed to create auth header")?,
);
Ok(headers)
}

pub(crate) fn get_os_image(
api_endpoint: &str,
api_key: &str,
device: &str,
version: &str,
) -> Result<Box<dyn Read>> {
let headers = get_header(api_key)?;
let request_url = format!("{}{}", api_endpoint, OS_IMG_URL);

let post_data = if is_device_image_flasher(api_endpoint, api_key, device)? {
Expand Down Expand Up @@ -199,12 +198,7 @@ pub(crate) fn patch_device_type(
dt_slug: &str,
uuid: &str,
) -> Result<()> {
let mut headers = header::HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(format!("Bearer {api_key}").as_str())
.upstream_with_context("Failed to create auth header")?,
);
let headers = get_header(api_key)?;

// Before we can patch the deviceType, we need to get the deviceId corresponding to the slug
let dt_id_request_url = get_device_type_info_url(api_endpoint, "id", dt_slug);
Expand Down Expand Up @@ -290,12 +284,8 @@ pub(crate) fn patch_device_type(
}

fn is_device_image_flasher(api_endpoint: &str, api_key: &str, device: &str) -> Result<bool> {
let mut headers = header::HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_str(format!("Bearer {api_key}").as_str())
.upstream_with_context("Failed to create auth header")?,
);
let headers = get_header(api_key)?;

let dt_contract_request_url = get_device_type_info_url(api_endpoint, "contract", device);
let res = Client::builder()
.default_headers(headers.clone())
Expand Down

0 comments on commit 6d95fbe

Please sign in to comment.