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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
extern crate serde; extern crate serde_json; extern crate reqwest; use hyper::Client as HttpClient; use hyper::header::{Headers, Authorization, Bearer, ContentType}; use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value}; use hyper::net::HttpsConnector; use hyper_native_tls::NativeTlsClient; use std::io::Read; use std::collections::HashMap; use std::iter::IntoIterator; pub struct Client { client: HttpClient, secret_key: String } impl Client { pub fn new(secret_key: &str) -> Client { let ssl = NativeTlsClient::new().unwrap(); let connector = HttpsConnector::new(ssl); let client = HttpClient::with_connector(connector); return Client{client: client, secret_key: secret_key.to_owned()}; } pub fn get_headers(&self) -> Headers { let mut headers = Headers::new(); headers.set( Authorization( Bearer { token: self.secret_key.to_owned() } ) ); headers.set( ContentType( Mime( TopLevel::Application, SubLevel::Json, vec![(Attr::Charset, Value::Utf8)] ) ) ); return headers; } pub fn get(&self, path: &str) -> String { let mut body_response = String::new(); let url = get_url(path); self.client.get(&url) .headers(self.get_headers()) .send() .unwrap() .read_to_string(&mut body_response) .unwrap(); return body_response; } pub fn get_filter(&self, path: &str, query_params: Option<HashMap<String, String>>, limit: Option<String>, before: Option<String>, after: Option<String> ) -> String { let mut query_url = String::from(path); query_url.push_str("?limit"); if limit.is_none() { query_url.push_str("=50"); } else { query_url.push_str(&format!("={:?}", limit)); } if !query_params.is_none() { for (k, v) in query_params.into_iter().flat_map(IntoIterator::into_iter) { query_url.push_str(&format!("&{:?}", k)); query_url.push_str(&format!("={:?}", v)); } } if !before.is_none() { query_url.push_str("&before"); query_url.push_str(&format!("={:?}", before)); } if !after.is_none() { query_url.push_str("&after"); query_url.push_str(&format!("={:?}", after)); } let mut body_response = String::new(); let url = get_url(&query_url); self.client.get(&url) .headers(self.get_headers()) .send() .unwrap() .read_to_string(&mut body_response) .unwrap(); return body_response; } pub fn post(&self, path: &str, map: &HashMap<String, serde_json::Value>) -> String { let mut body_response = String::new(); let url = get_url(path); let client_reqwest = reqwest::Client::new().unwrap(); client_reqwest.post(&url) .headers(self.get_headers()) .json(&map) .send() .unwrap() .read_to_string(&mut body_response) .unwrap(); return body_response; } pub fn delete(&self, path: &str) -> String { let mut body_response = String::new(); let url = get_url(path); self.client.delete(&url) .headers(self.get_headers()) .send() .unwrap() .read_to_string(&mut body_response) .unwrap(); return body_response; } pub fn patch(&self) { } pub fn capture(&self, path: &str) -> String { let mut body_response = String::new(); let url = get_url(path); self.client.post(&url) .headers(self.get_headers()) .send() .unwrap() .read_to_string(&mut body_response) .unwrap(); return body_response; } } fn get_url(path: &str) -> String { String::from("https://api.culqi.com/v2") + path }