Skip to content

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
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated pypi module
- Added Docstrings
- Renamed PypiData to PyPIData
- Added unit tests
  • Loading branch information
Allstreamer committed Aug 9, 2022
commit 85de44b05e8d79701dce33ca626dfdf34f05bf9e
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use clap::{AppSettings, Parser};

mod pypi;
use pypi::{request_package_info, PypiData};
use pypi::{request_package_info, PyPIData};

/// Python package manager written in Rust
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -48,10 +48,10 @@ enum Opt {
}

fn download_package(package_name: String, package_index: &String) -> Result<()> {
let package_info: PypiData = request_package_info(&package_name, package_index)?;
let package_info: PyPIData = request_package_info(&package_name, package_index)?;

// Example of getting data this will be more robust as the
// PypiData struct gets expanded (meaning less calls to .get())
// PyPIData struct gets expanded (meaning less calls to .get())
let latest_version = package_info.info.get("version").unwrap();
println!("Latest Version of {} is {}", package_name, latest_version);

Expand Down
57 changes: 51 additions & 6 deletions src/pypi.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
//! Warehouse PyPI API Implementation

use log::info;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

///
/// TODO: Implement more specific structs
/// 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)]
pub struct PypiData {
pub struct PyPIData {
/// Contains data such as Package Name, Author,
pub info: serde_json::value::Value,
pub last_serial: i32,
/// List of releases containing data such as
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
/// Implements Warehouse PyPI API call & JSON conversion
///
/// # Example
/// ```
/// use rust-pip::PyPI::request_package_info;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this would error with invalid syntax. Have you tried running the doc tests? That should be cargo test --doc.

///
/// let data = request_package_info("numpy", "https://pypi.org/").unwrap();
/// assert_eq!(data.info.get("license").unwrap(), "BSD");
/// ```
pub fn request_package_info<T>(
package_name: T,
package_index: T,
) -> Result<PypiData, reqwest::Error>
) -> Result<PyPIData, reqwest::Error>
where
T: ToString + Display,
{
Expand All @@ -26,7 +40,38 @@ where
info!("Requesting data from {}", path);
let resp: reqwest::blocking::Response = reqwest::blocking::get(path)?;

let decoded_json: PypiData = resp.json()?;
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.get("license").unwrap(), "BSD");
}

#[test]
fn check_pytorch_name() {
let data = request_package_info("pytorch", "https://pypi.org/").unwrap();

assert_eq!(data.info.get("name").unwrap(), "pytorch");
}

#[test]
fn check_pytorch_download_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"
);
}
}