diff --git a/api/external_api.php b/api/external_api.php index c59a50e..748d0c3 100644 --- a/api/external_api.php +++ b/api/external_api.php @@ -75,13 +75,8 @@ class external_api extends cora\api { curl_setopt($curl_handle, CURLINFO_HEADER_OUT, true); } - $should_cache = ( - $this::$cache === true && - $this::should_cache($arguments) === true - ); - // Check the cache - if ($should_cache === true) { + if ($this::$cache === true) { $cache_key = $this->generate_cache_key($arguments); $cache_entry = $this->get_cache_entry($cache_key); } else { @@ -116,7 +111,10 @@ class external_api extends cora\api { $this->log_mysql($curl_response); } - if($should_cache === true) { + if( + $this::$cache === true && + $this::should_cache($arguments, $curl_response, $this->curl_info) === true + ) { $this->create_update_cache_entry($cache_key, $curl_response); } diff --git a/api/smarty_streets.php b/api/smarty_streets.php index 09115b9..8b21a21 100644 --- a/api/smarty_streets.php +++ b/api/smarty_streets.php @@ -58,11 +58,8 @@ class smarty_streets extends external_api { ]); $response = json_decode($curl_response, true); - if ($response === null) { - throw new Exception('Invalid JSON'); - } - if (count($response) === 0) { + if ($response === null || count($response) === 0) { return null; } else { // Smarty doesn't return this but I want it. @@ -85,14 +82,19 @@ class smarty_streets extends external_api { } /** - * Determine whether or not a request should be cached. For this, just cache - * everything. + * Determine whether or not a request should be cached. For this, cache + * valid JSON responses with status code 200. * * @param array $arguments + * @param string $curl_response + * @param array $curl_info * * @return boolean */ - protected function should_cache($arguments) { - return true; + protected function should_cache($arguments, $curl_response, $curl_info) { + return ( + $curl_info['http_code'] === 200 && + json_decode($curl_response, true) !== null + ); } }