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
mod model; use std::convert::From; pub use model::*; pub static API_ROOT: &str = "https://api.giphy.com/v1/gifs"; #[derive(Debug)] pub struct ApiError { text: String } impl From<reqwest::Error> for ApiError { fn from(error: reqwest::Error) -> Self { ApiError { text: format!("{:?}", error) } } } pub struct SearchRequest<'a> { query: &'a str, limit: Option<u32>, offset: Option<u32>, lang: Option<iso639_1::Iso639_1> } impl <'a> SearchRequest<'a> { pub fn new (query: &'a str) -> SearchRequest<'a> { SearchRequest { query, limit: None, offset: None, lang: None } } pub fn limit(&mut self, limit: u32) { self.limit = Some(limit); } pub fn offset(&mut self, offset: u32) { self.offset = Some(offset); } pub fn lang(&mut self, lang: &iso639_1::Iso639_1) { self.lang = Some(lang.clone()); } } pub struct Api { pub (crate) url: String, pub (crate) key: String } impl Api { pub fn new(url: &str, key: &str) -> Api { Api { url: url.to_string(), key: key.to_string() } } pub fn search(&self, req: &SearchRequest) -> Result<SearchResponse, ApiError> { let endpoint = format!("{}/search?api_key={}&q={}", self.url, self.key, req.query ); let search_response = reqwest::get(&endpoint) .map_err(|e| ApiError::from(e))? .error_for_status()? .json::<SearchResponse>()?; Ok(search_response) } } #[cfg(test)] mod test { use std::env; use dotenv::dotenv; use mockito::{server_url, mock, Matcher}; use super::*; #[test] fn search_request_new() { let query = "test"; let req = SearchRequest::new(query); assert_eq!(req.query, query); assert_eq!(req.offset, None); assert_eq!(req.limit, None); assert_eq!(req.lang, None); } #[test] fn search_request_offset() { let offset = 10u32; let mut req = SearchRequest::new(""); req.offset(offset); assert_eq!(req.offset, Some(offset)); } #[test] fn search_request_limit() { let limit = 10u32; let mut req = SearchRequest::new(""); req.limit(limit); assert_eq!(req.limit, Some(limit)); } #[test] fn search_request_lang() { let lang = iso639_1::Iso639_1::En; let mut req = SearchRequest::new(""); req.lang(&lang); assert_eq!(req.lang, Some(lang)); } #[test] fn api_search_200_ok() { dotenv().ok(); let api_key = env::var("GIPHY_API_KEY_TEST").unwrap(); let api_root = server_url(); let _m = mock("GET", Matcher::Regex(r"/search.*".to_string())) .with_status(200) .with_body_from_file("data/search-response-rage-200.json") .create(); let api = Api::new(&api_root, &api_key); let req = SearchRequest::new("rage"); let response = api.search(&req) .unwrap(); assert!(response.pagination.count > 0); } }