Skip to content

Commit

Permalink
sorcery-ai suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Jul 1, 2024
1 parent 4f482d3 commit ebc9bec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
41 changes: 21 additions & 20 deletions lib/src/tes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ impl Task {
let response = self.transport.post(&url, None).await;
// println!("the response is: {:?}",response);
match response {
Ok(resp_str) => {
let parsed_json = serde_json::from_str::<serde_json::Value>(&resp_str);
match parsed_json {
Ok(json) => Ok(json),
Err(e) => Err(Box::new(e)),
}
}
Err(e) => Err(e),
}
Ok(resp_str) => {
let parsed_json = serde_json::from_str::<serde_json::Value>(&resp_str);
match parsed_json {
Ok(json) => Ok(json),
Err(e) => Err(format!("Failed to parse JSON: {}", e).into()),
}
}
Err(e) => Err(format!("HTTP request failed: {}", e).into()),
}
}
}
#[derive(Debug)]
Expand All @@ -93,7 +93,7 @@ pub struct TES {
impl TES {
pub async fn new(config: &Configuration) -> Result<Self, Box<dyn std::error::Error>> {
let transport = Transport::new(config);
let service_info = ServiceInfo::new(config).unwrap();
let service_info = ServiceInfo::new(config)?;

let resp = service_info.get().await;

Expand All @@ -113,8 +113,10 @@ impl TES {

fn check(&self) -> bool {
let resp = &self.service;
return resp.as_ref().unwrap().r#type.artifact == "tes";
// true
match resp.as_ref() {
Ok(service) => service.r#type.artifact == "tes",
Err(_) => false, // or handle the error as appropriate
}
}

pub async fn create(
Expand Down Expand Up @@ -150,10 +152,10 @@ impl TES {
};
Ok(task)
}
Err(e) => {
log::error!("Error: {}", e);
Err(e)
}
Err(e) => Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to post task: {}", e),
))),
}
}

Expand Down Expand Up @@ -244,9 +246,8 @@ mod tests {
return Err(e);
}
};

let task_json =
std::fs::read_to_string("./lib/sample/grape.tes").expect("Unable to read file");
let file_path = std::env::var("TASK_FILE_PATH").unwrap_or_else(|_| "./lib/sample/grape.tes".to_string());
let task_json = std::fs::read_to_string(file_path).expect("Unable to read file");
let task: TesTask = serde_json::from_str(&task_json).expect("JSON was not well-formatted");

let task = tes.create(task).await?;
Expand Down Expand Up @@ -287,7 +288,7 @@ mod tests {
setup();

let (task, _tes) = &create_task().await.expect("Failed to create task");
assert!(!task.id.clone().is_empty(), "Task ID should not be empty"); // doube check if it's a correct assertion
assert!(!task.id.clone().is_empty(), "Task ID should not be empty"); // double check if it's a correct assertion

let cancel = task.cancel().await;
assert!(cancel.is_ok());
Expand Down
30 changes: 13 additions & 17 deletions lib/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,12 @@ impl Transport {
params: Option<Value>,
) -> Result<String, Box<dyn Error>> {
let full_url = format!("{}{}", self.config.base_path, endpoint);
let url = reqwest::Url::parse(&full_url);
if url.is_err() {
error!(
"Invalid endpoint (shouldn't contain base url): {}",
endpoint
);
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Invalid endpoint",
)));
}
let url = reqwest::Url::parse(&full_url).map_err(|_| {
error!("Invalid endpoint (shouldn't contain base url): {}", endpoint);
Box::new(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid endpoint")) as Box<dyn std::error::Error>
})?;

let mut request_builder = self.client.request(method, &full_url).header(
let mut request_builder = self.client.request(method, url).header(
reqwest::header::USER_AGENT,
self.config.user_agent.clone().unwrap_or_default(),
);
Expand All @@ -48,21 +41,24 @@ impl Transport {
request_builder = request_builder.query(params_value);
}

if let Some(ref data_value) = data {
request_builder = request_builder.json(&data_value);
if let Some(ref data) = data {
request_builder = request_builder.json(&data);
}

let resp = request_builder.send().await?;
let resp = request_builder.send().await.map_err(|e| {
eprintln!("HTTP request failed: {}", e);
e
})?;

let status = resp.status();
let content = resp.text().await?;
let content = resp.text().await.map_err(|e| format!("Failed to read response text: {}", e))?;

if status.is_success() {
Ok(content)
} else {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
content,
format!("Request failed with status: {}. Response: {}", status, content),
)))
}
}
Expand Down

0 comments on commit ebc9bec

Please sign in to comment.