Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify RajaOngkir API Call #1

Merged
merged 3 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/rajaongkir.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
/**
* api key yang di dapat dari akun raja ongkir
*/
'API_KEY' => env('APIKEY_RAJAONGKIR', 'SomeRandomString'),
'API_KEY' => env('RAJAONGKIR_KEY', 'somerandomstring'),

/**
* tipe akun untuk menentukan api url
* Starter, Basic, Pro
* starter, basic, pro
*/
'TIPE_AKUN' => env('TIPE_AKUN_RAJAONGKIR', 'starter')
'ACCOUNT_TYPE' => env('RAJAONGKIR_TYPE', 'starter')
];
94 changes: 28 additions & 66 deletions src/LaravelRajaongkir.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,166 +2,128 @@

namespace Kodepintar\LaravelRajaongkir;

use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;

class LaravelRajaongkir
{
private static $baseurl_starter = 'https://api.rajaongkir.com/starter';
private static $baseurl_basic = 'https://api.rajaongkir.com/basic';
private static $baseurl_pro = 'https://pro.rajaongkir.com/api';
public const BASE_URL = [
'starter' => 'https://api.rajaongkir.com/starter',
'basic' => 'https://api.rajaongkir.com/basic',
'pro' => 'https://pro.rajaongkir.com/api',
];

protected $url = null;

public function __construct()
{
$config = config('ongkir.APIKEY_RAJAONGKIR', 'starter');
$accountType = config('rajaongkir.ACCOUNT_TYPE');

switch ($config) {
case 'starter':
return $this->url = $this->baseurl_starter;
$this->url = self::BASE_URL[$accountType];

break;

case 'basic':
return $this->url = $this->baseurl_basic;

break;

case 'pro':
return $this->url = $this->baseurl_pro;

break;

default:
return $this->url = $this->baseurl_starter;

break;
}
return $this;
}

protected function requestHeader()
protected function apiCall(string $urlPath, array $payload = [], string $method = 'GET'): Response
{
$url = $this->url.'/'.ltrim($urlPath, '/');

return Http::withHeaders([
'key' => config('ongkir.APIKEY_RAJAONGKIR'),
'key' => config('rajangkir.API_KEY'),
'content-type' => 'application/x-www-form-urlencoded',
]);
])->{strtolower($method)}($url, $payload);
}

/**
* Method "province" digunakan untuk mendapatkan daftar propinsi yang ada di Indonesia.
*/
public function getProvince()
{
return $this->requestHeader()->get($this->url . '/province');
return $this->apiCall('/province');
}

/**
* Method "city" digunakan untuk mendapatkan daftar kota/kabupaten yang ada di Indonesia.
*/
public function getCity()
{
return $this->requestHeader()->get(self::$url . '/city');
return $this->apiCall('/city');
}

/**
* Method "subdistrict" digunakan untuk mendapatkan daftar kecamatan yang ada di Indonesia.
*/
public function getSubdistrict()
{
return $this->requestHeader()->get(self::$url . '/subdistrict');
return $this->apiCall('/subdistrict');
}

/**
* Method “cost” digunakan untuk mengetahui tarif pengiriman (ongkos kirim) dari dan ke kecamatan tujuan tertentu dengan berat tertentu.
*
* @param [int] $origin
* @param [string] $originType
* @param [int] $destination
* @param [string] $destinationType
* @param [int] $weight
* @param [string] $courier
* @return void
*/
public function getCost($origin, $originType, $destination, $destinationType, $weight, $courier)
public function getCost(int $origin, string $originType, int $destination, string $destinationType, int $weight, string $courier)
{
return $this->requestHeader()->post(self::$url . '/subdistrict', [
return $this->apiCall('/subdistrict', [
"origin" => $origin,
"originType" => $originType,
"destination" => $destination,
"destinationType" => $destinationType,
"weight" => $weight,
"courier" => $courier,
]);
], 'POST');
}

/**
* Method "internationalOrigin" digunakan untuk mendapatkan daftar/nama kota yang mendukung pengiriman internasional.
*
* @param [int] $internationalOrigin
* @param [int] $province
* @return void
*/
public function getInternationalOrigin($internationalOrigin, $province)
{
return $this->requestHeader()->get(self::$url . '/v2/internationalOrigin', [
return $this->apiCall('/v2/internationalOrigin', [
"internationalOrigin" => $internationalOrigin,
"province" => $province,
]);
}

/**
* Method "internationalDestination" digunakan untuk mendapatkan daftar/nama negara tujuan pengiriman internasional.
*
* @param [int] $idCountry
* @return void
*/
public function getInternationalDestination($idCountry)
{
return $this->requestHeader()->get(self::$url . '/v2/internationalDestination', [
return $this->apiCall($this->url . '/v2/internationalDestination', [
"id" => $idCountry,
]);
}

/**
* Method “internationalCost” digunakan untuk mengetahui tarif pengiriman (ongkos kirim)
* internasional dari kota-kota di Indonesia ke negara tujuan di seluruh dunia.
*
* @param [int] $origin
* @param [int] $destination
* @param [int] $weight
* @param [string] $courier
* @return void
*/
public function getInternationalCost($origin, $destination, $weight, $courier)
public function getInternationalCost(int $origin, int $destination, int $weight, string $courier)
{
return $this->requestHeader()->post(self::$url . '/v2/internationalCost', [
return $this->apiCall($this->url . '/v2/internationalCost', [
"origin" => $origin,
"destination" => $destination,
"weight" => $weight,
"courier" => $courier,
]);
], 'POST');
}

/**
* Method "currency" digunakan untuk mendapatkan informasi nilai tukar rupiah terhadap US dollar.
*/
public function getCurrency()
{
return $this->requestHeader()->post(self::$url . '/currency');
return $this->apiCall('/currency', [], 'POST');
}

/**
* Method “waybill” untuk digunakan melacak/mengetahui status pengiriman berdasarkan nomor resi.
*
* @param [string] $noResi
* @param [string] $courier
* @return void
*/
public function getTracking($noResi, $courier)
{
return $this->requestHeader()->post(self::$url . '/waybill', [
return $this->apiCall('/waybill', [
"waybill" => $noResi,
"courier" => $courier,
]);
], 'POST');
}
}
7 changes: 7 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

it('can load rajangkir config', function () {
/** @var Kodepintar\LaravelRajaongkir\Tests\TestCase $this */
$this->assertEquals(config('rajaongkir.ACCOUNT_TYPE'), 'starter');
$this->assertEquals(config('rajaongkir.API_KEY'), 'somerandomstring');
});
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.