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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
//! Bridge to provide a client implementation for the `hyper` crate. //! //! # Examples //! //! Refer to the documentation for [`AurRequester`]. //! //! [`AurRequester`]: trait.AurRequester.html use constants::API_URI; use futures::{Future, Stream, future}; use hyper::body::Body; use hyper::client::connect::Connect; use hyper::client::Client as HyperClient; use hyper::{Request, Uri}; use model::{InfoResult, Search, SearchResult}; use serde_json; use std::fmt::{Display, Write}; use std::str::FromStr; use Error; macro_rules! ftry { ($code:expr) => { match $code { Ok(v) => v, Err(why) => return Box::new(future::err(From::from(why))), } } } /// Trait which defines the methods necessary to interact with the service. /// /// # Examples /// /// To bring in the implemenation for the `hyper` Client, simply use the /// trait: /// /// ```rust,no_run /// use aur::AurHyperRequester; /// ``` /// /// At this point, the methods will be on your Hyper Client. pub trait AurRequester { /// Retrieves information about one or more packages along with metadata. /// /// # Examples /// /// Ensure that the `"rust-nightly"` package exists: /// /// ```rust,ignore /// extern crate aur; /// extern crate hyper; /// extern crate hyper_tls; /// extern crate tokio_core; /// /// use aur::bridge::hyper::AurRequester; /// use hyper::Client; /// use hyper_tls::HttpsConnector; /// use tokio_core::Core; /// /// let core = Core::new()?; /// /// let handle = core.handle(); /// let connector = HttpsConnector::new(4, &handle)?; /// let client = Client::configure().connector(connector).build(&handle); /// /// let done = client.aur_info(&["rust-nightly"]).map(|search| { /// assert_eq!(search.result_count, 1); /// }).map_err(|_| ()); /// ``` /// /// # Errors /// /// Resolves to [`Error::Fmt`] if there was an error formatting the URI. /// /// Resolves to [`Error::Hyper`] if there was an error sending the request. /// /// Resolves to [`Error::Json`] if there was an error deserializing the /// response body. /// /// Resolves to [`Error::Uri`] if there was an error parsing the Uri. /// /// [`Error::Fmt`]: ../../enum.Error.html#variant.Fmt /// [`Error::Hyper`]: ../../enum.Error.html#variant.Hyper /// [`Error::Json`]: ../../enum.Error.html#variant.Json /// [`Error::Uri`]: ../../enum.Error.html#variant.Uri fn aur_info<T: Display>(&self, packages: &[T]) -> Box<Future<Item = Search<InfoResult>, Error = Error> + Send>; /// Searches for packages by a query, optionally filtering by maintainer /// name. /// /// # Examples /// /// Ensure that at least two packages return for the `"rust"` query, not /// specifying a maintainer: /// /// ```rust,ignore /// extern crate aur; /// extern crate hyper; /// extern crate hyper_tls; /// extern crate tokio_core; /// /// use aur::bridge::hyper::AurRequester; /// use hyper::Client; /// use hyper_tls::HttpsConnector; /// use tokio_core::Core; /// /// let handle = core.handle(); /// let connector = HttpsConnector::new(4, handle)?; /// let client = Client::configure().connector(connector).build(&handle); /// /// let done = client.aur_search(Some("rust"), None).map(|search| { /// assert!(search.result_count >= 2); /// }).map_err(|_| ()); /// ``` /// /// # Errors /// /// Resolves to [`Error::Fmt`] if there was an error formatting the URI. /// /// Resolves to [`Error::Hyper`] if there was an error sending the request. /// /// Resolves to [`Error::Json`] if there was an error deserializing the /// response body. /// /// Resolves to [`Error::Uri`] if there was an error parsing the Uri. /// /// [`Error::Fmt`]: ../../enum.Error.html#variant.Fmt /// [`Error::Hyper`]: ../../enum.Error.html#variant.Hyper /// [`Error::Json`]: ../../enum.Error.html#variant.Json /// [`Error::Uri`]: ../../enum.Error.html#variant.Uri fn aur_search(&self, query: Option<&str>, maintainer: Option<&str>) -> Box<Future<Item = Search<SearchResult>, Error = Error> + Send>; } impl<C> AurRequester for HyperClient<C, Body> where C: Connect + Sync + 'static, C::Future: 'static, C::Transport: 'static { fn aur_info<T: Display>(&self, packages: &[T]) -> Box<Future<Item = Search<InfoResult>, Error = Error> + Send + 'static> { let mut url = format!("{}&type=info", API_URI); for package in packages { if let Err(why) = write!(url, "&arg[]={}", package) { return Box::new(future::err(Error::Fmt(why))); } } let c = &url; let uri = ftry!(Uri::from_str(c)); let mut request = Request::get(uri); let req = ftry!(request.body(Body::empty())); Box::new(self.request(req) .and_then(|res| res.into_body().concat2()) .map_err(From::from) .and_then(|body| serde_json::from_slice(&body).map_err(From::from))) } fn aur_search(&self, query: Option<&str>, maintainer: Option<&str>) -> Box<Future<Item = Search<SearchResult>, Error = Error> + Send + 'static> { let mut url = format!("{}&type=search", API_URI); if let Some(query) = query { url.push_str("&arg="); url.push_str(query); } if let Some(maintainer) = maintainer { url.push_str("&maintainer="); url.push_str(maintainer); } let c = &url; let uri = ftry!(Uri::from_str(c)); let mut request = Request::get(uri); let req = ftry!(request.body(Body::empty())); Box::new(self.request(req) .and_then(|res| res.into_body().concat2()) .map_err(From::from) .and_then(|body| serde_json::from_slice(&body).map_err(From::from))) } }