1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#[macro_use] extern crate failure; extern crate futures; extern crate hyper; extern crate hyper_tls; #[macro_use] extern crate serde_derive; extern crate serde_json; mod models; use std::vec::Vec; use failure::Error; use futures::{Future, Stream}; use hyper::{Body, Method, Request, StatusCode}; use hyper::client::{Client, HttpConnector}; use hyper_tls::HttpsConnector; use models::{App, Category, NewRelease}; fn get_https_client() -> Client<HttpsConnector<HttpConnector>, Body> { let https = HttpsConnector::new(4).unwrap(); Client::builder().build::<_, Body>(https) } pub fn get_categories() -> impl Future<Item = Vec<Category>, Error = Error> + Send { let uri = "https://apps.nextcloud.com/api/v1/categories.json" .parse() .unwrap(); let client = get_https_client(); client .get(uri) .and_then(|res| { res.into_body().concat2().and_then(move |body| { let apps: Vec<Category> = serde_json::from_slice(&body).unwrap(); Ok(apps) }) }) .map_err(|err| err.into()) } pub fn get_apps_and_releases( version: &String, ) -> impl Future<Item = Vec<App>, Error = Error> + Send { let raw_uri = format!( "https://apps.nextcloud.com/api/v1/platform/{}/apps.json", version ); let uri = raw_uri.parse().unwrap(); let https = hyper_tls::HttpsConnector::new(4).unwrap(); let client = hyper::Client::builder().build::<_, hyper::Body>(https); client .get(uri) .and_then(|res| { res.into_body().concat2().and_then(move |body| { let apps: Vec<App> = serde_json::from_slice(&body).unwrap(); Ok(apps) }) }) .map_err(|err| Error::from(err)) } pub fn publish_app( url: &String, is_nightly: bool, signature: &String, api_token: &String, ) -> impl Future<Item = (), Error = Error> + Send { let release = NewRelease { download: url.to_owned(), signature: signature.to_owned(), nightly: is_nightly, }; let release_json = serde_json::to_string(&release).unwrap(); let https = hyper_tls::HttpsConnector::new(4).unwrap(); let client = hyper::Client::builder().build::<_, hyper::Body>(https); let mut req = Request::builder(); req.method(Method::POST); req.uri("https://apps.nextcloud.com/api/v1/apps/releases"); req.header( hyper::header::AUTHORIZATION, hyper::header::HeaderValue::from_str(&format!("Token {}", api_token)).unwrap(), ); req.header( hyper::header::CONTENT_TYPE, hyper::header::HeaderValue::from_static("application/json"), ); req.header( hyper::header::CONTENT_LENGTH, hyper::header::HeaderValue::from_str(&release_json.len().to_string()).unwrap(), ); client .request(req.body(release_json.into()).unwrap()) .map_err(|err| Error::from(err)) .and_then(|res| match res.status() { StatusCode::OK => Ok(()), StatusCode::CREATED => Ok(()), _ => Err(format_err!( "error uploading release, got HTTP status {}", res.status() )), }) }