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
Next Next commit
Implemented Warehouse Pypi API Call
- Added log 0.4 Crate
- Added serde 1.0 Crate
- Added serde_json 1.0 Crate
- Created pypi.rs
- Created request_package_info function
- Created PypiData struct
- Added request_package_info example to main.rs
  • Loading branch information
Allstreamer committed Aug 8, 2022
commit 90b02dcd2f2c825cee1c9338e12e2fb9cda78949
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ reqwest = { version = "0.11", features = ["blocking", "json"] }
structopt = { version = "0.3.26", features = ["color"] }
strum = "0.24.1"
strum_macros = "0.24.2"
log = "0.4"
serde = {version = "1", features = ["derive"]}
serde_json = "1.0"


[dev-dependencies]
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::path::PathBuf;
use structopt::StructOpt;
use anyhow::Result;

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

/// A basic example
#[derive(StructOpt, Debug)]
Expand Down Expand Up @@ -44,7 +48,16 @@ enum Opt {
Help {},
}

fn download_package(package_name: String, package_index: &String) {}
fn download_package(package_name: String, package_index: &String) -> Result<()> {
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())
let latest_version = package_info.info.get("version").unwrap();
println!("Latest Version of {} is {}", package_name, latest_version);

Ok(())
}

fn main() {
let opt = Opt::from_args();
Expand Down
29 changes: 29 additions & 0 deletions src/pypi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use log::info;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

///
/// TODO: Implement more specific structs
#[derive(Debug, Serialize, Deserialize)]
pub struct PypiData {
pub info: serde_json::value::Value,
pub last_serial: i32,
pub releases: serde_json::value::Value,
pub urls: Vec<serde_json::value::Value>,
pub vulnerabilities: Vec<serde_json::value::Value>,
}

/// Implements Warehouse Pypi API call & JSON conversion
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)
}