Skip to content

Commit a13053b

Browse files
committed
feat(client-cli): add GitHub token support for authenticated API calls in snapshot converter command
1 parent 66b2c87 commit a13053b

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

mithril-client-cli/src/commands/tools/snapshot_converter.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ pub struct SnapshotConverterCommand {
9191
/// If set, the converted snapshot replaces the current ledger state in the `db_directory`.
9292
#[clap(long)]
9393
commit: bool,
94+
95+
/// GitHub token for authenticated API calls.
96+
#[clap(long, env = "GITHUB_TOKEN")]
97+
github_token: Option<String>,
9498
}
9599

96100
impl SnapshotConverterCommand {
@@ -113,7 +117,7 @@ impl SnapshotConverterCommand {
113117
)
114118
})?;
115119
let archive_path = Self::download_cardano_node_distribution(
116-
ReqwestGitHubApiClient::new()?,
120+
ReqwestGitHubApiClient::new(self.github_token.clone())?,
117121
ReqwestHttpDownloader::new()?,
118122
&self.cardano_node_version,
119123
&distribution_dir,

mithril-client-cli/src/utils/github_release_retriever/reqwest.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,31 @@ use super::{GitHubRelease, GitHubReleaseRetriever};
99

1010
pub struct ReqwestGitHubApiClient {
1111
client: Client,
12+
github_token: Option<String>,
1213
}
1314

1415
impl ReqwestGitHubApiClient {
15-
pub fn new() -> MithrilResult<Self> {
16+
pub fn new(github_token: Option<String>) -> MithrilResult<Self> {
1617
let client = Client::builder()
1718
.user_agent("mithril-client")
1819
.build()
1920
.context("Failed to build Reqwest GitHub API client")?;
2021

21-
Ok(Self { client })
22+
Ok(Self {
23+
client,
24+
github_token,
25+
})
2226
}
2327

2428
async fn download<U: IntoUrl, T: DeserializeOwned>(&self, source_url: U) -> MithrilResult<T> {
2529
let url = source_url
2630
.into_url()
2731
.with_context(|| "Given `source_url` is not a valid Url")?;
28-
let response = self
29-
.client
30-
.get(url.clone())
32+
let mut request = self.client.get(url.clone());
33+
if let Some(token) = &self.github_token {
34+
request = request.bearer_auth(token);
35+
}
36+
let response = request
3137
.send()
3238
.await
3339
.with_context(|| format!("Failed to send request to GitHub API: {}", url))?;
@@ -122,7 +128,7 @@ mod tests {
122128
when.method(GET).path("/endpoint");
123129
then.status(200).body(r#"{ "key": "value" }"#);
124130
});
125-
let client = ReqwestGitHubApiClient::new().unwrap();
131+
let client = ReqwestGitHubApiClient::new(None).unwrap();
126132

127133
let result: FakeApiResponse = client
128134
.download(format!("{}/endpoint", server.base_url()))
@@ -144,7 +150,7 @@ mod tests {
144150
when.method(GET).path("/endpoint");
145151
then.status(200).body("this is not json");
146152
});
147-
let client = ReqwestGitHubApiClient::new().unwrap();
153+
let client = ReqwestGitHubApiClient::new(None).unwrap();
148154

149155
let result: MithrilResult<FakeApiResponse> = client
150156
.download(format!("{}/endpoint", server.base_url()))
@@ -158,7 +164,7 @@ mod tests {
158164

159165
#[tokio::test]
160166
async fn download_fails_on_invalid_url() {
161-
let client = ReqwestGitHubApiClient::new().unwrap();
167+
let client = ReqwestGitHubApiClient::new(None).unwrap();
162168

163169
let result: MithrilResult<FakeApiResponse> = client.download("not a valid url").await;
164170

@@ -172,7 +178,7 @@ mod tests {
172178
when.method(GET).path("/endpoint");
173179
then.status(StatusCode::INTERNAL_SERVER_ERROR.into());
174180
});
175-
let client = ReqwestGitHubApiClient::new().unwrap();
181+
let client = ReqwestGitHubApiClient::new(None).unwrap();
176182

177183
let result: MithrilResult<FakeApiResponse> = client
178184
.download(format!("{}/endpoint", server.base_url()))

0 commit comments

Comments
 (0)