161 lines
4.7 KiB
Plaintext
161 lines
4.7 KiB
Plaintext
#[allow(unused_imports)]
|
|
use progenitor_client::{encode_path, RequestBuilderExt};
|
|
pub use progenitor_client::{ByteStream, Error, ResponseValue};
|
|
#[allow(unused_imports)]
|
|
use reqwest::header::{HeaderMap, HeaderValue};
|
|
pub mod types {
|
|
use serde::{Deserialize, Serialize};
|
|
#[allow(unused_imports)]
|
|
use std::convert::TryFrom;
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
///Client for Parameter override test
|
|
///
|
|
///Minimal API for testing parameter overrides
|
|
pub struct Client {
|
|
pub(crate) baseurl: String,
|
|
pub(crate) client: reqwest::Client,
|
|
}
|
|
|
|
impl Client {
|
|
/// Create a new client.
|
|
///
|
|
/// `baseurl` is the base URL provided to the internal
|
|
/// `reqwest::Client`, and should include a scheme and hostname,
|
|
/// as well as port and a path stem if applicable.
|
|
pub fn new(baseurl: &str) -> Self {
|
|
let dur = std::time::Duration::from_secs(15);
|
|
let client = reqwest::ClientBuilder::new()
|
|
.connect_timeout(dur)
|
|
.timeout(dur)
|
|
.build()
|
|
.unwrap();
|
|
Self::new_with_client(baseurl, client)
|
|
}
|
|
|
|
/// Construct a new client with an existing `reqwest::Client`,
|
|
/// allowing more control over its configuration.
|
|
///
|
|
/// `baseurl` is the base URL provided to the internal
|
|
/// `reqwest::Client`, and should include a scheme and hostname,
|
|
/// as well as port and a path stem if applicable.
|
|
pub fn new_with_client(baseurl: &str, client: reqwest::Client) -> Self {
|
|
Self {
|
|
baseurl: baseurl.to_string(),
|
|
client,
|
|
}
|
|
}
|
|
|
|
/// Return the base URL to which requests are made.
|
|
pub fn baseurl(&self) -> &String {
|
|
&self.baseurl
|
|
}
|
|
|
|
/// Return the internal `reqwest::Client` used to make requests.
|
|
pub fn client(&self) -> &reqwest::Client {
|
|
&self.client
|
|
}
|
|
}
|
|
|
|
impl Client {
|
|
///Gets a key
|
|
///
|
|
///Sends a `GET` request to `/key`
|
|
///
|
|
///Arguments:
|
|
/// - `key`: The same key parameter that overlaps with the path level
|
|
/// parameter
|
|
/// - `unique_key`: A key parameter that will not be overridden by the path
|
|
/// spec
|
|
///```ignore
|
|
/// let response = client.key_get()
|
|
/// .key(key)
|
|
/// .unique_key(unique_key)
|
|
/// .send()
|
|
/// .await;
|
|
/// ```
|
|
pub fn key_get(&self) -> builder::KeyGet {
|
|
builder::KeyGet::new(self)
|
|
}
|
|
}
|
|
|
|
pub mod builder {
|
|
use super::types;
|
|
#[allow(unused_imports)]
|
|
use super::{
|
|
encode_path, ByteStream, Error, HeaderMap, HeaderValue, RequestBuilderExt, ResponseValue,
|
|
};
|
|
///Builder for [`Client::key_get`]
|
|
///
|
|
///[`Client::key_get`]: super::Client::key_get
|
|
#[derive(Debug, Clone)]
|
|
pub struct KeyGet<'a> {
|
|
client: &'a super::Client,
|
|
key: Result<Option<bool>, String>,
|
|
unique_key: Result<Option<String>, String>,
|
|
}
|
|
|
|
impl<'a> KeyGet<'a> {
|
|
pub fn new(client: &'a super::Client) -> Self {
|
|
Self {
|
|
client,
|
|
key: Ok(None),
|
|
unique_key: Ok(None),
|
|
}
|
|
}
|
|
|
|
pub fn key<V>(mut self, value: V) -> Self
|
|
where
|
|
V: std::convert::TryInto<bool>,
|
|
{
|
|
self.key = value
|
|
.try_into()
|
|
.map(Some)
|
|
.map_err(|_| "conversion to `bool` for key failed".to_string());
|
|
self
|
|
}
|
|
|
|
pub fn unique_key<V>(mut self, value: V) -> Self
|
|
where
|
|
V: std::convert::TryInto<String>,
|
|
{
|
|
self.unique_key = value
|
|
.try_into()
|
|
.map(Some)
|
|
.map_err(|_| "conversion to `String` for unique_key failed".to_string());
|
|
self
|
|
}
|
|
|
|
///Sends a `GET` request to `/key`
|
|
pub async fn send(self) -> Result<ResponseValue<()>, Error<()>> {
|
|
let Self {
|
|
client,
|
|
key,
|
|
unique_key,
|
|
} = self;
|
|
let key = key.map_err(Error::InvalidRequest)?;
|
|
let unique_key = unique_key.map_err(Error::InvalidRequest)?;
|
|
let url = format!("{}/key", client.baseurl,);
|
|
let mut query = Vec::with_capacity(2usize);
|
|
if let Some(v) = &key {
|
|
query.push(("key", v.to_string()));
|
|
}
|
|
if let Some(v) = &unique_key {
|
|
query.push(("uniqueKey", v.to_string()));
|
|
}
|
|
let request = client.client.get(url).query(&query).build()?;
|
|
let result = client.client.execute(request).await;
|
|
let response = result?;
|
|
match response.status().as_u16() {
|
|
200u16 => Ok(ResponseValue::empty(response)),
|
|
_ => Err(Error::UnexpectedResponse(response)),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub mod prelude {
|
|
pub use self::super::Client;
|
|
}
|