Skip to content

Commit be271e2

Browse files
committed
feat: allow callers to pass header values
1 parent bc85757 commit be271e2

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/api/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct BlockfrostAPI {
1818

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

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

3636
client_builder
37-
.default_headers(build_header_map(project_id))
37+
.default_headers(build_header_map(project_id, &settings.headers))
3838
.build()
3939
.map(|client| Self {
4040
settings,

src/ipfs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl BlockfrostIPFS {
3232
/// [`HeaderValue`]: reqwest::header::HeaderValue
3333
/// [`HeaderValue::from_str`]: reqwest::header::HeaderValue::from_str
3434
pub fn new(project_id: &str, settings: IpfsSettings) -> Self {
35-
let client = create_client_with_project_id(project_id);
35+
let client = create_client_with_project_id(project_id, &settings.headers);
3636

3737
Self {
3838
client,
@@ -62,7 +62,7 @@ impl BlockfrostIPFS {
6262
project_id: impl AsRef<str>, settings: IpfsSettings, client_builder: ClientBuilder,
6363
) -> reqwest::Result<Self> {
6464
client_builder
65-
.default_headers(build_header_map(project_id.as_ref()))
65+
.default_headers(build_header_map(project_id.as_ref(), &settings.headers))
6666
.build()
6767
.map(|client| Self {
6868
settings,

src/settings.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
use std::time::Duration;
1+
use std::{collections::HashMap, time::Duration};
22

33
#[derive(Debug, Clone)]
4+
#[non_exhaustive]
45
pub struct BlockFrostSettings {
56
pub retry_settings: RetrySettings,
7+
pub headers: HashMap<String, String>,
68
}
79

810
impl BlockFrostSettings {
911
pub fn new() -> Self {
1012
Self {
1113
retry_settings: RetrySettings::default(),
14+
headers: HashMap::new(),
1215
}
1316
}
1417
}
1518

1619
#[derive(Debug, Clone)]
20+
#[non_exhaustive]
1721
pub struct IpfsSettings {
1822
pub retry_settings: RetrySettings,
23+
pub headers: HashMap<String, String>,
1924
}
2025

2126
impl IpfsSettings {
@@ -29,6 +34,7 @@ impl IpfsSettings {
2934
pub fn new() -> Self {
3035
Self {
3136
retry_settings: RetrySettings::default(),
37+
headers: HashMap::new(),
3238
}
3339
}
3440
}

src/utils.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::{collections::HashMap, str::FromStr};
2+
13
use crate::USER_AGENT;
24
use reqwest::{
3-
header::{HeaderMap, HeaderValue},
5+
header::{HeaderMap, HeaderName, HeaderValue},
46
Client,
57
};
68
use serde_json::{from_str as json_from, Value as JsonValue};
@@ -11,16 +13,18 @@ pub(crate) fn try_formatting_json(text: &str) -> serde_json::Result<String> {
1113
serde_json::to_string_pretty(&json)
1214
}
1315

14-
pub(crate) fn create_client_with_project_id(project_id: impl AsRef<str>) -> Client {
15-
let header_map = build_header_map(project_id.as_ref());
16+
pub(crate) fn create_client_with_project_id(
17+
project_id: impl AsRef<str>, headers: &HashMap<String, String>,
18+
) -> Client {
19+
let header_map = build_header_map(project_id.as_ref(), headers);
1620
// Safety: This unwrap is guaranteed to never fail if we only call .default_headers()
1721
Client::builder()
1822
.default_headers(header_map)
1923
.build()
2024
.unwrap()
2125
}
2226

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

3438
header_map.insert("project_id", project_id);
3539
header_map.insert("User-Agent", user_agent);
40+
for (key, val) in headers.iter() {
41+
let (Ok(key), Ok(val)) = (HeaderName::from_str(key), HeaderValue::from_str(val)) else {
42+
panic!("Header \"{key}: {val}\" is invalid")
43+
};
44+
header_map.insert(key, val);
45+
}
3646
header_map
3747
}

0 commit comments

Comments
 (0)