Skip to content

Commit

Permalink
Merge pull request #54 from SupernaviX/custom-headers
Browse files Browse the repository at this point in the history
feat: allow callers to pass header values
  • Loading branch information
vladimirvolek authored Dec 7, 2024
2 parents bc85757 + be271e2 commit a19d4c5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct BlockfrostAPI {

impl BlockfrostAPI {
pub fn new(project_id: &str, settings: BlockFrostSettings) -> Self {
let client = create_client_with_project_id(project_id);
let client = create_client_with_project_id(project_id, &settings.headers);
let base_url = Url::get_base_url_from_project_id(project_id);

Self {
Expand All @@ -34,7 +34,7 @@ impl BlockfrostAPI {
let base_url = Url::get_base_url_from_project_id(project_id);

client_builder
.default_headers(build_header_map(project_id))
.default_headers(build_header_map(project_id, &settings.headers))
.build()
.map(|client| Self {
settings,
Expand Down
4 changes: 2 additions & 2 deletions src/ipfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl BlockfrostIPFS {
/// [`HeaderValue`]: reqwest::header::HeaderValue
/// [`HeaderValue::from_str`]: reqwest::header::HeaderValue::from_str
pub fn new(project_id: &str, settings: IpfsSettings) -> Self {
let client = create_client_with_project_id(project_id);
let client = create_client_with_project_id(project_id, &settings.headers);

Self {
client,
Expand Down Expand Up @@ -62,7 +62,7 @@ impl BlockfrostIPFS {
project_id: impl AsRef<str>, settings: IpfsSettings, client_builder: ClientBuilder,
) -> reqwest::Result<Self> {
client_builder
.default_headers(build_header_map(project_id.as_ref()))
.default_headers(build_header_map(project_id.as_ref(), &settings.headers))
.build()
.map(|client| Self {
settings,
Expand Down
8 changes: 7 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use std::time::Duration;
use std::{collections::HashMap, time::Duration};

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BlockFrostSettings {
pub retry_settings: RetrySettings,
pub headers: HashMap<String, String>,
}

impl BlockFrostSettings {
pub fn new() -> Self {
Self {
retry_settings: RetrySettings::default(),
headers: HashMap::new(),
}
}
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct IpfsSettings {
pub retry_settings: RetrySettings,
pub headers: HashMap<String, String>,
}

impl IpfsSettings {
Expand All @@ -29,6 +34,7 @@ impl IpfsSettings {
pub fn new() -> Self {
Self {
retry_settings: RetrySettings::default(),
headers: HashMap::new(),
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::HashMap, str::FromStr};

use crate::USER_AGENT;
use reqwest::{
header::{HeaderMap, HeaderValue},
header::{HeaderMap, HeaderName, HeaderValue},
Client,
};
use serde_json::{from_str as json_from, Value as JsonValue};
Expand All @@ -11,16 +13,18 @@ pub(crate) fn try_formatting_json(text: &str) -> serde_json::Result<String> {
serde_json::to_string_pretty(&json)
}

pub(crate) fn create_client_with_project_id(project_id: impl AsRef<str>) -> Client {
let header_map = build_header_map(project_id.as_ref());
pub(crate) fn create_client_with_project_id(
project_id: impl AsRef<str>, headers: &HashMap<String, String>,
) -> Client {
let header_map = build_header_map(project_id.as_ref(), headers);
// Safety: This unwrap is guaranteed to never fail if we only call .default_headers()
Client::builder()
.default_headers(header_map)
.build()
.unwrap()
}

pub(crate) fn build_header_map(project_id: &str) -> HeaderMap {
pub(crate) fn build_header_map(project_id: &str, headers: &HashMap<String, String>) -> HeaderMap {
let mut header_map = HeaderMap::new();
let mut project_id = HeaderValue::from_str(project_id).unwrap_or_else(|_| {
panic!(
Expand All @@ -33,5 +37,11 @@ pub(crate) fn build_header_map(project_id: &str) -> HeaderMap {

header_map.insert("project_id", project_id);
header_map.insert("User-Agent", user_agent);
for (key, val) in headers.iter() {
let (Ok(key), Ok(val)) = (HeaderName::from_str(key), HeaderValue::from_str(val)) else {
panic!("Header \"{key}: {val}\" is invalid")
};
header_map.insert(key, val);
}
header_map
}

0 comments on commit a19d4c5

Please sign in to comment.