diff --git a/api/common/Service/TiposCambios.php b/api/common/Service/TiposCambios.php index 4628652..26f4b7b 100644 --- a/api/common/Service/TiposCambios.php +++ b/api/common/Service/TiposCambios.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use GuzzleHttp\Client; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Exception\ServerException; use ProVM\Common\Factory\Model as Factory; use Contabilidad\Moneda; use Contabilidad\TipoCambio; @@ -35,7 +36,7 @@ class TiposCambios { 'desde' => $moneda_codigo ]; $headers = [ - 'Authorization' => 'Bearer ' . $this->key + 'Authorization' => "Bearer {$this->key}" ]; $url = implode('/', [ $this->base_url, @@ -44,14 +45,19 @@ class TiposCambios { ]); try { $response = $this->client->request('POST', $url, ['json' => $data, 'headers' => $headers]); - } catch (ConnectException | RequestException $e) { + } catch (ConnectException | RequestException | ServerException $e) { error_log($e); return null; } if ($response->getStatusCode() !== 200) { + error_log('Could not connect to python API.'); return null; } $result = json_decode($response->getBody()); + if (isset($result->message) and $result->message === 'Not Authorized') { + error_log('Not authorized for connecting to python API.'); + return null; + } return $result->serie[0]->valor; } public function get(string $fecha, int $moneda_id) { @@ -62,7 +68,7 @@ class TiposCambios { } // If a value exists in the database $cambio = $moneda->cambio($fecha); - if ($cambio) { + if ($cambio !== null) { if ($cambio->desde()->id != $moneda->id) { return 1 / $cambio->valor; } diff --git a/api/src/Moneda.php b/api/src/Moneda.php index 97602a2..9b0fc81 100644 --- a/api/src/Moneda.php +++ b/api/src/Moneda.php @@ -26,7 +26,7 @@ class Moneda extends Model { $cambio = $this->factory->find(TipoCambio::class) ->where([['desde_id', $this->id], ['hasta_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]]) ->one(); - if (!$cambio) { + if ($cambio === null) { $cambio = $this->factory->find(TipoCambio::class) ->where([['hasta_id', $this->id], ['desde_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]]) ->one(); diff --git a/python/src/app.py b/python/src/app.py index a372365..4c6bc1b 100644 --- a/python/src/app.py +++ b/python/src/app.py @@ -22,7 +22,10 @@ def validate_key(request_obj): if isinstance(auth, list): auth = auth[0] if 'Bearer' in auth: - auth = auth.split(' ')[1] + try: + auth = auth.split(' ')[1] + except: + return False return auth == api_key if 'API_KEY' in request_obj.values: return request_obj.values.get('API_KEY') == api_key