-
Notifications
You must be signed in to change notification settings - Fork 10
Implemented Warehouse Pypi API Call #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Allstreamer
wants to merge
9
commits into
Rust-Python-Packaging:develop
Choose a base branch
from
Allstreamer:pypi-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
90b02dc
Implemented Warehouse Pypi API Call
Allstreamer 641a1e4
Merge branch 'develop' into pypi-api
Allstreamer 605ae33
Formatting & Linting
Allstreamer 85de44b
Updated pypi module
Allstreamer 052c987
Rename check_pytorch_download_v1
Allstreamer 50eecae
Updated pypi.rs to include negative test case
Allstreamer 3803f21
Update pypi.rs
Allstreamer 2180227
Added PyPIPackageInfo & PyPIPackageDownloadInfo
Allstreamer f42e648
Fixed Documentation
Allstreamer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
//! Warehouse PyPI API Implementation | ||
|
||
use log::info; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Display; | ||
|
||
/// Download stats | ||
#[derive(Debug, Serialize, Deserialize, PartialEq)] | ||
pub struct PyPIPackageDownloadInfo { | ||
last_day: i32, | ||
last_week: i32, | ||
last_month: i32, | ||
} | ||
/// Public package information | ||
#[derive(Debug, Serialize, Deserialize, PartialEq)] | ||
pub struct PyPIPackageInfo { | ||
pub author: String, | ||
pub author_email: String, | ||
pub bugtrack_url: serde_json::value::Value, | ||
pub classifiers: Vec<String>, | ||
pub description: String, | ||
pub description_content_type: String, | ||
pub docs_url: serde_json::value::Value, | ||
pub download_url: String, | ||
pub downloads: PyPIPackageDownloadInfo, | ||
pub home_page: String, | ||
pub keywords: String, | ||
pub license: String, | ||
pub maintainer: String, | ||
pub maintainer_email: String, | ||
/// Package name | ||
pub name: String, | ||
pub package_url: String, | ||
pub platform: String, | ||
pub project_url: String, | ||
pub project_urls: serde_json::value::Value, | ||
pub release_url: String, | ||
pub requires_dist: serde_json::value::Value, | ||
/// Minimum required python version | ||
pub requires_python: String, | ||
/// Project Summary | ||
pub summary: String, | ||
/// Latest stable version number | ||
pub version: String, | ||
pub yanked: bool, | ||
pub yanked_reason: serde_json::value::Value, | ||
} | ||
|
||
/// Set of Information describing a Python package hosted on a Warehouse instance | ||
/// for exact details of what is contained go to <https://warehouse.pypa.io/api-reference/json.html#project> | ||
#[derive(Debug, Serialize, Deserialize, PartialEq)] | ||
pub struct PyPIData { | ||
/// Contains data such as package name, author and license | ||
pub info: PyPIPackageInfo, | ||
pub last_serial: i32, | ||
/// List of releases containing Object with downloads for each release and it's versions | ||
pub releases: serde_json::value::Value, | ||
/// Link and related data to sdist & bdist_wheel | ||
pub urls: Vec<serde_json::value::Value>, | ||
/// Vector of known vulnerabilities of the package | ||
pub vulnerabilities: Vec<serde_json::value::Value>, | ||
} | ||
|
||
/// Implements Warehouse PyPI API call & JSON conversion | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use pypi::request_package_info; | ||
/// | ||
/// let data = request_package_info("numpy", "https://pypi.org/").unwrap(); | ||
/// assert_eq!(data.info.license, "BSD"); | ||
/// ``` | ||
pub fn request_package_info<T>( | ||
package_name: T, | ||
package_index: T, | ||
) -> Result<PyPIData, reqwest::Error> | ||
where | ||
T: ToString + Display, | ||
{ | ||
let path = format!("{}/pypi/{}/json", package_index, package_name); | ||
|
||
info!("Requesting data from {}", path); | ||
let resp: reqwest::blocking::Response = reqwest::blocking::get(path)?; | ||
|
||
let decoded_json: PyPIData = resp.json()?; | ||
|
||
Ok(decoded_json) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::pypi::request_package_info; | ||
|
||
#[test] | ||
fn check_numpy_licence() { | ||
let data = request_package_info("numpy", "https://pypi.org/").unwrap(); | ||
|
||
assert_eq!(data.info.license, "BSD"); | ||
} | ||
|
||
#[test] | ||
fn check_pytorch_name() { | ||
let data = request_package_info("pytorch", "https://pypi.org/").unwrap(); | ||
|
||
assert_eq!(data.info.name, "pytorch"); | ||
} | ||
|
||
#[test] | ||
fn check_numpy_download_name_v1() { | ||
let data = request_package_info("numpy", "https://pypi.org/").unwrap(); | ||
|
||
assert_eq!( | ||
data.releases.get("1.0").unwrap()[0] | ||
.get("filename") | ||
.unwrap(), | ||
"numpy-1.0.1.dev3460.win32-py2.4.exe" | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "`Err` value: reqwest::Error")] | ||
fn check_fails_invalid_url() { | ||
let _err = | ||
request_package_info("numpy", "invalid_url obviously wrong").unwrap(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The typical Rust convention is to keep acronyms with the first letter in uppercase, and the rest in lowercase. This would also need to be updated in the rest of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
John wanted this formatting and I think matching the Offical naming looks better.