Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Err handling and bug fix username flag passed without auth #2

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ impl ApiClient {
// this will allow the user to not have to specify the auth type for each request and
// xurl will be able to choose the correct auth type based on the route
let token = {
let auth_ref = auth.borrow();
auth_ref.first_oauth2_token()
let mut auth_ref = auth.borrow_mut();
if let Some(username) = username {
auth_ref.get_token_store().get_oauth2_token(username)
} else {
auth_ref.first_oauth2_token()
}
};
if let Some(token) = token {
match token {
Expand Down Expand Up @@ -117,7 +121,7 @@ impl ApiClient {
}
}

pub async fn build_request(
pub async fn build_request(
&self,
method: &str,
endpoint: &str,
Expand Down Expand Up @@ -174,20 +178,38 @@ impl ApiClient {
data: Option<&str>,
auth_type: Option<&str>,
username: Option<&str>,
verbose: bool,
) -> Result<serde_json::Value, Error> {
let request_builder = self
.build_request(method, endpoint, headers, data, auth_type, username)
.await?;

let req = request_builder.try_clone().unwrap().build()?;
let response = request_builder.send().await?;

if verbose {
println!("Request: {:#?}", req);
println!("Response: {:#?}", response)
}

let status = response.status();
let body: Value = response.json().await?;

if !status.is_success() {
return Err(Error::ApiError(body));
match response.json::<serde_json::Value>().await {
Ok(res) => {
if !status.is_success() {
Err(Error::ApiError(res))
} else {
Ok(res)
}
},
Err(_) => {
let status = status.to_string();
Err(Error::ApiError(serde_json::json!({
"status": status,
"error": "Empty body"
})))
}
}

Ok(body)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub struct Cli {
/// Username for OAuth2 authentication
#[arg(short, long)]
pub username: Option<String>,

/// Print verbose information
#[arg(short, long)]
pub verbose: bool,
}

#[derive(Subcommand)]
Expand Down
22 changes: 20 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,35 @@ async fn main() -> Result<(), Error> {
let client = ApiClient::new(config).with_auth(auth);

// Make the request
let response = client
let response = match client
.send_request(
cli.method.as_deref().unwrap_or("GET"),
&url,
&cli.headers,
cli.data.as_deref(),
cli.auth.as_deref(),
cli.username.as_deref(),
cli.verbose,
)
.await?;
.await {
Ok(res) => res,
Err(e) => match e {
Error::ApiError(e) => {
println!("{}", serde_json::to_string_pretty(&e)?);
std::process::exit(1)
},
Error::HttpError(e) => {
println!("{}", e);
std::process::exit(1)
},
_ => {
println!("{}", e);
std::process::exit(1)
}
}
};


// Pretty print the response
println!("{}", serde_json::to_string_pretty(&response)?);

Expand Down
Loading