@@ -9,25 +9,31 @@ use super::{GitHubRelease, GitHubReleaseRetriever};
9
9
10
10
pub struct ReqwestGitHubApiClient {
11
11
client : Client ,
12
+ github_token : Option < String > ,
12
13
}
13
14
14
15
impl ReqwestGitHubApiClient {
15
- pub fn new ( ) -> MithrilResult < Self > {
16
+ pub fn new ( github_token : Option < String > ) -> MithrilResult < Self > {
16
17
let client = Client :: builder ( )
17
18
. user_agent ( "mithril-client" )
18
19
. build ( )
19
20
. context ( "Failed to build Reqwest GitHub API client" ) ?;
20
21
21
- Ok ( Self { client } )
22
+ Ok ( Self {
23
+ client,
24
+ github_token,
25
+ } )
22
26
}
23
27
24
28
async fn download < U : IntoUrl , T : DeserializeOwned > ( & self , source_url : U ) -> MithrilResult < T > {
25
29
let url = source_url
26
30
. into_url ( )
27
31
. 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
31
37
. send ( )
32
38
. await
33
39
. with_context ( || format ! ( "Failed to send request to GitHub API: {}" , url) ) ?;
@@ -122,7 +128,7 @@ mod tests {
122
128
when. method ( GET ) . path ( "/endpoint" ) ;
123
129
then. status ( 200 ) . body ( r#"{ "key": "value" }"# ) ;
124
130
} ) ;
125
- let client = ReqwestGitHubApiClient :: new ( ) . unwrap ( ) ;
131
+ let client = ReqwestGitHubApiClient :: new ( None ) . unwrap ( ) ;
126
132
127
133
let result: FakeApiResponse = client
128
134
. download ( format ! ( "{}/endpoint" , server. base_url( ) ) )
@@ -144,7 +150,7 @@ mod tests {
144
150
when. method ( GET ) . path ( "/endpoint" ) ;
145
151
then. status ( 200 ) . body ( "this is not json" ) ;
146
152
} ) ;
147
- let client = ReqwestGitHubApiClient :: new ( ) . unwrap ( ) ;
153
+ let client = ReqwestGitHubApiClient :: new ( None ) . unwrap ( ) ;
148
154
149
155
let result: MithrilResult < FakeApiResponse > = client
150
156
. download ( format ! ( "{}/endpoint" , server. base_url( ) ) )
@@ -158,7 +164,7 @@ mod tests {
158
164
159
165
#[ tokio:: test]
160
166
async fn download_fails_on_invalid_url ( ) {
161
- let client = ReqwestGitHubApiClient :: new ( ) . unwrap ( ) ;
167
+ let client = ReqwestGitHubApiClient :: new ( None ) . unwrap ( ) ;
162
168
163
169
let result: MithrilResult < FakeApiResponse > = client. download ( "not a valid url" ) . await ;
164
170
@@ -172,7 +178,7 @@ mod tests {
172
178
when. method ( GET ) . path ( "/endpoint" ) ;
173
179
then. status ( StatusCode :: INTERNAL_SERVER_ERROR . into ( ) ) ;
174
180
} ) ;
175
- let client = ReqwestGitHubApiClient :: new ( ) . unwrap ( ) ;
181
+ let client = ReqwestGitHubApiClient :: new ( None ) . unwrap ( ) ;
176
182
177
183
let result: MithrilResult < FakeApiResponse > = client
178
184
. download ( format ! ( "{}/endpoint" , server. base_url( ) ) )
0 commit comments