|
- <?php
-
- namespace App\Util\Custom\HelloTechno;
-
- use App\Exceptions\AppCommonException;
- 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 $customerCde = null)
- {
- $res = Http::get(static::getCustomersUrl($customerCde));
- return static::handleResponse($res);
- }
-
- public static function getParkings(string $customerCode, ?string $parkingManagementCode = null)
- {
- $res = Http::get(static::getParkingsUrl($customerCode, $parkingManagementCode));
- return static::handleResponse($res);
- }
-
- public static function getAdjustData(string $customerCode, string $parkingManagementCode, int $adjustSeqNo)
- {
- $res = Http::get(static::getAdjustDataUrl($customerCode, $parkingManagementCode, $adjustSeqNo));
- return static::handleResponse($res);
- }
-
- private static function getHost(): string
- {
- return config(static::CONFIG_KEY_API_HOST, "http://localhost");
- }
-
- private static function getCustomersUrl(?string $customerCode)
- {
- $condition = [static::getHost(), static::URL_CUSTOMERS];
- if ($customerCode) {
- $condition[] = $customerCode;
- }
- return implode('/', $condition);
- }
-
- private static function getParkingsUrl(string $customerCode, ?string $parkingManagementCode)
- {
- $condition = [static::getHost(), static::URL_PARKINGS, $customerCode];
- if ($parkingManagementCode) {
- $condition[] = $parkingManagementCode;
- }
- return implode('/', $condition);
- }
-
- private static function getAdjustDataUrl(string $customerCode, string $parkingManagementCode, int $adjustSeqNo)
- {
- $condition = [static::getHost(), static::URL_ADJUST_DATA, $customerCode, $parkingManagementCode, $adjustSeqNo];
- return implode('/', $condition);
- }
-
- private static function handleResponse(Response $res): array
- {
- 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, []);
- }
- }
|