Skip to content

Commit

Permalink
Simplify constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 committed Aug 12, 2023
1 parent 78f9001 commit 5c71368
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "holiday_event_api"
version = "1.1.0"
version = "1.2.0"
edition = "2021"
license = "MIT"
description = "The Official Holiday and Event API for Rust."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use holiday_event_api::{
#[tokio::main]
async fn main() {
// Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
let client = HolidayEventApi::new("<your API key>".into(), None);
let client = HolidayEventApi::new("<your API key>");

if client.is_err() {
println!("{}", client.unwrap_err());
Expand Down
2 changes: 1 addition & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use holiday_event_api::{
#[tokio::main]
async fn main() {
// Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
let client = HolidayEventApi::new("<your API key>", None);
let client = HolidayEventApi::new("<your API key>");

if client.is_err() {
println!("{}", client.unwrap_err());
Expand Down
60 changes: 31 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ pub struct HolidayEventApi {
static APP_USER_AGENT: &str = concat!("HolidayApiRust/", env!("CARGO_PKG_VERSION"));

impl HolidayEventApi {
pub fn new(api_key: &str, base_url: Option<&str>) -> Result<Self, String> {
pub fn new(api_key: &str) -> Result<Self, String> {
Self::new_internal(api_key, "https://api.apilayer.com/checkiday/")
}

pub(crate) fn new_internal(api_key: &str, base_url: &str) -> Result<Self, String> {
let api_key_header = HeaderValue::try_from(api_key);
if api_key.is_empty() || api_key_header.is_err() {
return Err("Please provide a valid API key. Get one at https://apilayer.com/marketplace/checkiday-api#pricing.".into());
Expand All @@ -37,9 +41,7 @@ impl HolidayEventApi {
return Err("Error instantiating client.".into());
};

let Ok(base_url) = Url::parse(
base_url.unwrap_or("https://api.apilayer.com/checkiday/"),
) else {
let Ok(base_url) = Url::parse(base_url) else {
return Err("Invalid base_url.".into());
};

Expand Down Expand Up @@ -168,21 +170,21 @@ mod tests {

#[test]
fn fails_with_missing_api_key() {
let result = HolidayEventApi::new("", None);
let result = HolidayEventApi::new("");
assert!(result.is_err());
assert_eq!("Please provide a valid API key. Get one at https://apilayer.com/marketplace/checkiday-api#pricing.".to_string(), result.unwrap_err());
}

#[test]
fn fails_with_invalid_base_url() {
let result = HolidayEventApi::new("abc123", Some("derp"));
let result = HolidayEventApi::new_internal("abc123", "derp");
assert!(result.is_err());
assert_eq!("Invalid base_url.".to_string(), result.unwrap_err());
}

#[test]
fn returns_a_new_client() {
assert!(HolidayEventApi::new("abc123", None).is_ok());
assert!(HolidayEventApi::new("abc123").is_ok());
}
}

Expand All @@ -200,7 +202,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
assert!(aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -226,7 +228,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
assert!(aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -249,7 +251,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
assert!(aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -271,7 +273,7 @@ mod tests {
.with_body("{\"error\":\"MyError!\"}")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -293,7 +295,7 @@ mod tests {
.with_status(500)
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -315,7 +317,7 @@ mod tests {
.with_status(599)
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -330,7 +332,7 @@ mod tests {
#[test]
fn server_error_other() {
let fake_url = "http://localhost";
let api = HolidayEventApi::new("abc123", Some(fake_url)).unwrap();
let api = HolidayEventApi::new_internal("abc123", fake_url).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -356,7 +358,7 @@ mod tests {
.with_body("{")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand Down Expand Up @@ -386,7 +388,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
assert!(aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand All @@ -410,7 +412,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand Down Expand Up @@ -443,7 +445,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: None,
adult: None,
Expand Down Expand Up @@ -511,7 +513,7 @@ mod tests {
.with_body_from_file("testdata/getEvents-parameters.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_events(model::GetEventsRequest {
date: Some("now".into()),
adult: Some(true),
Expand Down Expand Up @@ -566,7 +568,7 @@ mod tests {
.with_body_from_file("testdata/getEventInfo-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_event_info(model::GetEventInfoRequest {
id: "f90b893ea04939d7456f30c54f68d7b4".into(),
start: None,
Expand Down Expand Up @@ -675,7 +677,7 @@ mod tests {
.with_body_from_file("testdata/getEventInfo-parameters.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_event_info(model::GetEventInfoRequest {
id: "f90b893ea04939d7456f30c54f68d7b4".into(),
start: Some(2002),
Expand Down Expand Up @@ -767,7 +769,7 @@ mod tests {
.with_body_from_file("testdata/getEventInfo-starter.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_event_info(model::GetEventInfoRequest {
id: "1a85c01ea2a6e3f921667c59391aa7ee".into(),
start: None,
Expand Down Expand Up @@ -817,7 +819,7 @@ mod tests {
.with_body("{\"error\":\"Event not found.\"}")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.get_event_info(model::GetEventInfoRequest {
id: "hi".into(),
start: None,
Expand All @@ -832,7 +834,7 @@ mod tests {

#[test]
fn missing_id() {
let api = HolidayEventApi::new("abc123", None).unwrap();
let api = HolidayEventApi::new("abc123").unwrap();
let result = aw!(api.get_event_info(model::GetEventInfoRequest {
id: "".into(),
start: None,
Expand All @@ -857,7 +859,7 @@ mod tests {
.with_body_from_file("testdata/search-default.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.search(model::SearchRequest {
query: "zucchini".into(),
adult: None,
Expand Down Expand Up @@ -896,7 +898,7 @@ mod tests {
.with_body_from_file("testdata/search-parameters.json")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.search(model::SearchRequest {
query: "porch day".into(),
adult: Some(true),
Expand Down Expand Up @@ -930,7 +932,7 @@ mod tests {
.with_body("{\"error\":\"Please enter a longer search term.\"}")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.search(model::SearchRequest {
query: "a".into(),
adult: None,
Expand All @@ -953,7 +955,7 @@ mod tests {
.with_body("{\"error\":\"Too many results returned. Please refine your query.\"}")
.create();

let api = HolidayEventApi::new("abc123", Some(&server.url())).unwrap();
let api = HolidayEventApi::new_internal("abc123", &server.url()).unwrap();
let result = aw!(api.search(model::SearchRequest {
query: "day".into(),
adult: None,
Expand All @@ -970,7 +972,7 @@ mod tests {

#[test]
fn missing_parameters() {
let api = HolidayEventApi::new("abc123", None).unwrap();
let api = HolidayEventApi::new("abc123").unwrap();
let result = aw!(api.search(model::SearchRequest {
query: "".into(),
adult: None,
Expand Down

0 comments on commit 5c71368

Please sign in to comment.