Skip to content
Snippets Groups Projects
Verified Commit 2a85c588 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

[OAuth] Document full_key and get_data

parent dcaf0e1a
No related branches found
No related tags found
No related merge requests found
...@@ -45,6 +45,9 @@ use reqwest; ...@@ -45,6 +45,9 @@ use reqwest;
use serde_json; use serde_json;
use jq_rs; use jq_rs;
/// Construct a configuration key from parts
///
/// Simply joins all elements of the passed array with a '.'
fn full_key(parts: Vec<&str>) -> String { fn full_key(parts: Vec<&str>) -> String {
parts.join(".") parts.join(".")
} }
...@@ -136,17 +139,24 @@ pub fn get_access_token_password<E: Copy>(conf: &Config, prefix: &str, username: ...@@ -136,17 +139,24 @@ pub fn get_access_token_password<E: Copy>(conf: &Config, prefix: &str, username:
} }
} }
/// Retrieve text data from a configured URL endpoint
///
/// Takes the same arguments as `get_data_jq`.
fn get_data(conf: &Config, prefix: &str, endpoint: &str, param: String, token: &BasicTokenResponse) -> Result<String, Box<dyn error::Error>> { fn get_data(conf: &Config, prefix: &str, endpoint: &str, param: String, token: &BasicTokenResponse) -> Result<String, Box<dyn error::Error>> {
/// Extract token as string from deserialized access token
let access_token = token.access_token().secret(); let access_token = token.access_token().secret();
let token_type = "Bearer".to_string(); // FIXME Probably we need to handle other token types
// Retreieve endpoint URL from configuration
let mut endpoint_url: String = get_or_error(&conf, &full_key(vec![prefix, "urls", endpoint]), "")?; let mut endpoint_url: String = get_or_error(&conf, &full_key(vec![prefix, "urls", endpoint]), "")?;
endpoint_url = endpoint_url.replace("{}", &param); endpoint_url = endpoint_url.replace("{}", &param);
debug!("Loading text data from {}", endpoint_url); debug!("Loading text data from {}", endpoint_url);
// Send off request to server
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
Ok(client Ok(client
.get(&endpoint_url) .get(&endpoint_url)
.header(reqwest::header::AUTHORIZATION, format!("Bearer {}", access_token)) .header(reqwest::header::AUTHORIZATION, format!("{} {}", token_type, access_token))
.send()? .send()?
.text()?) .text()?)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment