Skip to content

Commit 9af64bb

Browse files
Use octocrab for GraphQL queries (#187)
Remove the manual use of reqwest::Client for graphql queries and use octocrab instead. This removes the need to specify the github api base url multiple times. We instead leverage the global octocrab instance for which we configure the api base url once. This is beneficial for a cleaner implementation of Github Enterprise support (#158), as we can configure the base url once. For an example of what GHE support could look like on top of this, here is a draft PR jtietema#1 --------- Co-authored-by: Sven Over <sp@cedenti.st>
1 parent 217408a commit 9af64bb

File tree

3 files changed

+9
-33
lines changed

3 files changed

+9
-33
lines changed

spr/src/commands/list.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use crate::error::Error;
99
use crate::error::Result;
1010
use graphql_client::{GraphQLQuery, Response};
11-
use reqwest;
1211

1312
#[allow(clippy::upper_case_acronyms)]
1413
type URI = String;
@@ -21,7 +20,6 @@ type URI = String;
2120
pub struct SearchQuery;
2221

2322
pub async fn list(
24-
graphql_client: reqwest::Client,
2523
config: &crate::config::Config,
2624
) -> Result<()> {
2725
let variables = search_query::Variables {
@@ -31,13 +29,9 @@ pub async fn list(
3129
),
3230
};
3331
let request_body = SearchQuery::build_query(variables);
34-
let res = graphql_client
35-
.post("https://api.github.com/graphql")
36-
.json(&request_body)
37-
.send()
32+
let response_body: Response<search_query::ResponseData> = octocrab::instance()
33+
.post("/graphql",Some(&request_body))
3834
.await?;
39-
let response_body: Response<search_query::ResponseData> =
40-
res.json().await?;
4135

4236
print_pr_info(response_body).ok_or_else(|| Error::new("unexpected error"))
4337
}

spr/src/github.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::collections::{HashMap, HashSet};
2121
pub struct GitHub {
2222
config: crate::config::Config,
2323
git: crate::git::Git,
24-
graphql_client: reqwest::Client,
2524
}
2625

2726
#[derive(Debug, Clone)]
@@ -134,12 +133,10 @@ impl GitHub {
134133
pub fn new(
135134
config: crate::config::Config,
136135
git: crate::git::Git,
137-
graphql_client: reqwest::Client,
138136
) -> Self {
139137
Self {
140138
config,
141139
git,
142-
graphql_client,
143140
}
144141
}
145142

@@ -165,7 +162,6 @@ impl GitHub {
165162
let GitHub {
166163
config,
167164
git,
168-
graphql_client,
169165
} = self;
170166

171167
let variables = pull_request_query::Variables {
@@ -174,13 +170,9 @@ impl GitHub {
174170
number: number as i64,
175171
};
176172
let request_body = PullRequestQuery::build_query(variables);
177-
let res = graphql_client
178-
.post("https://api.github.com/graphql")
179-
.json(&request_body)
180-
.send()
173+
let response_body: Response<pull_request_query::ResponseData> = octocrab::instance()
174+
.post("/graphql", Some(&request_body))
181175
.await?;
182-
let response_body: Response<pull_request_query::ResponseData> =
183-
res.json().await?;
184176

185177
if let Some(errors) = response_body.errors {
186178
let error =
@@ -395,15 +387,10 @@ impl GitHub {
395387
number: number as i64,
396388
};
397389
let request_body = PullRequestMergeabilityQuery::build_query(variables);
398-
let res = self
399-
.graphql_client
400-
.post("https://api.github.com/graphql")
401-
.json(&request_body)
402-
.send()
403-
.await?;
404-
let response_body: Response<
405-
pull_request_mergeability_query::ResponseData,
406-
> = res.json().await?;
390+
let response_body: Response<pull_request_mergeability_query::ResponseData> =
391+
octocrab::instance()
392+
.post("/graphql", Some(&request_body))
393+
.await?;
407394

408395
if let Some(errors) = response_body.errors {
409396
let error = Err(Error::new(format!(

spr/src/main.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,9 @@ pub async fn spr() -> Result<()> {
170170
format!("Bearer {}", github_auth_token).parse()?,
171171
);
172172

173-
let graphql_client = reqwest::Client::builder()
174-
.default_headers(headers)
175-
.build()?;
176-
177173
let mut gh = spr::github::GitHub::new(
178174
config.clone(),
179175
git.clone(),
180-
graphql_client.clone(),
181176
);
182177

183178
match cli.command {
@@ -190,7 +185,7 @@ pub async fn spr() -> Result<()> {
190185
Commands::Amend(opts) => {
191186
commands::amend::amend(opts, &git, &mut gh, &config).await?
192187
}
193-
Commands::List => commands::list::list(graphql_client, &config).await?,
188+
Commands::List => commands::list::list(&config).await?,
194189
Commands::Patch(opts) => {
195190
commands::patch::patch(opts, &git, &mut gh, &config).await?
196191
}

0 commit comments

Comments
 (0)