Skip to content

Commit

Permalink
update region
Browse files Browse the repository at this point in the history
  • Loading branch information
daniwebdev committed Oct 31, 2023
1 parent 10f11de commit 2e07266
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 58 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
}
},
"scripts": {
"pest": "./vendor/bin/pest"
"pest": "./vendor/bin/pest",
"test-region": "./vendor/bin/pest tests/IndonesianRegionTest.php",
"test-idx": "./vendor/bin/pest tests/StockIDXTest.php"
}
}
43 changes: 24 additions & 19 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,22 @@
use GuzzleHttp\Client as GuzzleHttpClient;

class Client {

const API_BASE_PATH = 'https://api.goapi.io';
const API_VERSION = '';

private $config;
const API_VERSION = '';

private $config;
private $http;

function __construct(array $config=[])
public function __construct(array $config = [])
{
$this->config = array_merge([
'base_uri' => self::API_BASE_PATH.'/'.self::API_VERSION.'/',
'api_key' => '',
'base_uri' => self::API_BASE_PATH . '/' . self::API_VERSION . '/',
'api_key' => '',
], $config);

}

function getHttpClient() {
if(!isset($this->http)) {
$this->http = $this->createHttpClient();
}

return $this->http;
}

protected function createHttpClient() {
private function createHttpClient()
{
$options = [
'base_uri' => $this->config['base_uri'],
'headers' => [
Expand All @@ -40,8 +30,23 @@ protected function createHttpClient() {
return new GuzzleHttpClient($options);
}

function createStockIDX(): \GOAPI\IO\StockIDX {
public function getHttpClient()
{
if (!isset($this->http)) {
$this->http = $this->createHttpClient();
}

return $this->http;
}


public function createStockIDX()
{
return new \GOAPI\IO\StockIDX($this->getHttpClient());
}


public function createIndonesianRegion()
{
return new \GOAPI\IO\IndonesianRegion($this->getHttpClient());
}
}
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function filter(callable $callback = null)

public function values(): array
{
return $this->items;
return array_values($this->items);
}

public function take($count)
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace GOAPI\IO\Exceptions;
use GuzzleHttp\Exception\GuzzleException;

class RequestException extends GuzzleException {

}
36 changes: 24 additions & 12 deletions src/IndonesianRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,42 @@ private function makeRequest($endpoint, $params = []) {
]);

// Assuming the API returns JSON response
return json_decode($response->getBody());
return json_decode($response->getBody(), true);
} catch (\Exception $e) {
// Handle exceptions and errors here
// You might want to log the error or throw a custom exception
return ["error" => $e->getMessage()];
}
}

public function getProvince() {
$endpoint = "/province";
return $this->makeRequest($endpoint);
public function getProvinsi() {
$endpoint = "/provinsi";
return (new Collection($this->makeRequest($endpoint)['data']))->map(function($item) {
return new \GOAPI\IO\Resources\Region\Region($item['id'], $item['name']);
});
}

public function getCity($provinceId) {
$endpoint = "/city";
$params = ["province_id" => $provinceId];
return $this->makeRequest($endpoint, $params);
public function getKota($provinsiId) {
$endpoint = "/kota";
$params = ["provinsi_id" => $provinsiId];
return (new Collection($this->makeRequest($endpoint, $params)['data']))->map(function($item) {
return new \GOAPI\IO\Resources\Region\Region($item['id'], $item['name']);
});
}

public function getSubDistrict() {

public function getKecamatan($kotaId) {
$endpoint = "/kecamatan";
$params = ["kota_id" => $kotaId];
return (new Collection($this->makeRequest($endpoint, $params)['data']))->map(function($item) {
return new \GOAPI\IO\Resources\Region\Region($item['id'], $item['name']);
});
}

public function getVillage() {

public function getKelurahan($kecamatanId) {
$endpoint = "/kelurahan";
$params = ["kecamatan_id" => $kecamatanId];
return (new Collection($this->makeRequest($endpoint, $params)['data']))->map(function($item) {
return new \GOAPI\IO\Resources\Region\Region($item['id'], $item['name']);
});
}
}
17 changes: 17 additions & 0 deletions src/Resources/Region/Region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace GOAPI\IO\Resources\Region;

class Region {

/**
* Constructor for the class.
*
* @param mixed $code The code parameter.
* @param mixed $name The name parameter.
*/
function __construct(public $id, public $name)
{

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GoAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

class Broker {
public $code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

class BrokerSummary
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

use Psr\Http\Message\ResponseInterface;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

class CompanyProfile {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;


class StockIndex {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

class StockIndicator {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

use Psr\Http\Message\ResponseInterface;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace GOAPI\IO\Resources;
namespace GOAPI\IO\Resources\Stock;

use Psr\Http\Message\ResponseInterface;

Expand Down
20 changes: 10 additions & 10 deletions src/StockIDX.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ private function makeRequest($endpoint, $params = []) {
public function getCompanies() {
$endpoint = "/companies";
return (new Collection($this->makeRequest($endpoint)['data']['results']))->map(function($item) {
return new \GOAPI\IO\Resources\Company($item['symbol'], $item['name'], $item['logo']);
return new \GOAPI\IO\Resources\Stock\Company($item['symbol'], $item['name'], $item['logo']);
});
}

public function getProfile($symbol) {
$endpoint = "/$symbol/profile";

if($this->makeRequest($endpoint)['data']) {
return \GOAPI\IO\Resources\CompanyProfile::fromArray($this->makeRequest($endpoint)['data']);
return \GOAPI\IO\Resources\Stock\CompanyProfile::fromArray($this->makeRequest($endpoint)['data']);
}

}
Expand All @@ -57,35 +57,35 @@ public function getStockPrices(array $symbols) {
$endpoint = "/prices";
$params = ["symbols" => implode(',', $symbols)];
return (new Collection($this->makeRequest($endpoint, $params)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockPrice::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockPrice::fromArray($item);
});
}

public function getTrendingStocks() {
$endpoint = "/trending";
return (new Collection($this->makeRequest($endpoint)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockPriceChange::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockPriceChange::fromArray($item);
});
}

public function getTopGainerStocks() {
$endpoint = "/top_gainer";
return (new Collection($this->makeRequest($endpoint)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockPriceChange::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockPriceChange::fromArray($item);
});
}

public function getTopLoserStocks() {
$endpoint = "/top_loser";
return (new Collection($this->makeRequest($endpoint)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockPriceChange::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockPriceChange::fromArray($item);
});
}

public function getIndices() {
$endpoint = "/indices";
return (new Collection($this->makeRequest($endpoint)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockPriceChange::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockPriceChange::fromArray($item);
});
}

Expand All @@ -100,7 +100,7 @@ public function getHistoricalData($symbol, $from = null, $to = null) {
}

return (new Collection($this->makeRequest($endpoint)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockPrice::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockPrice::fromArray($item);
});
}

Expand All @@ -114,7 +114,7 @@ public function getBrokerSummary($symbol, $date) {
$params = ["date" => $date];

return (new Collection($this->makeRequest($endpoint, $params)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\BrokerSummary::fromArray($item);
return \GOAPI\IO\Resources\Stock\BrokerSummary::fromArray($item);
});
}

Expand All @@ -129,7 +129,7 @@ public function getStockIndicators($page = 1, $date = null) {
}

return (new Collection($this->makeRequest($endpoint, $params)['data']['results']))->map(function($item) {
return \GOAPI\IO\Resources\StockIndicator::fromArray($item);
return \GOAPI\IO\Resources\Stock\StockIndicator::fromArray($item);
});
}
}
49 changes: 49 additions & 0 deletions tests/IndonesianRegionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


$client = new \GOAPI\IO\Client([
'api_key' => getenv('GOAPI_IO_API_KEY'), // your api key
]);

test('get client instance', function () use($client) {
$region = $client->createIndonesianRegion();

expect($region instanceof \GOAPI\IO\IndonesianRegion)->toBeTrue();
});

test('get provinsi', function () use($client) {
$region = $client->createIndonesianRegion();

$response = $region->getProvinsi();

expect($response->values())->toBeArray();
expect($response->values()[0] instanceof \GOAPI\IO\Resources\Region\Region)->toBeTrue();
});

test('get kota', function () use($client) {
$region = $client->createIndonesianRegion();

$response = $region->getKota('11');


expect($response->values())->toBeArray();
expect($response->values()[0] instanceof \GOAPI\IO\Resources\Region\Region)->toBeTrue();
});

test('get kecamatan', function () use($client) {
$region = $client->createIndonesianRegion();

$response = $region->getKecamatan('11.01');

expect($response->values())->toBeArray();
expect($response->values()[0] instanceof \GOAPI\IO\Resources\Region\Region)->toBeTrue();
});

test('get kelurahan', function () use($client) {
$region = $client->createIndonesianRegion();

$response = $region->getKelurahan('11.01.01');

expect($response->values())->toBeArray();
expect($response->values()[0] instanceof \GOAPI\IO\Resources\Region\Region)->toBeTrue();
});
15 changes: 8 additions & 7 deletions tests/StockIDXTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
expect($companies->count() > 0)->toBeTrue();
});

test('get company profile', function() use($client) {
$stockIDX = $client->createStockIDX();
$response = $stockIDX->getProfile(symbol: 'BBCA');

expect($response instanceof \GOAPI\IO\Resources\Stock\CompanyProfile)->toBeTrue();
expect($response->symbol == 'BBCA')->toBeTrue();
});

test('get stock price', function() use($client) {
$stockIDX = $client->createStockIDX();
$symbols = ['BBCA'];
Expand Down Expand Up @@ -73,10 +81,3 @@

expect($results instanceof \GOAPI\IO\Collection)->toBeTrue();
});

test('get company profile', function() use($client) {
$stockIDX = $client->createStockIDX();
$response = $stockIDX->getProfile(symbol: 'BBCA');

expect($response instanceof \GOAPI\IO\Resources\CompanyProfile)->toBeTrue();
});

0 comments on commit 2e07266

Please sign in to comment.