-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(codebase)!: move internal logic to
lib
(#4)
This refactors **the whole codebase** and makes some additions, for example: - better documentation - performance improvements - error handling - integration & unit tests - other stuff i forgot about --------- Signed-off-by: Adam Perkowski <adas1per@protonmail.com>
- Loading branch information
1 parent
f2e22b6
commit c0021f0
Showing
19 changed files
with
900 additions
and
512 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
newver.json | ||
oldver.json | ||
keyfile.toml | ||
*_old | ||
*.old |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# nvrs | ||
# https://github.com/adamperkowski/nvrs | ||
|
||
# this is an example key configuration file | ||
# see `nvrs.toml` | ||
|
||
[keys] | ||
# github = "[REDACTED]" | ||
# gitlab = "[REDACTED]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,49 @@ | ||
use reqwest::{ | ||
header::{HeaderMap, HeaderValue, USER_AGENT}, | ||
StatusCode, | ||
}; | ||
use crate::{api, error}; | ||
|
||
pub fn get_latest(package: String, _: Vec<String>, _: String) -> crate::api::ReleaseFuture { | ||
#[derive(serde::Deserialize)] | ||
struct AURResponse { | ||
results: Vec<AURResult>, | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[derive(serde::Deserialize)] | ||
struct AURResult { | ||
Version: String, | ||
} | ||
|
||
pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture { | ||
Box::pin(async move { | ||
let url = format!("https://aur.archlinux.org/rpc/v5/info/{}", package); | ||
let mut headers = HeaderMap::new(); | ||
headers.insert(USER_AGENT, HeaderValue::from_static("nvrs")); | ||
let client = reqwest::Client::new(); | ||
|
||
let result = client.get(url).headers(headers).send().await.unwrap(); | ||
|
||
match result.status() { | ||
StatusCode::OK => (), | ||
status => { | ||
crate::custom_error("GET request didn't return 200", format!("\n{}", status), ""); | ||
return None; | ||
} | ||
} | ||
let url = format!("https://aur.archlinux.org/rpc/v5/info/{}", args.args[0]); | ||
let client = args.request_client; | ||
|
||
let result = client.get(url).headers(api::setup_headers()).send().await?; | ||
api::match_statuscode(&result.status(), args.package.clone())?; | ||
|
||
let json: serde_json::Value = result.json().await.unwrap(); | ||
let first_result = json.get("results").unwrap().get(0).unwrap(); | ||
|
||
Some(crate::api::Release { | ||
tag_name: first_result | ||
.get("Version") | ||
.unwrap() | ||
.to_string() | ||
.split('-') | ||
.next() | ||
.unwrap_or("") | ||
.replace("\"", "") | ||
.to_string(), | ||
html_url: first_result | ||
.get("URL") | ||
.unwrap() | ||
.to_string() | ||
.replace("\"", ""), | ||
}) | ||
let json: AURResponse = result.json().await?; | ||
|
||
if let Some(first) = json.results.first() { | ||
let version = first.Version.split_once('-').unwrap(); | ||
|
||
Ok(api::Release { | ||
name: version.0.to_string(), | ||
tag: None, | ||
url: String::new(), | ||
}) | ||
} else { | ||
Err(error::Error::NoVersion(args.package)) | ||
} | ||
}) | ||
} | ||
|
||
#[tokio::test] | ||
async fn request_test() { | ||
let package = "permitter".to_string(); | ||
let args = api::ApiArgs { | ||
package: package.clone(), | ||
args: vec![package], | ||
api_key: String::new(), | ||
request_client: reqwest::Client::new(), | ||
}; | ||
|
||
assert!(get_latest(args).await.is_ok()); | ||
} |
Oops, something went wrong.