add auth
This commit is contained in:
@@ -10,6 +10,35 @@ struct HttpResponse {
|
||||
time_elapsed: u128, // milliseconds
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct BasicAuth {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct BearerAuth {
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct ApiKeyAuth {
|
||||
key: String,
|
||||
value: String,
|
||||
#[serde(rename = "addTo")]
|
||||
add_to: String, // "header" or "query"
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct AuthConfig {
|
||||
#[serde(rename = "type")]
|
||||
auth_type: String, // "none", "basic", "bearer", "api_key"
|
||||
basic: BasicAuth,
|
||||
bearer: BearerAuth,
|
||||
#[serde(rename = "apiKey")]
|
||||
api_key: ApiKeyAuth,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn execute_request(
|
||||
method: String,
|
||||
@@ -17,6 +46,7 @@ async fn execute_request(
|
||||
headers: HashMap<String, String>,
|
||||
body: Option<String>,
|
||||
query_params: Option<HashMap<String, String>>,
|
||||
auth: Option<AuthConfig>,
|
||||
) -> Result<HttpResponse, String> {
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(30))
|
||||
@@ -37,6 +67,31 @@ async fn execute_request(
|
||||
request_builder = request_builder.header(key, value);
|
||||
}
|
||||
|
||||
// Add Auth
|
||||
if let Some(auth_config) = auth {
|
||||
match auth_config.auth_type.as_str() {
|
||||
"basic" => {
|
||||
request_builder = request_builder.basic_auth(
|
||||
auth_config.basic.username,
|
||||
Some(auth_config.basic.password),
|
||||
);
|
||||
}
|
||||
"bearer" => {
|
||||
request_builder = request_builder.bearer_auth(auth_config.bearer.token);
|
||||
}
|
||||
"api_key" => {
|
||||
let key = auth_config.api_key.key;
|
||||
let value = auth_config.api_key.value;
|
||||
if auth_config.api_key.add_to == "header" {
|
||||
request_builder = request_builder.header(key, value);
|
||||
} else if auth_config.api_key.add_to == "query" {
|
||||
request_builder = request_builder.query(&[(key, value)]);
|
||||
}
|
||||
}
|
||||
_ => {} // "none" or unknown
|
||||
}
|
||||
}
|
||||
|
||||
// Add Body
|
||||
if let Some(b) = body {
|
||||
if !b.is_empty() {
|
||||
|
||||
Reference in New Issue
Block a user