diff --git a/src/github/api.rs b/src/github/api.rs index b4d69cf..65e4cc3 100644 --- a/src/github/api.rs +++ b/src/github/api.rs @@ -10,7 +10,7 @@ use jwt_simple::{ claims::Claims, reexports::coarsetime::Duration, }; -use reqwest::{RequestBuilder, StatusCode}; +use reqwest::{RequestBuilder, Response, StatusCode}; use serde::Deserialize; use tracing::{debug, warn}; @@ -81,7 +81,7 @@ impl GitHub { .post(format!("app/installations/{installation}/access_tokens")) .send() .await? - .json() + .parse_json() .await?; self.jwt = installation_token.token; @@ -252,3 +252,20 @@ pub struct Installation { struct InstallationToken { token: String, } + +trait JsonExt { + async fn parse_json Deserialize<'a>>(self) -> Result; +} + +impl JsonExt for Response { + async fn parse_json Deserialize<'a>>(self) -> Result { + let bytes = self.bytes().await?; + + debug!( + "Parsing JSON: {}", + std::str::from_utf8(&bytes).unwrap_or("[INVALID UTF8]") + ); + + Ok(serde_json::from_slice(&bytes)?) + } +} diff --git a/src/github/api/pr.rs b/src/github/api/pr.rs index 397fc6e..951e2f4 100644 --- a/src/github/api/pr.rs +++ b/src/github/api/pr.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use super::{ApiError, GitHub, OwnerId, RepoId}; +use super::{ApiError, GitHub, JsonExt, OwnerId, RepoId}; #[derive(Clone, Debug, Deserialize)] pub struct MinimalPullRequest { @@ -14,17 +14,16 @@ impl MinimalPullRequest { owner: OwnerId, repo: RepoId, ) -> Result { - Ok(api - .get(format!( - "repos/{owner}/{repo}/pulls/{pull_number}", - owner = owner, - repo = repo, - pull_number = self.number - )) - .send() - .await? - .json() - .await?) + api.get(format!( + "repos/{owner}/{repo}/pulls/{pull_number}", + owner = owner, + repo = repo, + pull_number = self.number + )) + .send() + .await? + .parse_json() + .await } } @@ -74,12 +73,11 @@ impl GitHub { pr: usize, update: PullRequestUpdate, ) -> Result { - Ok(self - .patch(format!("{}/{}/pulls/{}", owner, repo, pr)) + self.patch(format!("{}/{}/pulls/{}", owner, repo, pr)) .json(&update) .send() .await? - .json() - .await?) + .parse_json() + .await } }