2014-09-27 04:14:46 +00:00
|
|
|
extern crate curl;
|
|
|
|
extern crate serialize;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::io::{mod, fs, MemReader, MemWriter, File};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::util::ChainedReader;
|
|
|
|
use std::result;
|
|
|
|
|
|
|
|
use curl::http;
|
2014-12-02 03:11:20 +00:00
|
|
|
use curl::http::handle::Method::{Put, Get, Delete};
|
|
|
|
use curl::http::handle::{Method, Request};
|
2014-09-27 04:14:46 +00:00
|
|
|
use serialize::json;
|
|
|
|
|
|
|
|
pub struct Registry {
|
|
|
|
host: String,
|
2014-11-22 14:36:14 +00:00
|
|
|
token: Option<String>,
|
2014-09-27 04:14:46 +00:00
|
|
|
handle: http::Handle,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2014-12-12 04:20:29 +00:00
|
|
|
#[deriving(PartialEq, Copy)]
|
2014-11-22 14:36:14 +00:00
|
|
|
pub enum Auth {
|
|
|
|
Authorized,
|
|
|
|
Unauthorized
|
|
|
|
}
|
|
|
|
|
2014-09-27 04:14:46 +00:00
|
|
|
pub enum Error {
|
2014-11-19 06:29:19 +00:00
|
|
|
Curl(curl::ErrCode),
|
2014-09-27 04:14:46 +00:00
|
|
|
NotOkResponse(http::Response),
|
|
|
|
NonUtf8Body,
|
2014-11-19 06:29:19 +00:00
|
|
|
Api(Vec<String>),
|
2014-09-27 04:14:46 +00:00
|
|
|
Unauthorized,
|
2014-11-22 14:36:14 +00:00
|
|
|
TokenMissing,
|
2014-11-19 06:29:19 +00:00
|
|
|
Io(io::IoError),
|
2014-09-27 04:14:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 14:36:14 +00:00
|
|
|
#[deriving(Decodable)]
|
|
|
|
pub struct Crate {
|
|
|
|
pub name: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub max_version: String
|
|
|
|
}
|
|
|
|
|
2014-09-27 04:14:46 +00:00
|
|
|
#[deriving(Encodable)]
|
|
|
|
pub struct NewCrate {
|
|
|
|
pub name: String,
|
|
|
|
pub vers: String,
|
|
|
|
pub deps: Vec<NewCrateDependency>,
|
|
|
|
pub features: HashMap<String, Vec<String>>,
|
|
|
|
pub authors: Vec<String>,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub documentation: Option<String>,
|
|
|
|
pub homepage: Option<String>,
|
|
|
|
pub readme: Option<String>,
|
|
|
|
pub keywords: Vec<String>,
|
|
|
|
pub license: Option<String>,
|
2014-11-25 06:18:54 +00:00
|
|
|
pub license_file: Option<String>,
|
2014-09-27 04:14:46 +00:00
|
|
|
pub repository: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Encodable)]
|
|
|
|
pub struct NewCrateDependency {
|
|
|
|
pub optional: bool,
|
|
|
|
pub default_features: bool,
|
|
|
|
pub name: String,
|
|
|
|
pub features: Vec<String>,
|
|
|
|
pub version_req: String,
|
|
|
|
pub target: Option<String>,
|
2014-11-21 02:17:31 +00:00
|
|
|
pub kind: String,
|
2014-09-27 04:14:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 06:15:41 +00:00
|
|
|
#[deriving(Decodable)]
|
|
|
|
pub struct User {
|
|
|
|
pub id: uint,
|
|
|
|
pub login: String,
|
|
|
|
pub avatar: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub name: Option<String>,
|
|
|
|
}
|
|
|
|
|
2014-09-27 04:14:46 +00:00
|
|
|
#[deriving(Decodable)] struct R { ok: bool }
|
|
|
|
#[deriving(Decodable)] struct ApiErrorList { errors: Vec<ApiError> }
|
|
|
|
#[deriving(Decodable)] struct ApiError { detail: String }
|
|
|
|
#[deriving(Encodable)] struct OwnersReq<'a> { users: &'a [&'a str] }
|
2014-11-13 06:15:41 +00:00
|
|
|
#[deriving(Decodable)] struct Users { users: Vec<User> }
|
2014-11-22 14:36:14 +00:00
|
|
|
#[deriving(Decodable)] struct Crates { crates: Vec<Crate> }
|
2014-09-27 04:14:46 +00:00
|
|
|
|
|
|
|
impl Registry {
|
2014-11-22 14:36:14 +00:00
|
|
|
pub fn new(host: String, token: Option<String>) -> Registry {
|
2014-09-27 04:14:46 +00:00
|
|
|
Registry::new_handle(host, token, http::Handle::new())
|
|
|
|
}
|
|
|
|
|
2014-11-22 14:36:14 +00:00
|
|
|
pub fn new_handle(host: String, token: Option<String>,
|
2014-09-27 04:14:46 +00:00
|
|
|
handle: http::Handle) -> Registry {
|
|
|
|
Registry {
|
|
|
|
host: host,
|
|
|
|
token: token,
|
|
|
|
handle: handle,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_owners(&mut self, krate: &str, owners: &[&str]) -> Result<()> {
|
|
|
|
let body = json::encode(&OwnersReq { users: owners });
|
|
|
|
let body = try!(self.put(format!("/crates/{}/owners", krate),
|
|
|
|
body.as_bytes()));
|
|
|
|
assert!(json::decode::<R>(body.as_slice()).unwrap().ok);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_owners(&mut self, krate: &str, owners: &[&str]) -> Result<()> {
|
|
|
|
let body = json::encode(&OwnersReq { users: owners });
|
|
|
|
let body = try!(self.delete(format!("/crates/{}/owners", krate),
|
|
|
|
Some(body.as_bytes())));
|
|
|
|
assert!(json::decode::<R>(body.as_slice()).unwrap().ok);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-11-13 06:15:41 +00:00
|
|
|
pub fn list_owners(&mut self, krate: &str) -> Result<Vec<User>> {
|
|
|
|
let body = try!(self.get(format!("/crates/{}/owners", krate)));
|
|
|
|
Ok(json::decode::<Users>(body.as_slice()).unwrap().users)
|
|
|
|
}
|
|
|
|
|
2014-09-27 04:14:46 +00:00
|
|
|
pub fn publish(&mut self, krate: &NewCrate, tarball: &Path) -> Result<()> {
|
|
|
|
let json = json::encode(krate);
|
|
|
|
// Prepare the body. The format of the upload request is:
|
|
|
|
//
|
|
|
|
// <le u32 of json>
|
|
|
|
// <json request> (metadata for the package)
|
|
|
|
// <le u32 of tarball>
|
|
|
|
// <source tarball>
|
2014-11-19 06:29:19 +00:00
|
|
|
let stat = try!(fs::stat(tarball).map_err(Error::Io));
|
2014-09-27 04:14:46 +00:00
|
|
|
let header = {
|
|
|
|
let mut w = MemWriter::new();
|
|
|
|
w.write_le_u32(json.len() as u32).unwrap();
|
|
|
|
w.write_str(json.as_slice()).unwrap();
|
|
|
|
w.write_le_u32(stat.size as u32).unwrap();
|
2014-12-02 03:11:20 +00:00
|
|
|
MemReader::new(w.into_inner())
|
2014-09-27 04:14:46 +00:00
|
|
|
};
|
2014-11-19 06:29:19 +00:00
|
|
|
let tarball = try!(File::open(tarball).map_err(Error::Io));
|
2014-09-27 04:14:46 +00:00
|
|
|
let size = stat.size as uint + header.get_ref().len();
|
|
|
|
let mut body = ChainedReader::new(vec![box header as Box<Reader>,
|
|
|
|
box tarball as Box<Reader>].into_iter());
|
|
|
|
|
|
|
|
let url = format!("{}/api/v1/crates/new", self.host);
|
2014-11-22 14:36:14 +00:00
|
|
|
|
|
|
|
let token = try!(self.token.as_ref().ok_or(Error::TokenMissing)).as_slice();
|
|
|
|
let request = self.handle.put(url, &mut body)
|
|
|
|
.content_length(size)
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.header("Authorization", token);
|
|
|
|
let response = handle(request.exec());
|
2014-09-27 04:14:46 +00:00
|
|
|
let _body = try!(response);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-11-22 14:36:14 +00:00
|
|
|
pub fn search(&mut self, query: &str) -> Result<Vec<Crate>> {
|
|
|
|
let body = try!(self.req(format!("/crates?q={}", query), None, Get, Auth::Unauthorized));
|
|
|
|
|
|
|
|
Ok(json::decode::<Crates>(body.as_slice()).unwrap().crates)
|
|
|
|
}
|
|
|
|
|
2014-09-27 04:14:46 +00:00
|
|
|
pub fn yank(&mut self, krate: &str, version: &str) -> Result<()> {
|
|
|
|
let body = try!(self.delete(format!("/crates/{}/{}/yank", krate, version),
|
|
|
|
None));
|
|
|
|
assert!(json::decode::<R>(body.as_slice()).unwrap().ok);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unyank(&mut self, krate: &str, version: &str) -> Result<()> {
|
|
|
|
let body = try!(self.put(format!("/crates/{}/{}/unyank", krate, version),
|
2014-11-22 10:04:40 +00:00
|
|
|
&[]));
|
2014-09-27 04:14:46 +00:00
|
|
|
assert!(json::decode::<R>(body.as_slice()).unwrap().ok);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn put(&mut self, path: String, b: &[u8]) -> Result<String> {
|
2014-11-22 14:36:14 +00:00
|
|
|
self.req(path, Some(b), Put, Auth::Authorized)
|
2014-11-13 06:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&mut self, path: String) -> Result<String> {
|
2014-11-22 14:36:14 +00:00
|
|
|
self.req(path, None, Get, Auth::Authorized)
|
2014-09-27 04:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn delete(&mut self, path: String, b: Option<&[u8]>) -> Result<String> {
|
2014-11-22 14:36:14 +00:00
|
|
|
self.req(path, b, Delete, Auth::Authorized)
|
2014-11-13 06:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn req(&mut self, path: String, body: Option<&[u8]>,
|
2014-11-22 14:36:14 +00:00
|
|
|
method: Method, authorized: Auth) -> Result<String> {
|
2014-11-13 06:15:41 +00:00
|
|
|
let mut req = Request::new(&mut self.handle, method)
|
|
|
|
.uri(format!("{}/api/v1{}", self.host, path))
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.content_type("application/json");
|
2014-11-22 14:36:14 +00:00
|
|
|
|
|
|
|
if authorized == Auth::Authorized {
|
2014-12-07 20:25:32 +00:00
|
|
|
let token = try!(self.token.as_ref().ok_or(Error::TokenMissing)).as_slice();
|
2014-11-22 14:36:14 +00:00
|
|
|
req = req.header("Authorization", token);
|
|
|
|
}
|
2014-11-13 06:15:41 +00:00
|
|
|
match body {
|
2014-09-27 04:14:46 +00:00
|
|
|
Some(b) => req = req.body(b),
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
handle(req.exec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle(response: result::Result<http::Response, curl::ErrCode>)
|
|
|
|
-> Result<String> {
|
2014-11-19 06:29:19 +00:00
|
|
|
let response = try!(response.map_err(Error::Curl));
|
2014-09-27 04:14:46 +00:00
|
|
|
match response.get_code() {
|
|
|
|
0 => {} // file upload url sometimes
|
|
|
|
200 => {}
|
2014-11-19 06:29:19 +00:00
|
|
|
403 => return Err(Error::Unauthorized),
|
|
|
|
_ => return Err(Error::NotOkResponse(response))
|
2014-09-27 04:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let body = match String::from_utf8(response.move_body()) {
|
|
|
|
Ok(body) => body,
|
2014-11-19 06:29:19 +00:00
|
|
|
Err(..) => return Err(Error::NonUtf8Body),
|
2014-09-27 04:14:46 +00:00
|
|
|
};
|
|
|
|
match json::decode::<ApiErrorList>(body.as_slice()) {
|
|
|
|
Ok(errors) => {
|
2014-11-19 06:29:19 +00:00
|
|
|
return Err(Error::Api(errors.errors.into_iter().map(|s| s.detail)
|
|
|
|
.collect()))
|
2014-09-27 04:14:46 +00:00
|
|
|
}
|
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
Ok(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Show for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2014-11-19 06:29:19 +00:00
|
|
|
Error::NonUtf8Body => write!(f, "reponse body was not utf-8"),
|
|
|
|
Error::Curl(ref err) => write!(f, "http error: {}", err),
|
|
|
|
Error::NotOkResponse(ref resp) => {
|
2014-09-27 04:14:46 +00:00
|
|
|
write!(f, "failed to get a 200 OK response: {}", resp)
|
|
|
|
}
|
2014-11-19 06:29:19 +00:00
|
|
|
Error::Api(ref errs) => {
|
2014-09-27 04:14:46 +00:00
|
|
|
write!(f, "api errors: {}", errs.connect(", "))
|
|
|
|
}
|
2014-11-19 06:29:19 +00:00
|
|
|
Error::Unauthorized => write!(f, "unauthorized API access"),
|
2014-11-22 14:36:14 +00:00
|
|
|
Error::TokenMissing => write!(f, "no upload token found, please run `cargo login`"),
|
2014-11-19 06:29:19 +00:00
|
|
|
Error::Io(ref e) => write!(f, "io error: {}", e),
|
2014-09-27 04:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|