chore: refactor endpoint getters

This commit is contained in:
əlemi 2024-10-20 01:47:17 +02:00
parent 6915a71802
commit b82a63eeb7
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 56 additions and 31 deletions

View file

@ -7,11 +7,8 @@ pub enum PostWomanError {
#[error("invalid method: {0:?}")] #[error("invalid method: {0:?}")]
InvalidMethod(#[from] http::method::InvalidMethod), InvalidMethod(#[from] http::method::InvalidMethod),
#[error("invalid header name: {0:?}")] #[error("invalid header: {0:?}")]
InvalidHeaderName(#[from] reqwest::header::InvalidHeaderName), InvalidHeader(#[from] InvalidHeaderError),
#[error("invalid header value: {0:?}")]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
#[error("contains Unprintable characters: {0:?}")] #[error("contains Unprintable characters: {0:?}")]
Unprintable(#[from] reqwest::header::ToStrError), Unprintable(#[from] reqwest::header::ToStrError),
@ -19,9 +16,6 @@ pub enum PostWomanError {
#[error("header '{0}' not found in response")] #[error("header '{0}' not found in response")]
HeaderNotFound(String), HeaderNotFound(String),
#[error("invalid header: '{0}'")]
InvalidHeader(String),
#[error("error opening collection: {0:?}")] #[error("error opening collection: {0:?}")]
ErrorOpeningCollection(#[from] std::io::Error), ErrorOpeningCollection(#[from] std::io::Error),
@ -46,3 +40,13 @@ pub enum PostWomanError {
#[error("regex failed matching in content: {0}")] #[error("regex failed matching in content: {0}")]
NoMatch(String), NoMatch(String),
} }
#[derive(Debug, thiserror::Error)]
pub enum InvalidHeaderError {
#[error("invalid header name: {0:?}")]
Name(#[from] http::header::InvalidHeaderName),
#[error("invalid header value: {0:?}")]
Value(#[from] http::header::InvalidHeaderValue),
#[error("invalid header format: {0}")]
Format(String)
}

View file

@ -1,9 +1,12 @@
use std::{collections::HashMap, str::FromStr}; use std::{collections::HashMap, str::FromStr};
use base64::{prelude::BASE64_STANDARD, Engine}; use base64::{prelude::BASE64_STANDARD, Engine};
use http::header::{InvalidHeaderName, InvalidHeaderValue};
use http::method::InvalidMethod;
use http::{HeaderMap, HeaderName, HeaderValue}; use http::{HeaderMap, HeaderName, HeaderValue};
use jaq_interpret::FilterT; use jaq_interpret::FilterT;
use crate::errors::InvalidHeaderError;
use crate::{PostWomanError, APP_USER_AGENT}; use crate::{PostWomanError, APP_USER_AGENT};
use crate::ext::{stringify_toml, stringify_json, StringOr}; use crate::ext::{stringify_toml, stringify_json, StringOr};
@ -29,6 +32,41 @@ pub struct EndpointConfig {
} }
impl EndpointConfig { impl EndpointConfig {
pub fn body(&mut self) -> Result<String, serde_json::Error> {
match self.body.take().unwrap_or_default() {
StringOr::Str(x) => Ok(x.clone()),
StringOr::T(json) => Ok(serde_json::to_string(&json)?),
}
}
pub fn method(&mut self) -> Result<reqwest::Method, InvalidMethod> {
match self.method {
Some(ref m) => Ok(reqwest::Method::from_str(m)?),
None => Ok(reqwest::Method::GET),
}
}
pub fn headers(&mut self) -> Result<HeaderMap, InvalidHeaderError> {
let mut headers = HeaderMap::default();
for header in self.headers.take().unwrap_or_default() {
let (k, v) = header.split_once(':')
.ok_or_else(|| InvalidHeaderError::Format(header.clone()))?;
headers.insert(
HeaderName::from_str(k)?,
HeaderValue::from_str(v)?
);
}
Ok(headers)
}
pub fn url(&mut self) -> String {
let mut url = self.url.clone();
if let Some(query) = self.query.take() {
url = format!("{url}?{}", query.join("&"));
}
url
}
pub fn fill(mut self, env: &toml::Table) -> Self { pub fn fill(mut self, env: &toml::Table) -> Self {
let mut vars: HashMap<String, String> = HashMap::default(); let mut vars: HashMap<String, String> = HashMap::default();
@ -80,29 +118,11 @@ impl EndpointConfig {
self self
} }
pub async fn execute(self, opts: &ClientConfig) -> Result<String, PostWomanError> { pub async fn execute(mut self, opts: &ClientConfig) -> Result<String, PostWomanError> {
let method = match self.method { let url = self.url();
Some(m) => reqwest::Method::from_str(&m)?, let body = self.body()?;
None => reqwest::Method::GET, let method = self.method()?;
}; let headers = self.headers()?;
let mut headers = HeaderMap::default();
for header in self.headers.unwrap_or_default() {
let (k, v) = header.split_once(':')
.ok_or_else(|| PostWomanError::InvalidHeader(header.clone()))?;
headers.insert(
HeaderName::from_str(k)?,
HeaderValue::from_str(v)?
);
}
let body = match self.body.unwrap_or_default() {
StringOr::Str(x) => x,
StringOr::T(json) => serde_json::to_string(&json)?,
};
let mut url = self.url;
if let Some(query) = self.query {
url = format!("{url}?{}", query.join("&"));
}
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
.user_agent(opts.user_agent.as_deref().unwrap_or(APP_USER_AGENT)) .user_agent(opts.user_agent.as_deref().unwrap_or(APP_USER_AGENT))
@ -111,6 +131,7 @@ impl EndpointConfig {
.danger_accept_invalid_certs(opts.accept_invalid_certs.unwrap_or(false)) .danger_accept_invalid_certs(opts.accept_invalid_certs.unwrap_or(false))
.build()?; .build()?;
let res = client let res = client
.request(method, url) .request(method, url)
.headers(headers) .headers(headers)