Skip to content

Commit

Permalink
chore(dev): better exception message when RAJA_ONGKIR_API_KEY is empty (
Browse files Browse the repository at this point in the history
  • Loading branch information
rizkyilhampra authored Jul 29, 2024
1 parent 68f0d8d commit 3822a40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
12 changes: 12 additions & 0 deletions app/Exceptions/EmptyRajaOngkirAPIException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class EmptyRajaOngkirAPIException extends Exception
{
protected $message = 'RajaOngkir API Key is empty. Please set the RAJAONGKIR_API_KEY in your .env file.';
}
34 changes: 23 additions & 11 deletions app/Http/Controllers/KotaProvinsiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exceptions\EmptyRajaOngkirAPIException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class KotaProvinsiController extends Controller
{
private $apiKey;

public function __construct()
{
$this->apiKey = env('RAJAONGKIR_API_KEY');
if(empty($this->apiKey)) throw new EmptyRajaOngkirAPIException();
}

public function indexProvince()
{
$cacheKey = 'provinces';
Expand All @@ -17,19 +27,20 @@ public function indexProvince()
$provinces = Cache::remember($cacheKey, $cacheTime, function () {
try {
$client = new Client();

$response = $client->request('GET', 'https://api.rajaongkir.com/starter/province', [
'headers' => [
'key' => env('RAJAONGKIR_API_KEY'),
'content-type' => 'application/x-www-form-urlencoded'
'key' => $this->apiKey,
'content-type' => 'application/x-www-form-urlencoded',
],
]);

$provinces = json_decode($response->getBody())->rajaongkir->results;
} catch (GuzzleException $e) {
// Handle any exception from Guzzle
$provinces = [$e->getMessage()];
}
Log::debug('Error: ' . json_encode($e->getMessage()));

$provinces = [];
}
return $provinces;
});

Expand All @@ -38,16 +49,16 @@ public function indexProvince()

public function indexCity(Request $request, $id)
{
$cacheKey = 'cities_' . $id;
$cacheKey = 'cities_'.$id;
$cacheTime = 60 * 60 * 24; // Cache for 24 hours

$cities = Cache::remember($cacheKey, $cacheTime, function () use ($id) {
try {
$client = new Client();
$response = $client->request('GET', 'https://api.rajaongkir.com/starter/city', [
'headers' => [
'key' => env('RAJAONGKIR_API_KEY'),
'content-type' => 'application/x-www-form-urlencoded'
'key' => $this->apiKey,
'content-type' => 'application/x-www-form-urlencoded',
],
'query' => [
'province' => $id,
Expand All @@ -56,8 +67,9 @@ public function indexCity(Request $request, $id)

$cities = json_decode($response->getBody())->rajaongkir->results;
} catch (GuzzleException $e) {
// Handle any exception from Guzzle
$cities = [$e->getMessage()];
Log::debug('Error: ' . json_encode($e->getMessage()));

$cities = [];
}

return $cities;
Expand Down

0 comments on commit 3822a40

Please sign in to comment.