Skip to content

Commit

Permalink
change: now handles when the rate limit has been reached
Browse files Browse the repository at this point in the history
  • Loading branch information
MordechaiHadad committed Nov 12, 2023
1 parent f75e445 commit 162c89e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 67 deletions.
80 changes: 80 additions & 0 deletions src/github_requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use reqwest::Client;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpstreamVersion {
pub tag_name: String,
pub target_commitish: Option<String>,
pub published_at: DateTime<Utc>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RepoCommit {
pub commit: Commit,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Commit {
pub author: CommitAuthor,
pub message: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CommitAuthor {
pub name: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ErrorResponse {
pub message: String,
pub documentation_url: String,
}

pub async fn get_upstream_nightly(client: &Client) -> Result<UpstreamVersion> {
let response = client
.get("https://api.github.com/repos/neovim/neovim/releases/tags/nightly")
.header("user-agent", "bob")
.header("Accept", "application/vnd.github.v3+json")
.send()
.await?
.text()
.await?;

deserialize_response(response)
}

pub async fn get_commits_for_nightly(
client: &Client,
since: &DateTime<Utc>,
until: &DateTime<Utc>,
) -> Result<Vec<RepoCommit>> {
let response = client
.get(format!(
"https://api.github.com/repos/neovim/neovim/commits?since={since}&until={until}&per_page=100"))
.header("user-agent", "bob")
.header("Accept", "application/vnd.github.v3+json")
.send()
.await?
.text()
.await?;

deserialize_response(response)
}

pub fn deserialize_response<T: DeserializeOwned>(response: String) -> Result<T> {
let value: serde_json::Value = serde_json::from_str(&response)?;

if let Some(_) = value.get("message") {

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / linux

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / linux

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / macos

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / macos-m1

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / macos

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / linux-arm

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / windows

redundant pattern matching, consider using `is_some()`

Check failure on line 69 in src/github_requests.rs

View workflow job for this annotation

GitHub Actions / windows

redundant pattern matching, consider using `is_some()`
let result: ErrorResponse = serde_json::from_value(value)?;

if result.documentation_url.contains("rate-limiting") {
return Err(anyhow!("Github API rate limit has been reach, either wait an hour or checkout https://github.com/MordechaiHadad/bob#increasing-github-rate-limit"));
}

return Err(anyhow!(result.message));
}

Ok(serde_json::from_value(value)?)
}
7 changes: 4 additions & 3 deletions src/handlers/install_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::config::Config;
use crate::github_requests::{get_upstream_nightly, UpstreamVersion, get_commits_for_nightly};
use crate::helpers::directories::get_downloads_directory;
use crate::helpers::version::nightly::{get_commits_for_nightly, produce_nightly_vec};
use crate::helpers::version::types::{LocalVersion, ParsedVersion, UpstreamVersion, VersionType};
use crate::helpers::version::nightly::produce_nightly_vec;
use crate::helpers::version::types::{LocalVersion, ParsedVersion, VersionType};
use crate::helpers::{self, directories, filesystem, handle_subprocess, unarchive};
use anyhow::{anyhow, Result};
use futures_util::stream::StreamExt;
Expand Down Expand Up @@ -42,7 +43,7 @@ pub async fn start(
}

let nightly_version = if version.version_type == VersionType::Nightly {
Some(helpers::version::nightly::get_upstream_nightly(client).await?)
Some(get_upstream_nightly(client).await?)
} else {
None
};
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod types;

use self::types::{ParsedVersion, VersionType};
use super::directories;
use crate::{config::Config, helpers::version::types::UpstreamVersion};
use crate::{config::Config, github_requests::{UpstreamVersion, deserialize_response}};
use anyhow::{anyhow, Context, Result};
use regex::Regex;
use reqwest::Client;
Expand Down Expand Up @@ -125,7 +125,7 @@ async fn search_stable_version(client: &Client) -> Result<String> {
.text()
.await?;

let versions: Vec<UpstreamVersion> = serde_json::from_str(&response)?;
let versions: Vec<UpstreamVersion> = deserialize_response(response, )?;
let stable_release = versions
.iter()
.find(|v| v.tag_name == "stable")
Expand Down
54 changes: 2 additions & 52 deletions src/helpers/version/nightly.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,10 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use regex::Regex;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tokio::fs;

use super::types::{LocalNightly, UpstreamVersion};
use crate::{config::Config, helpers::directories};
use super::types::LocalNightly;
use crate::{config::Config, helpers::directories, github_requests::UpstreamVersion};

#[derive(Serialize, Deserialize, Debug)]
pub struct RepoCommit {
pub commit: Commit,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Commit {
pub author: CommitAuthor,
pub message: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CommitAuthor {
pub name: String,
}

pub async fn get_upstream_nightly(client: &Client) -> Result<UpstreamVersion> {
let response = client
.get("https://api.github.com/repos/neovim/neovim/releases/tags/nightly")
.header("user-agent", "bob")
.header("Accept", "application/vnd.github.v3+json")
.send()
.await?
.text()
.await?;

serde_json::from_str(&response)
.map_err(|_| anyhow!("Failed to get upstream nightly version"))
}

pub async fn get_local_nightly(config: &Config) -> Result<UpstreamVersion> {
let downloads_dir = directories::get_downloads_directory(config).await?;
Expand All @@ -50,24 +18,6 @@ pub async fn get_local_nightly(config: &Config) -> Result<UpstreamVersion> {
}
}

pub async fn get_commits_for_nightly(
client: &Client,
since: &DateTime<Utc>,
until: &DateTime<Utc>,
) -> Result<Vec<RepoCommit>> {
let response = client
.get(format!(
"https://api.github.com/repos/neovim/neovim/commits?since={since}&until={until}&per_page=100"))
.header("user-agent", "bob")
.header("Accept", "application/vnd.github.v3+json")
.send()
.await?
.text()
.await?;

Ok(serde_json::from_str(&response)?)
}

pub async fn produce_nightly_vec(config: &Config) -> Result<Vec<LocalNightly>> {
let downloads_dir = directories::get_downloads_directory(config).await?;
let mut paths = fs::read_dir(&downloads_dir).await?;
Expand Down
11 changes: 1 addition & 10 deletions src/helpers/version/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use chrono::{DateTime, Utc};

use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::github_requests::UpstreamVersion;

pub struct ParsedVersion {
pub tag_name: String,
Expand All @@ -18,13 +16,6 @@ pub enum VersionType {
NightlyRollback
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpstreamVersion {
pub tag_name: String,
pub target_commitish: Option<String>,
pub published_at: DateTime<Utc>,
}

#[derive(Debug, Clone)]
pub struct LocalNightly {
pub data: UpstreamVersion,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod cli;
mod config;
mod handlers;
mod helpers;
pub mod github_requests;

extern crate core;

Expand Down

0 comments on commit 162c89e

Please sign in to comment.