You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.PddApiAbstract.php

71 lines
2.0 KiB
PHP

<?php
abstract class PddApiAbstract {
/**
* @var PddOAuthClient
*/
protected static $client;
/**
* @var PddOAuthBaseRequest
*/
protected static $lastRequest;
protected static $lastResponse;
protected static function getClient() {
if (!self::$client) {
self::$client = PddOAuthUtil::getPddOAuthClient();
}
return self::$client;
}
protected static function execute($request, $accessToken) {
self::$lastRequest = $request;
$response = self::getClient()->execute($request, $accessToken);
DebugData::add('pdd-api', [
'accessToken' => $accessToken,
'apiMethod' => $request->getApiMethod(),
'apiParams' => json_decode($request->getApiParas(), true),
'apiResponse' => $response,
]);
self::$lastResponse = $response;
if (!self::isSuccessResponse($response)) {
self::apiLog($request->getApiMethod(), $request, $response);
}
return $response;
}
protected static function handleResponse($response, $successDataBuilder = null) {
return $response;
}
protected static function isSuccessResponse($response) {
$response = ZcArrayHelper::objectToArray($response);
if (isset($response['code']) && $response['code'] == 0) {
return true;
}
$resultInfo = isset($response['code']) ? $response['resultInfo'] : $response;
if (!isset($resultInfo['statusCode']) || $resultInfo['statusCode'] !== 0) {
return false;
}
return true;
}
protected static function apiLog($apiName, $request, $response) {
Zc::getLog('tool/pddApi/' . $apiName)->info('params: ' . print_r($request->getApiParas(), true) . 'response: ' . print_r($response, true));
}
public static function getLastRequest() {
return self::$lastRequest;
}
public static function getLastResponse() {
return self::$lastResponse;
}
}