FIX:Unavailable value for tipocambio crashed the loading of values

This commit is contained in:
2022-01-07 00:23:52 -03:00
parent c1eeba04a2
commit d7dfc2d221
3 changed files with 14 additions and 5 deletions

View File

@ -5,6 +5,7 @@ use Carbon\Carbon;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;
use ProVM\Common\Factory\Model as Factory; use ProVM\Common\Factory\Model as Factory;
use Contabilidad\Moneda; use Contabilidad\Moneda;
use Contabilidad\TipoCambio; use Contabilidad\TipoCambio;
@ -35,7 +36,7 @@ class TiposCambios {
'desde' => $moneda_codigo 'desde' => $moneda_codigo
]; ];
$headers = [ $headers = [
'Authorization' => 'Bearer ' . $this->key 'Authorization' => "Bearer {$this->key}"
]; ];
$url = implode('/', [ $url = implode('/', [
$this->base_url, $this->base_url,
@ -44,14 +45,19 @@ class TiposCambios {
]); ]);
try { try {
$response = $this->client->request('POST', $url, ['json' => $data, 'headers' => $headers]); $response = $this->client->request('POST', $url, ['json' => $data, 'headers' => $headers]);
} catch (ConnectException | RequestException $e) { } catch (ConnectException | RequestException | ServerException $e) {
error_log($e); error_log($e);
return null; return null;
} }
if ($response->getStatusCode() !== 200) { if ($response->getStatusCode() !== 200) {
error_log('Could not connect to python API.');
return null; return null;
} }
$result = json_decode($response->getBody()); $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; return $result->serie[0]->valor;
} }
public function get(string $fecha, int $moneda_id) { public function get(string $fecha, int $moneda_id) {
@ -62,7 +68,7 @@ class TiposCambios {
} }
// If a value exists in the database // If a value exists in the database
$cambio = $moneda->cambio($fecha); $cambio = $moneda->cambio($fecha);
if ($cambio) { if ($cambio !== null) {
if ($cambio->desde()->id != $moneda->id) { if ($cambio->desde()->id != $moneda->id) {
return 1 / $cambio->valor; return 1 / $cambio->valor;
} }

View File

@ -26,7 +26,7 @@ class Moneda extends Model {
$cambio = $this->factory->find(TipoCambio::class) $cambio = $this->factory->find(TipoCambio::class)
->where([['desde_id', $this->id], ['hasta_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]]) ->where([['desde_id', $this->id], ['hasta_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]])
->one(); ->one();
if (!$cambio) { if ($cambio === null) {
$cambio = $this->factory->find(TipoCambio::class) $cambio = $this->factory->find(TipoCambio::class)
->where([['hasta_id', $this->id], ['desde_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]]) ->where([['hasta_id', $this->id], ['desde_id', 1], ['fecha', $fecha->format('Y-m-d H:i:s')]])
->one(); ->one();

View File

@ -22,7 +22,10 @@ def validate_key(request_obj):
if isinstance(auth, list): if isinstance(auth, list):
auth = auth[0] auth = auth[0]
if 'Bearer' in auth: if 'Bearer' in auth:
auth = auth.split(' ')[1] try:
auth = auth.split(' ')[1]
except:
return False
return auth == api_key return auth == api_key
if 'API_KEY' in request_obj.values: if 'API_KEY' in request_obj.values:
return request_obj.values.get('API_KEY') == api_key return request_obj.values.get('API_KEY') == api_key