fix status, rename get_interior_ref_list_by_shop_id

This commit is contained in:
Tyler Hallada 2020-10-24 03:02:43 -04:00
parent 1c2e01ed92
commit 32aaa29522
3 changed files with 33 additions and 27 deletions

View File

@ -161,9 +161,9 @@ FFIResult<RawInteriorRefVec> get_interior_ref_list(const char *api_url,
const char *api_key, const char *api_key,
int32_t interior_ref_list_id); int32_t interior_ref_list_id);
FFIResult<RawInteriorRefVec> get_latest_interior_ref_list_by_shop_id(const char *api_url, FFIResult<RawInteriorRefVec> get_interior_ref_list_by_shop_id(const char *api_url,
const char *api_key, const char *api_key,
int32_t shop_id); int32_t shop_id);
FFIResult<RawMerchandiseVec> get_merchandise_list(const char *api_url, FFIResult<RawMerchandiseVec> get_merchandise_list(const char *api_url,
const char *api_key, const char *api_key,

View File

@ -35,9 +35,9 @@ pub extern "C" fn status_check(api_url: *const c_char) -> FFIResult<bool> {
fn inner(api_url: &str) -> Result<Response> { fn inner(api_url: &str) -> Result<Response> {
#[cfg(not(test))] #[cfg(not(test))]
let api_url = Url::parse(api_url)?.join("status")?; let api_url = Url::parse(api_url)?.join("v1/status")?;
#[cfg(test)] #[cfg(test)]
let api_url = Url::parse(&mockito::server_url())?.join("status")?; let api_url = Url::parse(&mockito::server_url())?.join("v1/status")?;
Ok(reqwest::blocking::get(api_url)?) Ok(reqwest::blocking::get(api_url)?)
} }
@ -82,7 +82,7 @@ mod tests {
#[test] #[test]
fn test_status_check() { fn test_status_check() {
let mock = mock("GET", "/status").with_status(200).create(); let mock = mock("GET", "/v1/status").with_status(200).create();
let api_url = CString::new("url").unwrap().into_raw(); let api_url = CString::new("url").unwrap().into_raw();
let result = status_check(api_url); let result = status_check(api_url);
@ -99,7 +99,7 @@ mod tests {
#[test] #[test]
fn test_status_check_server_error() { fn test_status_check_server_error() {
let mock = mock("GET", "/status") let mock = mock("GET", "/v1/status")
.with_status(500) .with_status(500)
.with_body("Internal Server Error") .with_body("Internal Server Error")
.create(); .create();

View File

@ -259,7 +259,7 @@ pub extern "C" fn get_interior_ref_list(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn get_latest_interior_ref_list_by_shop_id( pub extern "C" fn get_interior_ref_list_by_shop_id(
api_url: *const c_char, api_url: *const c_char,
api_key: *const c_char, api_key: *const c_char,
shop_id: i32, shop_id: i32,
@ -267,26 +267,28 @@ pub extern "C" fn get_latest_interior_ref_list_by_shop_id(
let api_url = unsafe { CStr::from_ptr(api_url) }.to_string_lossy(); let api_url = unsafe { CStr::from_ptr(api_url) }.to_string_lossy();
let api_key = unsafe { CStr::from_ptr(api_key) }.to_string_lossy(); let api_key = unsafe { CStr::from_ptr(api_key) }.to_string_lossy();
info!( info!(
"get_latest_interior_ref_list_by_shop_id api_url: {:?}, api_key: {:?}, shop_id: {:?}", "get_interior_ref_list_by_shop_id api_url: {:?}, api_key: {:?}, shop_id: {:?}",
api_url, api_key, shop_id api_url, api_key, shop_id
); );
fn inner(api_url: &str, api_key: &str, shop_id: i32) -> Result<InteriorRefList> { fn inner(api_url: &str, api_key: &str, shop_id: i32) -> Result<InteriorRefList> {
#[cfg(not(test))] #[cfg(not(test))]
let url = Url::parse(api_url)? let url = Url::parse(api_url)?.join(&format!("v1/shops/{}/interior_ref_list", shop_id))?;
.join(&format!("v1/shops/{}/latest_interior_ref_list", shop_id))?;
#[cfg(test)] #[cfg(test)]
let url = Url::parse(&mockito::server_url())? let url = Url::parse(&mockito::server_url())?
.join(&format!("v1/shops/{}/latest_interior_ref_list", shop_id))?; .join(&format!("v1/shops/{}/interior_ref_list", shop_id))?;
info!("api_url: {:?}", url); info!("api_url: {:?}", url);
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let cache_path = file_cache_dir(api_url)? let cache_path =
.join(format!("latest_interior_ref_list_{}.json", shop_id)); file_cache_dir(api_url)?.join(format!("interior_ref_list_{}.json", shop_id));
match client.get(url).header("Api-Key", api_key).send() { match client.get(url).header("Api-Key", api_key).send() {
Ok(resp) => { Ok(resp) => {
info!("get_latest_interior_ref_list_by_shop_id response from api: {:?}", &resp); info!(
"get_interior_ref_list_by_shop_id response from api: {:?}",
&resp
);
if resp.status().is_success() { if resp.status().is_success() {
let bytes = resp.bytes()?; let bytes = resp.bytes()?;
update_file_cache(&cache_path, &bytes)?; update_file_cache(&cache_path, &bytes)?;
@ -298,7 +300,10 @@ pub extern "C" fn get_latest_interior_ref_list_by_shop_id(
} }
} }
Err(err) => { Err(err) => {
error!("get_latest_interior_ref_list_by_shop_id api request error: {}", err); error!(
"get_interior_ref_list_by_shop_id api request error: {}",
err
);
from_file_cache(&cache_path) from_file_cache(&cache_path)
} }
} }
@ -335,7 +340,7 @@ pub extern "C" fn get_latest_interior_ref_list_by_shop_id(
FFIResult::Ok(RawInteriorRefVec { ptr, len, cap }) FFIResult::Ok(RawInteriorRefVec { ptr, len, cap })
} }
Err(err) => { Err(err) => {
error!("get_latest_interior_ref_list_by_shop_id failed. {}", err); error!("get_interior_ref_list_by_shop_id failed. {}", err);
// TODO: how to do error handling? // TODO: how to do error handling?
let err_string = CString::new(err.to_string()) let err_string = CString::new(err.to_string())
.expect("could not create CString") .expect("could not create CString")
@ -526,8 +531,8 @@ mod tests {
} }
#[test] #[test]
fn test_get_latest_interior_ref_list_by_shop_id() { fn test_get_interior_ref_list_by_shop_id() {
let mock = mock("GET", "/v1/shops/1/latest_interior_ref_list") let mock = mock("GET", "/v1/shops/1/interior_ref_list")
.with_status(201) .with_status(201)
.with_header("content-type", "application/json") .with_header("content-type", "application/json")
.with_body( .with_body(
@ -557,7 +562,7 @@ mod tests {
let api_url = CString::new("url").unwrap().into_raw(); let api_url = CString::new("url").unwrap().into_raw();
let api_key = CString::new("api-key").unwrap().into_raw(); let api_key = CString::new("api-key").unwrap().into_raw();
let result = get_latest_interior_ref_list_by_shop_id(api_url, api_key, 1); let result = get_interior_ref_list_by_shop_id(api_url, api_key, 1);
mock.assert(); mock.assert();
match result { match result {
FFIResult::Ok(raw_interior_ref_vec) => { FFIResult::Ok(raw_interior_ref_vec) => {
@ -589,26 +594,27 @@ mod tests {
assert_eq!(raw_interior_ref.angle_z, 0.); assert_eq!(raw_interior_ref.angle_z, 0.);
assert_eq!(raw_interior_ref.scale, 1); assert_eq!(raw_interior_ref.scale, 1);
} }
FFIResult::Err(error) => panic!("get_latest_interior_ref_list_by_shop_id returned error: {:?}", unsafe { FFIResult::Err(error) => panic!(
CStr::from_ptr(error).to_string_lossy() "get_interior_ref_list_by_shop_id returned error: {:?}",
}), unsafe { CStr::from_ptr(error).to_string_lossy() }
),
} }
} }
#[test] #[test]
fn test_get_latest_interior_ref_list_by_shop_id_server_error() { fn test_get_interior_ref_list_by_shop_id_server_error() {
let mock = mock("GET", "/v1/shops/1/latest_interior_ref_list") let mock = mock("GET", "/v1/shops/1/interior_ref_list")
.with_status(500) .with_status(500)
.with_body("Internal Server Error") .with_body("Internal Server Error")
.create(); .create();
let api_url = CString::new("url").unwrap().into_raw(); let api_url = CString::new("url").unwrap().into_raw();
let api_key = CString::new("api-key").unwrap().into_raw(); let api_key = CString::new("api-key").unwrap().into_raw();
let result = get_latest_interior_ref_list_by_shop_id(api_url, api_key, 1); let result = get_interior_ref_list_by_shop_id(api_url, api_key, 1);
mock.assert(); mock.assert();
match result { match result {
FFIResult::Ok(raw_interior_ref_vec) => panic!( FFIResult::Ok(raw_interior_ref_vec) => panic!(
"get_latest_interior_ref_list_by_shop_id returned Ok result: {:#x?}", "get_interior_ref_list_by_shop_id returned Ok result: {:#x?}",
raw_interior_ref_vec raw_interior_ref_vec
), ),
FFIResult::Err(error) => { FFIResult::Err(error) => {