|
- <?php
-
- namespace App\Util\Custom\HelloTechno;
-
- use App\Exceptions\AppCommonException;
- use Exception;
- use Illuminate\Http\Client\Response;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
-
- class API
- {
-
- private const RESULT_CODE = 'result_code';
- private const RESULT_CODE_SUCCESS = 'SUCCESS';
- private const FIELD_DATA = 'data';
-
-
- private const URL_CUSTOMERS = 'customers';
- private const URL_PARKINGS = 'parkings';
- private const URL_ADJUST_DATA = 'adjust-data';
-
- private const CONFIG_KEY_API_HOST = "logic.custom.hellotechno.host";
-
- public static function getCustomers(?string $customerCode = null)
- {
-
- $query = [];
- if ($customerCode) {
- $query['customer_code'] = $customerCode;
- }
- return static::get(static::getCustomersUrl(), $query);
- }
-
- public static function getParkings(string $customerCode, ?string $parkingManagementCode = null)
- {
-
- $query['customer_code'] = $customerCode;
- if ($parkingManagementCode) {
- $query['parking_management_code'] = $parkingManagementCode;
- }
- return static::get(static::getParkingsUrl(), $query);
- }
-
- public static function getAdjustData(string $customerCode, string $parkingManagementCode, int $adjustSeqNo)
- {
- $query['customer_code'] = $customerCode;
- $query['parking_management_code'] = $parkingManagementCode;
- $query['adjust_seq_no'] = $adjustSeqNo;
- return static::get(static::getAdjustDataUrl(), $query);
- }
-
- private static function getHost(): string
- {
- return config(static::CONFIG_KEY_API_HOST, "http://localhost");
- }
-
- private static function getCustomersUrl()
- {
- $condition = [static::getHost(), static::URL_CUSTOMERS];
- return implode('/', $condition);
- }
-
- private static function getParkingsUrl()
- {
- // $condition = [static::getHost(), static::URL_PARKINGS];
- $condition = [static::getHost(), static::URL_PARKINGS, "9990"];
- return implode('/', $condition);
- }
-
- private static function getAdjustDataUrl()
- {
- $condition = [static::getHost(), static::URL_ADJUST_DATA];
- return implode('/', $condition);
- }
-
- private static function get(string $url, array $query = [])
- {
- try {
- $res = Http::get($url, $query);
- if ($res->failed()) {
- throw $res->throw();
- }
- $data = $res->json();
-
- if (data_get($data, static::RESULT_CODE) !== static::RESULT_CODE_SUCCESS) {
- Log::error("HT RESPONSE ERROR");
- Log::error($data);
- throw new AppCommonException('HT API 失敗');
- }
- return data_get($data, static::FIELD_DATA, []);
- } catch (Exception $e) {
- Log::error("HelloTechno API エラー URL:${url}]");
- throw $e;
- }
- }
- }
|