|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AmazonPHP\SellingPartner\Api\ApplicationsApi; |
| 6 | + |
| 7 | +use AmazonPHP\SellingPartner\AccessToken; |
| 8 | +use AmazonPHP\SellingPartner\Configuration; |
| 9 | +use AmazonPHP\SellingPartner\Exception\ApiException; |
| 10 | +use AmazonPHP\SellingPartner\Exception\InvalidArgumentException; |
| 11 | +use AmazonPHP\SellingPartner\HttpFactory; |
| 12 | +use AmazonPHP\SellingPartner\HttpSignatureHeaders; |
| 13 | +use Psr\Http\Client\ClientExceptionInterface; |
| 14 | +use Psr\Http\Client\ClientInterface; |
| 15 | +use Psr\Http\Message\RequestInterface; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Selling Partner API for Application Management. |
| 20 | + * |
| 21 | + * The Selling Partner API for Application Management lets you programmatically update the client secret on registered applications. |
| 22 | + * |
| 23 | + * The version of the OpenAPI document: 2023-11-30 |
| 24 | + * |
| 25 | + * This class was auto-generated by https://openapi-generator.tech |
| 26 | + * Do not change it, it will be overwritten with next execution of /bin/generate.sh |
| 27 | + */ |
| 28 | +final class ApplicationManagementSDK implements ApplicationManagementSDKInterface |
| 29 | +{ |
| 30 | + public function __construct(private readonly ClientInterface $client, private readonly HttpFactory $httpFactory, private readonly Configuration $configuration, private readonly LoggerInterface $logger) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Operation rotateApplicationClientSecret. |
| 36 | + * |
| 37 | + * @throws ApiException on non-2xx response |
| 38 | + * @throws InvalidArgumentException |
| 39 | + */ |
| 40 | + public function rotateApplicationClientSecret(AccessToken $accessToken, string $region) |
| 41 | + { |
| 42 | + $request = $this->rotateApplicationClientSecretRequest($accessToken, $region); |
| 43 | + |
| 44 | + $this->configuration->extensions()->preRequest('ApplicationManagement', 'rotateApplicationClientSecret', $request); |
| 45 | + |
| 46 | + try { |
| 47 | + $correlationId = $this->configuration->idGenerator()->generate(); |
| 48 | + $sanitizedRequest = $request; |
| 49 | + |
| 50 | + foreach ($this->configuration->loggingSkipHeaders() as $sensitiveHeader) { |
| 51 | + $sanitizedRequest = $sanitizedRequest->withoutHeader($sensitiveHeader); |
| 52 | + } |
| 53 | + |
| 54 | + if ($this->configuration->loggingEnabled('ApplicationManagement', 'rotateApplicationClientSecret')) { |
| 55 | + $this->logger->log( |
| 56 | + $this->configuration->logLevel('ApplicationManagement', 'rotateApplicationClientSecret'), |
| 57 | + 'Amazon Selling Partner API pre request', |
| 58 | + [ |
| 59 | + 'api' => 'ApplicationManagement', |
| 60 | + 'operation' => 'rotateApplicationClientSecret', |
| 61 | + 'request_correlation_id' => $correlationId, |
| 62 | + 'request_body' => (string) $sanitizedRequest->getBody(), |
| 63 | + 'request_headers' => $sanitizedRequest->getHeaders(), |
| 64 | + 'request_uri' => (string) $sanitizedRequest->getUri(), |
| 65 | + ] |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + $response = $this->client->sendRequest($request); |
| 70 | + |
| 71 | + $this->configuration->extensions()->postRequest('ApplicationManagement', 'rotateApplicationClientSecret', $request, $response); |
| 72 | + |
| 73 | + if ($this->configuration->loggingEnabled('ApplicationManagement', 'rotateApplicationClientSecret')) { |
| 74 | + $sanitizedResponse = $response; |
| 75 | + |
| 76 | + foreach ($this->configuration->loggingSkipHeaders() as $sensitiveHeader) { |
| 77 | + $sanitizedResponse = $sanitizedResponse->withoutHeader($sensitiveHeader); |
| 78 | + } |
| 79 | + |
| 80 | + $this->logger->log( |
| 81 | + $this->configuration->logLevel('ApplicationManagement', 'rotateApplicationClientSecret'), |
| 82 | + 'Amazon Selling Partner API post request', |
| 83 | + [ |
| 84 | + 'api' => 'ApplicationManagement', |
| 85 | + 'operation' => 'rotateApplicationClientSecret', |
| 86 | + 'response_correlation_id' => $correlationId, |
| 87 | + 'response_body' => (string) $sanitizedResponse->getBody(), |
| 88 | + 'response_headers' => $sanitizedResponse->getHeaders(), |
| 89 | + 'response_status_code' => $sanitizedResponse->getStatusCode(), |
| 90 | + 'request_uri' => (string) $sanitizedRequest->getUri(), |
| 91 | + 'request_body' => (string) $sanitizedRequest->getBody(), |
| 92 | + ] |
| 93 | + ); |
| 94 | + } |
| 95 | + } catch (ClientExceptionInterface $e) { |
| 96 | + throw new ApiException( |
| 97 | + "[{$e->getCode()}] {$e->getMessage()}", |
| 98 | + (int) $e->getCode(), |
| 99 | + null, |
| 100 | + null, |
| 101 | + $e |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + $statusCode = $response->getStatusCode(); |
| 106 | + |
| 107 | + if ($statusCode < 200 || $statusCode > 299) { |
| 108 | + throw new ApiException( |
| 109 | + \sprintf( |
| 110 | + '[%d] Error connecting to the API (%s)', |
| 111 | + $statusCode, |
| 112 | + (string) $request->getUri() |
| 113 | + ), |
| 114 | + $statusCode, |
| 115 | + $response->getHeaders(), |
| 116 | + (string) $response->getBody() |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Create request for operation 'rotateApplicationClientSecret'. |
| 125 | + * |
| 126 | + * @throws \AmazonPHP\SellingPartner\Exception\InvalidArgumentException |
| 127 | + */ |
| 128 | + public function rotateApplicationClientSecretRequest(AccessToken $accessToken, string $region) : RequestInterface |
| 129 | + { |
| 130 | + $resourcePath = '/applications/2023-11-30/clientSecret'; |
| 131 | + $formParams = []; |
| 132 | + $queryParams = []; |
| 133 | + $headerParams = []; |
| 134 | + $multipart = false; |
| 135 | + $query = ''; |
| 136 | + |
| 137 | + if (\count($queryParams)) { |
| 138 | + $query = \http_build_query($queryParams); |
| 139 | + } |
| 140 | + |
| 141 | + if ($multipart) { |
| 142 | + $headers = [ |
| 143 | + 'accept' => ['application/json'], |
| 144 | + 'host' => [$this->configuration->apiHost($region)], |
| 145 | + 'user-agent' => [$this->configuration->userAgent()], |
| 146 | + ]; |
| 147 | + } else { |
| 148 | + $headers = [ |
| 149 | + 'content-type' => ['application/json'], |
| 150 | + 'accept' => ['application/json'], |
| 151 | + 'host' => [$this->configuration->apiHost($region)], |
| 152 | + 'user-agent' => [$this->configuration->userAgent()], |
| 153 | + ]; |
| 154 | + } |
| 155 | + |
| 156 | + $request = $this->httpFactory->createRequest( |
| 157 | + 'POST', |
| 158 | + $this->configuration->apiURL($region) . $resourcePath . '?' . $query |
| 159 | + ); |
| 160 | + |
| 161 | + // for model (json/xml) |
| 162 | + if (\count($formParams) > 0) { |
| 163 | + if ($multipart) { |
| 164 | + $multipartContents = []; |
| 165 | + |
| 166 | + foreach ($formParams as $formParamName => $formParamValue) { |
| 167 | + $formParamValueItems = \is_array($formParamValue) ? $formParamValue : [$formParamValue]; |
| 168 | + |
| 169 | + foreach ($formParamValueItems as $formParamValueItem) { |
| 170 | + $multipartContents[] = [ |
| 171 | + 'name' => $formParamName, |
| 172 | + 'contents' => $formParamValueItem, |
| 173 | + ]; |
| 174 | + } |
| 175 | + } |
| 176 | + $request = $request->withParsedBody($multipartContents); |
| 177 | + } elseif ($headers['content-type'] === ['application/json']) { |
| 178 | + $request = $request->withBody($this->httpFactory->createStreamFromString(\json_encode($formParams, JSON_THROW_ON_ERROR))); |
| 179 | + } else { |
| 180 | + $request = $request->withParsedBody($formParams); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + foreach (\array_merge($headerParams, $headers) as $name => $header) { |
| 185 | + $request = $request->withHeader($name, $header); |
| 186 | + } |
| 187 | + |
| 188 | + return HttpSignatureHeaders::forConfig( |
| 189 | + $this->configuration, |
| 190 | + $accessToken, |
| 191 | + $region, |
| 192 | + $request |
| 193 | + ); |
| 194 | + } |
| 195 | +} |
0 commit comments