Skip to content

Commit

Permalink
Add errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultamartin committed May 20, 2020
1 parent e6e9fd8 commit 4c44eb1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
37 changes: 37 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::error::Error;

pub type ClientResult<T> = std::result::Result<T, ClientError>;

#[derive(Debug)]
pub enum ClientError {
ParcelNotFound,
Unauthorized,
InvalidFormat,
ServerError,
}

impl std::fmt::Display for ClientError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
match *self {
ClientError::ParcelNotFound => f.write_str("Parcel number could not be found"),
ClientError::Unauthorized => f.write_str("Unauthorized, check your okapi key"),
ClientError::InvalidFormat => f.write_str("The parcel number doesn't correspond to La Poste format"),
ClientError::ServerError => f.write_str("Could not reach server or parse response"),
}
}
}

impl Error for ClientError {}

impl From<surf::Exception> for ClientError {
fn from(_err: surf::Exception) -> Self {
ClientError::ServerError
}
}

impl From<std::io::Error> for ClientError {
fn from(_err: std::io::Error) -> Self {
ClientError::ServerError
}
}

28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use serde::{Serialize, Deserialize};
use surf::http;
use crate::errors::ClientError::*;
use crate::errors::ClientResult;

pub mod errors;

#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -46,15 +51,26 @@ pub struct Client {
}

impl Client {
pub fn new(okapi_key: String) -> Client {
Client { okapi_key }
pub fn new(okapi_key: &str) -> Client {
Client { okapi_key: okapi_key.to_string() }
}
pub async fn get_tracking_info(&self, tracking_number: String) -> Result<TrackingInfo,surf::Exception> {
let req = surf::get(format!("{}{}","https://api.laposte.fr/suivi/v2/idships/",tracking_number))
pub async fn get_tracking_info(&self, tracking_number: &str) -> ClientResult<TrackingInfo> {
let mut response = surf::get(format!("{}{}","https://api.laposte.fr/suivi/v2/idships/",tracking_number))
.set_header("Accept", "application/json")
.set_header("X-Okapi-Key", &(self.okapi_key));
.set_header("X-Okapi-Key", &(self.okapi_key))
.await?;

match response.status() {
http::StatusCode::OK => println!("Ok"),
http::StatusCode::BAD_REQUEST => return Err(InvalidFormat),
http::StatusCode::UNAUTHORIZED => return Err(Unauthorized),
http::StatusCode::NOT_FOUND => return Err(ParcelNotFound),
_ => return Err(ServerError),
};

println!("Response status: {}", response.status());

let res: TrackingInfo = req.recv_json().await?;
let res: TrackingInfo = response.body_json().await?;
Ok(res)
}
}
Expand Down

0 comments on commit 4c44eb1

Please sign in to comment.