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
use reqwest::{self, Method, RequestBuilder}; use ApiKey; /// A client for making authenticated requests to the ExtraHop REST API. /// /// The client encapsulates the host name and API key needed to make calls to /// the appliance. #[derive(Debug)] pub struct Client { host: String, api_key: ApiKey, r_client: reqwest::Client, } impl Client { /// Creates a new client which connects to the specified host using the provided key. pub fn new<IS: Into<String>>(host: IS, api_key: ApiKey) -> Self { Client { host: host.into(), api_key: api_key, r_client: reqwest::Client::new(), } } /// Creates a new client with certificate verification disabled. This should only be used /// when a human has passed an `--insecure` flag or the like. pub fn danger_new_unverified<IS: Into<String>>(host: IS, api_key: ApiKey) -> ::Result<Self> { let r_client = reqwest::Client::builder() .danger_disable_hostname_verification() .build()?; Ok(Client { host: host.into(), api_key, r_client, }) } /// Gets the appliance's host string. pub fn host(&self) -> &str { &self.host } /// Creates a GET request builder for the provided relative path with the /// `Authorization` header included. /// /// The path should not include the `/api/v1` prefix. /// /// ```rust,no_run /// # use extrahop::{ApiKey, Client}; /// let client = Client::new("extrahop.i.northwind.com", ApiKey::new("key")); /// client.get("/whitelist/devices").send().unwrap(); /// ``` pub fn get(&self, path: &str) -> RequestBuilder { self.request(Method::Get, path) } /// Creates a POST request builder for the provided relative path with the /// `Authorization` header included. /// /// The path should not include the `/api/v1` prefix. pub fn post(&self, path: &str) -> RequestBuilder { self.request(Method::Post, path) } /// Creates a PATCH request builder for the provided relative path with the /// `Authorization` header included. /// /// The path should not include the `/api/v1` prefix. pub fn patch(&self, path: &str) -> RequestBuilder { self.request(Method::Patch, path) } /// Creates a PUT request builder for the provided relative path with the /// `Authorization` header included. /// /// The path should not include the `/api/v1` prefix. pub fn put(&self, path: &str) -> RequestBuilder { self.request(Method::Put, path) } /// Creates a DELETE request builder for the provided relative path with the /// `Authorization` header included. /// /// The path should not include the `/api/v1/` prefix. pub fn delete(&self, path: &str) -> RequestBuilder { self.request(Method::Delete, path) } /// Creates a request builder for the provided relative path with the /// `Authorization` header included. /// /// The path should not include the `/api/v1` prefix. /// /// ```rust,no_run /// # extern crate reqwest; /// # extern crate extrahop; /// # use extrahop::{ApiKey, Client}; /// # fn main() { /// use reqwest::Method; /// let client = Client::new("extrahop", ApiKey::new("key".to_string())); /// client.request(Method::Get, "/whitelist/devices").send().unwrap(); /// # } /// ``` pub fn request(&self, method: Method, path: &str) -> RequestBuilder { let leading_slash = if path.starts_with("/") { "" } else { "/" }; let mut builder = self.r_client.request( method, &format!("https://{}/api/v1{}{}", self.host, leading_slash, path), ); builder.header(self.api_key.clone().to_header()); builder } }