Last active
March 9, 2022 18:08
-
-
Save josephajibodu/34b23335b6dbece01e7169e82026bac3 to your computer and use it in GitHub Desktop.
This will be the SDK for the Universe API. So that you won't stress yourself with too much details. The second file will be in the config folder. Forget about the third code for now
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Integrations; | |
use App\Models\User; | |
use Illuminate\Support\Facades\Http; | |
class Universe | |
{ | |
protected $apiKey; | |
protected $baseUrl; | |
protected $authToken; | |
protected $userEmail; | |
public function __construct() { | |
$this->initialize(); | |
} | |
public function initialize() { | |
// Get the current | |
$this->apiKey = config('universe.apiKey'); | |
$this->baseUrl = config('universe.baseUrl'); | |
$this->userEmail = config('universe.userEmail'); | |
// Login to Universe API to get the auth token | |
$this->authToken = $this->authenticate(); | |
} | |
public function getCountries() { | |
$url = $this->baseUrl . '/countries'; | |
$response = Http::withHeaders([ | |
'Authorization' => 'Bearer ' . $this->authToken, | |
'Accept' => 'application/json', | |
])->get($url)->json(); | |
return $response; | |
} | |
public function getStates(String $country) { | |
$url = $this->baseUrl . '/states/' . $country; | |
$response = Http::withHeaders([ | |
'Authorization' => 'Bearer ' . $this->authToken, | |
'Accept' => 'application/json', | |
])->get($url)->json(); | |
return $response; | |
} | |
protected function authenticate() { | |
// Get the auth token from cache | |
// $authToken = Cache::get('authToken'); | |
$url = $this->baseUrl . '/getaccesstoken'; | |
$response = Http::withHeaders([ | |
'Accept' => 'application/json', | |
'api-token' => $this->apiKey, | |
'user-email' => $this->userEmail | |
])->get($url)->json(); | |
return $response['auth_token']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'apiKey' => env('UNIVERSE_API_KEY'), | |
'baseUrl' => env('UNIVERSE_BASE_URL'), | |
'userEmail' => env('UNIVERSE_USER_EMAIL'), | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
class CreateCacheTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('cache', function($table) { | |
$table->string('key')->unique(); | |
$table->text('value'); | |
$table->integer('expiration'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('cache'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment