pub trait Endpoint {
type Output: DeserializeOwned;
// Required method
fn endpoint(&self) -> Cow<'static, str>;
// Provided methods
fn method(&self) -> &str { ... }
fn url(&self) -> Result<Url, HttpError> { ... }
fn ureq_client(&self, client: &Agent) -> Result<Self::Output, HttpError> { ... }
fn ureq(&self, token: &str) -> Result<Self::Output, HttpError> { ... }
fn reqwest_client(&self, client: &Client) -> Result<Self::Output, HttpError> { ... }
fn reqwest(&self, token: &str) -> Result<Self::Output, HttpError> { ... }
}Required Associated Types§
type Output: DeserializeOwned
Required Methods§
Provided Methods§
Sourcefn method(&self) -> &str
fn method(&self) -> &str
Returns the method required for this endpoint NOTE Default is GET
Examples found in repository?
examples/normal.rs (line 11)
8fn main() {
9 let api = AllowanceCheckURLBuilder::default().build().unwrap();
10 // Need to import the Endpoint trait
11 let response = ureq::request(api.method(), api.url().unwrap().as_str())
12 .set(TOKEN_KEY, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
13 .call();
14 match handle_ureq_response::<AllowanceCheck>(response) {
15 Ok(allowance) => {
16 println!(
17 "You have made {} API calls today",
18 allowance.allowance.count
19 );
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25}More examples
examples/get_status.rs (line 14)
9fn main() {
10 match get_token_from_env(None) {
11 Ok(val) => {
12 let api = EskomStatusUrlBuilder::default().build().unwrap();
13 // Need to import the Endpoint trait
14 let response = ureq::request(api.method(), api.url().unwrap().as_str())
15 .set(TOKEN_KEY, &val)
16 .call();
17 match handle_ureq_response::<EskomStatus>(response) {
18 Ok(status) => {
19 println!("{:?}", status);
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25 }
26 Err(e) => panic!("Environment variable error: {}", e),
27 }
28}examples/custom_env.rs (line 14)
9fn main() {
10 match get_token_from_env(Some("MY_CUSTOM_KEY")) {
11 Ok(val) => {
12 let api = AllowanceCheckURLBuilder::default().build().unwrap();
13 // Need to import the Endpoint trait
14 let response = ureq::request(api.method(), api.url().unwrap().as_str())
15 .set(TOKEN_KEY, &val)
16 .call();
17 match handle_ureq_response::<AllowanceCheck>(response) {
18 Ok(status) => {
19 println!("{:?}", status);
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25 }
26 Err(e) => panic!("Environment variable error: {}", e),
27 }
28}examples/area_search.rs (line 17)
9fn main() {
10 match get_token_from_env(None) {
11 Ok(val) => {
12 let api = AreaSearchURLBuilder::default()
13 .search_term("brooklyn")
14 .build()
15 .unwrap();
16 // Need to import the Endpoint trait
17 let response = ureq::request(api.method(), api.url().unwrap().as_str())
18 .set(TOKEN_KEY, &val)
19 .call();
20 match handle_ureq_response::<AreaSearch>(response) {
21 Ok(status) => {
22 println!("{:?}", status);
23 }
24 Err(e) => {
25 eprintln!("Error: {}", e);
26 }
27 }
28 }
29 Err(e) => panic!("Environment variable error: {}", e),
30 }
31}examples/area_details.rs (line 14)
6fn main() {
7 match get_token_from_env(None) {
8 Ok(val) => {
9 let api = AreaInfoURLBuilder::default()
10 .area_id("tshwane-6-brooklyn".to_owned())
11 .build()
12 .unwrap();
13 // Need to import the Endpoint trait
14 let response = ureq::request(api.method(), api.url().unwrap().as_str())
15 .set(TOKEN_KEY, &val)
16 .call();
17 match handle_ureq_response::<AreaInfo>(response) {
18 Ok(status) => {
19 println!("{:?}", status);
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25 }
26 Err(e) => panic!("Environment variable error: {}", e),
27 }
28}Sourcefn url(&self) -> Result<Url, HttpError>
fn url(&self) -> Result<Url, HttpError>
Returns the built URL for this endpoint
Examples found in repository?
examples/normal.rs (line 11)
8fn main() {
9 let api = AllowanceCheckURLBuilder::default().build().unwrap();
10 // Need to import the Endpoint trait
11 let response = ureq::request(api.method(), api.url().unwrap().as_str())
12 .set(TOKEN_KEY, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
13 .call();
14 match handle_ureq_response::<AllowanceCheck>(response) {
15 Ok(allowance) => {
16 println!(
17 "You have made {} API calls today",
18 allowance.allowance.count
19 );
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25}More examples
examples/get_status.rs (line 14)
9fn main() {
10 match get_token_from_env(None) {
11 Ok(val) => {
12 let api = EskomStatusUrlBuilder::default().build().unwrap();
13 // Need to import the Endpoint trait
14 let response = ureq::request(api.method(), api.url().unwrap().as_str())
15 .set(TOKEN_KEY, &val)
16 .call();
17 match handle_ureq_response::<EskomStatus>(response) {
18 Ok(status) => {
19 println!("{:?}", status);
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25 }
26 Err(e) => panic!("Environment variable error: {}", e),
27 }
28}examples/custom_env.rs (line 14)
9fn main() {
10 match get_token_from_env(Some("MY_CUSTOM_KEY")) {
11 Ok(val) => {
12 let api = AllowanceCheckURLBuilder::default().build().unwrap();
13 // Need to import the Endpoint trait
14 let response = ureq::request(api.method(), api.url().unwrap().as_str())
15 .set(TOKEN_KEY, &val)
16 .call();
17 match handle_ureq_response::<AllowanceCheck>(response) {
18 Ok(status) => {
19 println!("{:?}", status);
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25 }
26 Err(e) => panic!("Environment variable error: {}", e),
27 }
28}examples/area_search.rs (line 17)
9fn main() {
10 match get_token_from_env(None) {
11 Ok(val) => {
12 let api = AreaSearchURLBuilder::default()
13 .search_term("brooklyn")
14 .build()
15 .unwrap();
16 // Need to import the Endpoint trait
17 let response = ureq::request(api.method(), api.url().unwrap().as_str())
18 .set(TOKEN_KEY, &val)
19 .call();
20 match handle_ureq_response::<AreaSearch>(response) {
21 Ok(status) => {
22 println!("{:?}", status);
23 }
24 Err(e) => {
25 eprintln!("Error: {}", e);
26 }
27 }
28 }
29 Err(e) => panic!("Environment variable error: {}", e),
30 }
31}examples/area_details.rs (line 14)
6fn main() {
7 match get_token_from_env(None) {
8 Ok(val) => {
9 let api = AreaInfoURLBuilder::default()
10 .area_id("tshwane-6-brooklyn".to_owned())
11 .build()
12 .unwrap();
13 // Need to import the Endpoint trait
14 let response = ureq::request(api.method(), api.url().unwrap().as_str())
15 .set(TOKEN_KEY, &val)
16 .call();
17 match handle_ureq_response::<AreaInfo>(response) {
18 Ok(status) => {
19 println!("{:?}", status);
20 }
21 Err(e) => {
22 eprintln!("Error: {}", e);
23 }
24 }
25 }
26 Err(e) => panic!("Environment variable error: {}", e),
27 }
28}examples/normal_reqwest.rs (line 21)
9fn main() {
10 let api = AllowanceCheckURLBuilder::default().build().unwrap();
11 // Need to import the Endpoint trait
12 let mut headers = header::HeaderMap::new();
13 headers.insert(
14 TOKEN_KEY,
15 header::HeaderValue::from_str("XXXXXXXXXXXXXXXXXXXXXXX").unwrap(),
16 );
17 let client = reqwest::blocking::ClientBuilder::new()
18 .default_headers(headers)
19 .build()
20 .unwrap();
21 let response = client.get(api.url().unwrap().as_str()).send();
22 match handle_reqwest_response_blocking::<AllowanceCheck>(response) {
23 Ok(allowance) => {
24 println!(
25 "You have made {} API calls today",
26 allowance.allowance.count
27 );
28 }
29 Err(e) => {
30 eprintln!("Error: {}", e);
31 }
32 }
33}Sourcefn ureq_client(&self, client: &Agent) -> Result<Self::Output, HttpError>
fn ureq_client(&self, client: &Agent) -> Result<Self::Output, HttpError>
Uses a ureq client to make the API call and handle the response.
The assumption is made that the token is part of the default headers
Requires the ureq feature to be enabled
Sourcefn ureq(&self, token: &str) -> Result<Self::Output, HttpError>
fn ureq(&self, token: &str) -> Result<Self::Output, HttpError>
Creates a ureq client to make the API call and handle the response
Requires the ureq feature to be enabled