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

Improve the ReqwestError #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 42 additions & 37 deletions meteomatics/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ impl APIClient {
match result {
Ok(response) => match response.status() {
StatusCode::OK => {
let df = parse_response_to_df(
response).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
status => Err(ConnectorError::HttpError(
Expand All @@ -106,7 +106,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -172,8 +172,8 @@ impl APIClient {
match result {
Ok(response) => match response.status() {
StatusCode::OK => {
let df = parse_response_to_df(
response).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
status => Err(ConnectorError::HttpError(
Expand All @@ -182,7 +182,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -251,11 +251,14 @@ impl APIClient {
match result {
Ok(response) => match response.status() {
StatusCode::OK => {
let mut df = parse_response_to_df(
response).await?;
df.rename("stroke_time:sql", "validdate")?;
df.rename("stroke_lat:d", "lat")?;
df.rename("stroke_lon:d", "lon")?;
let mut df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
df.rename("stroke_time:sql", "validdate")
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
df.rename("stroke_lat:d", "lat")
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
df.rename("stroke_lon:d", "lon")
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
status => Err(ConnectorError::HttpError(
Expand All @@ -264,7 +267,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -297,7 +300,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -374,13 +377,14 @@ impl APIClient {
Ok(response) => match response.status() {
StatusCode::OK => {
if needs_latlon {
let df = parse_response_to_df(
response).await?;
let df = df_add_latlon(df, coordinates.get(0).unwrap()).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
let df = df_add_latlon(df, coordinates.get(0).unwrap()).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
} else {
let df = parse_response_to_df(
response).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
}
Expand All @@ -390,7 +394,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -466,13 +470,14 @@ impl APIClient {
Ok(response) => match response.status() {
StatusCode::OK => {
if needs_latlon {
let df = parse_response_to_df(
response).await?;
let df = df_add_postal(df, postals.get(0).unwrap()).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
let df = df_add_postal(df, postals.get(0).unwrap()).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
} else {
let df = parse_response_to_df(
response).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
}
Expand All @@ -482,7 +487,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -554,8 +559,8 @@ impl APIClient {
match result {
Ok(response) => match response.status() {
StatusCode::OK => {
let df = parse_grid_response_to_df(
response).await?;
let df = parse_grid_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
status => Err(ConnectorError::HttpError(
Expand All @@ -564,7 +569,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -639,8 +644,8 @@ impl APIClient {
match result {
Ok(response) => match response.status() {
StatusCode::OK => {
let df = parse_response_to_df(
response).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
status => Err(ConnectorError::HttpError(
Expand All @@ -649,7 +654,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -726,8 +731,8 @@ impl APIClient {
match result {
Ok(response) => match response.status() {
StatusCode::OK => {
let df = parse_response_to_df(
response).await?;
let df = parse_response_to_df(response).await
.map_err(|e| ConnectorError::PolarsError(e.to_string()))?;
Ok(df)
}
status => Err(ConnectorError::HttpError(
Expand All @@ -736,7 +741,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -829,7 +834,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -917,7 +922,7 @@ impl APIClient {
status,
)),
},
Err(_) => Err(ConnectorError::ReqwestError),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

Expand Down Expand Up @@ -1000,7 +1005,7 @@ impl APIClient {
.basic_auth(&self.username, Some(String::from(&self.password)))
.send()
.await
.map_err(|_| ConnectorError::ReqwestError)
.map_err(|e| ConnectorError::ReqwestError(e.to_string()))
}
}

Expand Down
20 changes: 4 additions & 16 deletions meteomatics/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use thiserror::Error;
#[derive(Error, Debug)]
pub enum ConnectorError {
/// ReqwestError.
#[error("ReqwestError error")]
ReqwestError,
#[error("ReqwestError error: {0}")]
ReqwestError(String),

/// HTTP response error.
#[error("HTTP error: `{0}`, `{1}`, {2}`")]
Expand All @@ -15,8 +15,8 @@ pub enum ConnectorError {
LibraryError(String),

/// Polars error.
#[error("Polars error")]
PolarsError,
#[error("Polars error: `{0}`")]
PolarsError(String),

/// Generic error.
#[error(transparent)]
Expand All @@ -32,24 +32,12 @@ pub enum ConnectorError {
}


impl From<polars::error::PolarsError> for ConnectorError {
fn from(_: polars::error::PolarsError) -> Self {
ConnectorError::PolarsError
}
}

impl From<url::ParseError> for ConnectorError {
fn from(_: url::ParseError) -> Self {
ConnectorError::ParseError
}
}

impl From<reqwest::Error> for ConnectorError {
fn from(_: reqwest::Error) -> Self {
ConnectorError::ReqwestError
}
}

impl From<std::io::Error> for ConnectorError {
fn from(_: std::io::Error) -> Self {
ConnectorError::FileIOError
Expand Down
9 changes: 6 additions & 3 deletions meteomatics/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ pub struct Limit{

// Deserializes the response for the user_stats_json query.
pub async fn extract_user_statistics(response: Response) -> std::result::Result<UStatsResponse, ConnectorError> {
let json: UStatsResponse = response.json::<UStatsResponse>().await?;
Ok(json)
match response.json::<UStatsResponse>()
.await {
Ok(json) => Ok(json),
Err(e) => Err(ConnectorError::ReqwestError(e.to_string())),
}
}

/// Writes the HTTP response to a file.
Expand All @@ -123,7 +126,7 @@ pub async fn extract_user_statistics(response: Response) -> std::result::Result<
/// * `file_name` - The name for the file to be written (complete with path).
///
pub async fn write_file(response: Response, file_name: &String) -> std::result::Result<(), ConnectorError> {
let body = response.bytes().await?;
let body = response.bytes().await.map_err(|e| ConnectorError::ReqwestError(e.to_string())).unwrap();
let mut content = std::io::Cursor::new(body);

let mut file = File::create(file_name)?;
Expand Down