1
1
use anyhow:: Context ;
2
2
use chrono:: { DateTime , Utc } ;
3
+ use http:: header;
3
4
use http:: header:: USER_AGENT ;
4
5
use serde:: de:: DeserializeOwned ;
5
6
6
7
use crate :: { api:: github:: Issue , load:: SiteCtxt } ;
7
8
9
+ const BOT_USER_AGENT : & str = "perf-rust-lang-org-server" ;
10
+
8
11
/// A client for interacting with the GitHub API
9
12
pub struct Client {
10
13
repository_url : String ,
@@ -227,6 +230,49 @@ impl Client {
227
230
}
228
231
}
229
232
233
+ async fn send (
234
+ & self ,
235
+ request : reqwest:: RequestBuilder ,
236
+ ) -> Result < reqwest:: Response , reqwest:: Error > {
237
+ request
238
+ . header ( USER_AGENT , BOT_USER_AGENT )
239
+ . basic_auth ( "rust-timer" , Some ( & self . token ) )
240
+ . send ( )
241
+ . await
242
+ }
243
+ }
244
+
245
+ const GRAPHQL_API_BASE : & str = "https://api.github.com/graphql" ;
246
+
247
+ /// A client for interacting with the GraphQL GitHub API.
248
+ pub struct GraphQLClient {
249
+ inner : reqwest:: Client ,
250
+ }
251
+
252
+ impl GraphQLClient {
253
+ /// Create a GraphQL client from a `SiteCtxt`.
254
+ pub fn from_ctxt ( ctxt : & SiteCtxt ) -> Self {
255
+ let token = ctxt
256
+ . config
257
+ . keys
258
+ . github_api_token
259
+ . clone ( )
260
+ . expect ( "needs github API token" ) ;
261
+
262
+ let mut headers = header:: HeaderMap :: new ( ) ;
263
+ headers. insert ( USER_AGENT , header:: HeaderValue :: from_static ( BOT_USER_AGENT ) ) ;
264
+ headers. insert (
265
+ header:: AUTHORIZATION ,
266
+ header:: HeaderValue :: from_str ( & format ! ( "token {}" , token) ) . unwrap ( ) ,
267
+ ) ;
268
+
269
+ let client = reqwest:: ClientBuilder :: new ( )
270
+ . default_headers ( headers)
271
+ . build ( )
272
+ . unwrap ( ) ;
273
+ Self { inner : client }
274
+ }
275
+
230
276
pub async fn get_comments ( & self , pull_request : u32 ) -> anyhow:: Result < Vec < ResponseComment > > {
231
277
const QUERY : & str = "query($owner: String!, $repo: String!, $pr: Int!, $cursor: String) {
232
278
repository(owner: $owner, name: $repo) {
@@ -273,7 +319,7 @@ impl Client {
273
319
let mut cursor = None ;
274
320
loop {
275
321
let mut resp: Response = self
276
- . graphql (
322
+ . send (
277
323
QUERY ,
278
324
serde_json:: json!( {
279
325
"owner" : owner,
@@ -303,7 +349,7 @@ impl Client {
303
349
}
304
350
}" ;
305
351
306
- self . graphql :: < Option < MinimizeData > , _ > (
352
+ self . send :: < Option < MinimizeData > , _ > (
307
353
MINIMIZE ,
308
354
serde_json:: json!( {
309
355
"node_id" : comment_id,
@@ -314,7 +360,7 @@ impl Client {
314
360
Ok ( ( ) )
315
361
}
316
362
317
- async fn graphql < T : DeserializeOwned , V : serde:: Serialize > (
363
+ async fn send < T : DeserializeOwned , V : serde:: Serialize > (
318
364
& self ,
319
365
query : & str ,
320
366
variables : V ,
@@ -327,7 +373,7 @@ impl Client {
327
373
328
374
let response: GraphResponse < T > = self
329
375
. inner
330
- . post ( & self . repository_url )
376
+ . post ( GRAPHQL_API_BASE )
331
377
. json ( & GraphPayload { query, variables } )
332
378
. send ( )
333
379
. await ?
@@ -344,17 +390,6 @@ impl Client {
344
390
) )
345
391
}
346
392
}
347
-
348
- async fn send (
349
- & self ,
350
- request : reqwest:: RequestBuilder ,
351
- ) -> Result < reqwest:: Response , reqwest:: Error > {
352
- request
353
- . header ( USER_AGENT , "perf-rust-lang-org-server" )
354
- . basic_auth ( "rust-timer" , Some ( & self . token ) )
355
- . send ( )
356
- . await
357
- }
358
393
}
359
394
360
395
#[ derive( serde:: Deserialize ) ]
0 commit comments