Skip to content

Commit

Permalink
Merge pull request #2 from xdevplatform/errHandling
Browse files Browse the repository at this point in the history
Err handling and bug fix username flag passed without auth
  • Loading branch information
tapans authored Dec 17, 2024
2 parents c0eeef1 + 82cd8b1 commit 8bffcf5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
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

0 comments on commit 8bffcf5

Please sign in to comment.