Skip to content

Commit

Permalink
Merge pull request #7 from cluebbehusen/cjl-validate-status
Browse files Browse the repository at this point in the history
Validate status
  • Loading branch information
cluebbehusen authored Sep 8, 2023
2 parents c3fd0fd + feba3a4 commit 3b32831
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oura-api"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"
description = "A client for the Oura V2 REST API"
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ macro_rules! generic_endpoint {
.get(&url)
.bearer_auth(&self.token)
.send()?
.error_for_status()?
.json::<$type>()?;
Ok(response)
}
Expand All @@ -85,6 +86,7 @@ macro_rules! list_endpoint {
.bearer_auth(&self.token)
.query(&query)
.send()?
.error_for_status()?
.json::<ListResponse<$type>>()?;
Ok(response)
}
Expand All @@ -101,6 +103,7 @@ macro_rules! get_endpoint {
.get(&url)
.bearer_auth(&self.token)
.send()?
.error_for_status()?
.json::<$type>()?;
Ok(response)
}
Expand Down
22 changes: 22 additions & 0 deletions tests/oura_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ fn it_applies_query_to_list_requests() {
assert!(result.is_ok());
}

#[test]
fn it_returns_error_for_error_status() {
let mut server = Server::new();
let base_url = server.url();
let client = OuraClient::build_with_base_url("token", &base_url);

let mock = server
.mock("GET", "/daily_activity")
.match_header("Authorization", "Bearer token")
.with_status(400)
.with_header("content-type", "application/json")
.create();

let result = client.list_daily_activity(get_empty_date_query());

mock.assert();
assert_eq!(
result.unwrap_err().status(),
Some(reqwest::StatusCode::BAD_REQUEST)
);
}

macro_rules! test_endpoint {
($test_name: ident, $client_function: ident, $fixture_path: literal, $url_path: literal, $type: ty, $($get_arguments: ident)?) => {
#[test]
Expand Down

0 comments on commit 3b32831

Please sign in to comment.