From 3a31f5be242da7a37623c671d0604f26d3db7202 Mon Sep 17 00:00:00 2001 From: Jeroen Tietema Date: Thu, 25 Jan 2024 08:30:39 +0100 Subject: [PATCH] Use octocrab for GraphQL queries Remove the manual use of reqwest::Client for graphql queries and use octocrab instead. This removes the need to specify the github api url multiple times. We instead leverage the global octocrab instance for which we configure the api url once. This is beneficial for a cleaner implementation of Github Enterprise support (#158), as we can configure the base url once. --- spr/src/commands/list.rs | 10 ++-------- spr/src/github.rs | 25 ++++++------------------- spr/src/main.rs | 7 +------ 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/spr/src/commands/list.rs b/spr/src/commands/list.rs index 81670e0..0434cda 100644 --- a/spr/src/commands/list.rs +++ b/spr/src/commands/list.rs @@ -8,7 +8,6 @@ use crate::error::Error; use crate::error::Result; use graphql_client::{GraphQLQuery, Response}; -use reqwest; #[allow(clippy::upper_case_acronyms)] type URI = String; @@ -21,7 +20,6 @@ type URI = String; pub struct SearchQuery; pub async fn list( - graphql_client: reqwest::Client, config: &crate::config::Config, ) -> Result<()> { let variables = search_query::Variables { @@ -31,13 +29,9 @@ pub async fn list( ), }; let request_body = SearchQuery::build_query(variables); - let res = graphql_client - .post("https://api.github.com/graphql") - .json(&request_body) - .send() + let response_body: Response = octocrab::instance() + .post("graphql",Some(&request_body)) .await?; - let response_body: Response = - res.json().await?; print_pr_info(response_body).ok_or_else(|| Error::new("unexpected error")) } diff --git a/spr/src/github.rs b/spr/src/github.rs index 4be8c31..dfa6fd9 100644 --- a/spr/src/github.rs +++ b/spr/src/github.rs @@ -21,7 +21,6 @@ use std::collections::{HashMap, HashSet}; pub struct GitHub { config: crate::config::Config, git: crate::git::Git, - graphql_client: reqwest::Client, } #[derive(Debug, Clone)] @@ -134,12 +133,10 @@ impl GitHub { pub fn new( config: crate::config::Config, git: crate::git::Git, - graphql_client: reqwest::Client, ) -> Self { Self { config, git, - graphql_client, } } @@ -165,7 +162,6 @@ impl GitHub { let GitHub { config, git, - graphql_client, } = self; let variables = pull_request_query::Variables { @@ -174,13 +170,9 @@ impl GitHub { number: number as i64, }; let request_body = PullRequestQuery::build_query(variables); - let res = graphql_client - .post("https://api.github.com/graphql") - .json(&request_body) - .send() + let response_body: Response = octocrab::instance() + .post("graphql", Some(&request_body)) .await?; - let response_body: Response = - res.json().await?; if let Some(errors) = response_body.errors { let error = @@ -395,15 +387,10 @@ impl GitHub { number: number as i64, }; let request_body = PullRequestMergeabilityQuery::build_query(variables); - let res = self - .graphql_client - .post("https://api.github.com/graphql") - .json(&request_body) - .send() - .await?; - let response_body: Response< - pull_request_mergeability_query::ResponseData, - > = res.json().await?; + let response_body: Response = + octocrab::instance() + .post("graphql", Some(&request_body)) + .await?; if let Some(errors) = response_body.errors { let error = Err(Error::new(format!( diff --git a/spr/src/main.rs b/spr/src/main.rs index a89a48d..3ddad3f 100644 --- a/spr/src/main.rs +++ b/spr/src/main.rs @@ -172,14 +172,9 @@ pub async fn spr() -> Result<()> { format!("Bearer {}", github_auth_token).parse()?, ); - let graphql_client = reqwest::Client::builder() - .default_headers(headers) - .build()?; - let mut gh = spr::github::GitHub::new( config.clone(), git.clone(), - graphql_client.clone(), ); match cli.command { @@ -192,7 +187,7 @@ pub async fn spr() -> Result<()> { Commands::Amend(opts) => { commands::amend::amend(opts, &git, &mut gh, &config).await? } - Commands::List => commands::list::list(graphql_client, &config).await?, + Commands::List => commands::list::list(&config).await?, Commands::Patch(opts) => { commands::patch::patch(opts, &git, &mut gh, &config).await? }