From fd0d92c40006a8dda50341315f4dd9d92cd621da Mon Sep 17 00:00:00 2001 From: Misolai Date: Tue, 5 Sep 2023 10:48:55 +0800 Subject: [PATCH] Update to 3.0.2 --- composer.json | 2 +- src/Client.php | 98 ++++++++++++++++++++++----------------------- src/CurlRequest.php | 6 +-- 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index f6822ff..4ee4556 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "AmazonAdvertisingApi\\": "src/" } }, - "version": "3.0.1", + "version": "3.0.2", "require": { "ext-curl": "*", "ext-json": "*", diff --git a/src/Client.php b/src/Client.php index 4133996..088315e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -57,6 +57,7 @@ class Client private $portfoliosVersion; private $reportsVersion; private $applicationVersion; + public $campaignTypePrefix; private $userAgent; private $isUseProxy = false; private $endpoint = null; @@ -85,15 +86,15 @@ public function __construct($config) $this->spVersion = $config['spVersion'] ?? ''; $this->portfoliosVersion = $config['portfoliosVersion'] ?? ''; $this->reportsVersion = $config['reportsVersion'] ?? ''; - $this->apiVersion = is_null($this->apiVersion) ? $this->versionStrings["apiVersion"] : $this->apiVersion; - $this->applicationVersion = $this->versionStrings["applicationVersion"]; + $this->apiVersion = is_null($this->apiVersion) ? $this->versionStrings['apiVersion'] : $this->apiVersion; + $this->applicationVersion = $this->versionStrings['applicationVersion']; $this->userAgent = $config['appUserAgent']; $this->headerAccept = $config['headerAccept'] ?? ''; $this->validateConfig($config); $this->validateConfigParameters(); $this->setEndpoints(); - if (is_null($this->config["accessToken"]) && !is_null($this->config["refreshToken"])) { + if (is_null($this->config['accessToken']) && !is_null($this->config['refreshToken'])) { $this->doRefreshToken(); } } @@ -120,7 +121,7 @@ public function doRefreshToken(): array "User-Agent: $this->userAgent" ); - $refresh_token = rawurldecode($this->config["refreshToken"]); + $refresh_token = rawurldecode($this->config['refreshToken']); $params = array( 'grant_type' => 'refresh_token', @@ -145,9 +146,9 @@ public function doRefreshToken(): array $response = $this->executeRequest($request); - $response_array = json_decode($response["response"], true); + $response_array = json_decode($response['response'], true); if (is_array($response_array) && array_key_exists("access_token", $response_array)) { - $this->config["accessToken"] = $response_array["access_token"]; + $this->config['accessToken'] = $response_array['access_token']; } else { $this->logAndThrow("Unable to refresh token. 'access_token' not found in response. " . print_r($response, true)); @@ -170,7 +171,7 @@ public function download($location, bool $gunzip = false): array } if ($gunzip) { $response = $this->executeRequest($request); - $response["response"] = gzdecode($response["response"]); + $response['response'] = gzdecode($response['response']); return $response; } return $this->executeRequest($request); @@ -194,7 +195,7 @@ protected function saveDownloaded(CurlRequest $request): array $extractedFile = $this->extractFile($filePath); fclose($tmpFile); $response['response_type'] = 'file'; - $response["response"] = $extractedFile; + $response['response'] = $extractedFile; } else { fclose($tmpFile); unlink($filePath); @@ -252,18 +253,18 @@ private function operation(string $interface, ?array $params = [], string $metho $url = "$this->endpoint/$interface"; $this->requestId = null; switch (strtolower($method)) { - case "get": + case 'get': if (!empty($params)) { - $url .= "?"; + $url .= '?'; foreach ($params as $k => $v) { - $url .= "$k=" . rawurlencode($v) . "&"; + $url .= "$k=" . rawurlencode($v) . '&'; } - $url = rtrim($url, "&"); + $url = rtrim($url, '&'); } break; - case "put": - case "post": - case "delete": + case 'put': + case 'post': + case 'delete': if (!empty($params)) { $data = json_encode($params); $request->setOption(CURLOPT_POST, true); @@ -290,33 +291,33 @@ protected function executeRequest(CurlRequest $request): array $this->requestId = $request->requestId; $response_info = $request->getInfo(); $request->close(); - if ($response_info["http_code"] == 307) { + if ($response_info['http_code'] == 307) { /* application/octet-stream */ - return $this->download($response_info["redirect_url"], true); + return $this->download($response_info['redirect_url'], true); } - if (!preg_match("/^(2|3)\d{2}$/", $response_info["http_code"])) { + if (!preg_match("/^(2|3)\d{2}$/", $response_info['http_code'])) { $requestId = 0; $json = json_decode($response, true); if (!is_null($json)) { - if (array_key_exists("requestId", $json)) { - $requestId = json_decode($response, true)["requestId"]; + if (array_key_exists('requestId', $json)) { + $requestId = json_decode($response, true)['requestId']; } } return array( - "success" => false, - "code" => $response_info["http_code"], - "response" => is_array($response) ? $response : $json, + 'success' => false, + 'code' => $response_info['http_code'], + 'response' => is_array($response) ? $response : $json, 'responseInfo' => $response_info, - "requestId" => $requestId + 'requestId' => $requestId ); } else { return array( - "success" => true, - "code" => $response_info["http_code"], + 'success' => true, + 'code' => $response_info['http_code'], 'responseInfo' => $response_info, - "response" => $response, - "requestId" => $this->requestId + 'response' => $response, + 'requestId' => $this->requestId ); } } @@ -349,42 +350,42 @@ private function validateConfig($config): bool private function validateConfigParameters(): bool { foreach ($this->config as $k => $v) { - if (is_null($v) && $k !== "accessToken" && $k !== "refreshToken") { + if (is_null($v) && $k !== 'accessToken' && $k !== 'refreshToken') { $this->logAndThrow("Missing required parameter $k."); } switch ($k) { - case "clientId": + case 'clientId': if (!preg_match("/^amzn1\.application-oa2-client\.[a-z0-9]{32}$/i", $v)) { - $this->logAndThrow("Invalid parameter value for clientId."); + $this->logAndThrow('Invalid parameter value for clientId.'); } break; - case "clientSecret": + case 'clientSecret': if (!preg_match("/^[a-z0-9]{64}$/i", $v)) { - $this->logAndThrow("Invalid parameter value for clientSecret."); + $this->logAndThrow('Invalid parameter value for clientSecret.'); } break; - case "accessToken": + case 'accessToken': if (!is_null($v)) { if (!preg_match("/^Atza(\||%7C|%7c).*$/", $v)) { - $this->logAndThrow("Invalid parameter value for accessToken."); + $this->logAndThrow('Invalid parameter value for accessToken.'); } } break; - case "refreshToken": + case 'refreshToken': if (!is_null($v)) { if (!preg_match("/^Atzr(\||%7C|%7c).*$/", $v)) { - $this->logAndThrow("Invalid parameter value for refreshToken."); + $this->logAndThrow('Invalid parameter value for refreshToken.'); } } break; - case "sandbox": + case 'sandbox': if (!is_bool($v)) { - $this->logAndThrow("Invalid parameter value for sandbox."); + $this->logAndThrow('Invalid parameter value for sandbox.'); } break; - case "saveFile": + case 'saveFile': if (!is_bool($v)) { - $this->logAndThrow("Invalid parameter value for saveFile."); + $this->logAndThrow('Invalid parameter value for saveFile.'); } break; } @@ -399,16 +400,16 @@ private function validateConfigParameters(): bool private function setEndpoints(): bool { /* check if region exists and set api/token endpoints */ - if (array_key_exists(strtolower($this->config["region"]), $this->endpoints)) { - $region_code = strtolower($this->config["region"]); - if ($this->config["sandbox"]) { - $this->endpoint = "https://{$this->endpoints[$region_code]["sandbox"]}/"; + if (array_key_exists(strtolower($this->config['region']), $this->endpoints)) { + $region_code = strtolower($this->config['region']); + if ($this->config['sandbox']) { + $this->endpoint = "https://{$this->endpoints[$region_code]['sandbox']}/"; } else { - $this->endpoint = "https://{$this->endpoints[$region_code]["prod"]}/"; + $this->endpoint = "https://{$this->endpoints[$region_code]['prod']}/"; } - $this->tokenUrl = $this->endpoints[$region_code]["tokenUrl"]; + $this->tokenUrl = $this->endpoints[$region_code]['tokenUrl']; } else { - $this->logAndThrow("Invalid region."); + $this->logAndThrow('Invalid region.'); } return true; } @@ -421,5 +422,4 @@ private function logAndThrow($message) { throw new Exception($message); } - } diff --git a/src/CurlRequest.php b/src/CurlRequest.php index f8e1636..8dcaaa9 100644 --- a/src/CurlRequest.php +++ b/src/CurlRequest.php @@ -8,7 +8,7 @@ */ class CurlRequest { - private $handle = null; + private $handle; public $requestId = null; public function __construct($config) @@ -74,7 +74,7 @@ public function getInfo() return curl_getinfo($this->handle); } - public function getError() + public function getError(): string { return curl_error($this->handle); } @@ -84,7 +84,7 @@ public function close() curl_close($this->handle); } - private function handleHeaderLine($ch, $line) + private function handleHeaderLine($ch, $line): int { $matches = array(); if (preg_match("/x-amz-request-id:\ \S+/", $line)) {