feat: connect to github step

This commit is contained in:
2026-04-25 00:49:50 +01:00
parent a9f4d1d923
commit a54cc84660
12 changed files with 450 additions and 122 deletions

View File

@@ -18,6 +18,8 @@ pub(crate) struct DeviceCodeResponse {
pub user_code: String,
pub vertification_uri: Option<String>,
pub expires_in: u16,
// minimum number of seconds between polling for access token
pub interval: u16,
}
@@ -78,10 +80,32 @@ impl query::QueryFn for RequestAccessToken {
"https://github.com/login/oauth/access_token?client_id={}&device_code={}",
c.github.client_id, self.device_code
))
.header("Accept", "application/json")
.form(&params)
.send()
.await?;
let status = res.status();
api::parse_response(res).await
let data = res.bytes().await?;
println!("status: {:?}, data: {:?}", status, str::from_utf8(&data));
if status.is_success() {
// for device code flow, github returns error with 200 response code
// so the body is either a valid access token or an error
let json: serde_json::Value = serde_json::from_slice(&data)?;
let maybe_error = &json["error"];
if maybe_error.is_string() {
let error = serde_json::from_value::<api::GithubError>(json)?;
Err(api::Error::Github(error))
} else {
let res = serde_json::from_value::<RequestAccessTokenResponse>(json)?;
Ok(res)
}
} else {
serde_json::from_slice::<api::GithubError>(&data)
.map_err(|e| e.into())
.and_then(|e| Err(api::Error::Github(e)))
}
}
}