diff --git a/src/Client/License.php b/src/Client/License.php index 9a0d387..a9cc041 100644 --- a/src/Client/License.php +++ b/src/Client/License.php @@ -104,6 +104,9 @@ public function getContentInfo(LicenseRequest $request, string $access_token, Ht $end_point = $this->_config->getEndPoints()['license_info']; $request_url = $end_point . '?' . http_build_query($request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } $raw_response = $http_client->doGet($request_url, $headers); $license_response = new LicenseResponse(json_decode($raw_response, true)); return $license_response; @@ -127,7 +130,10 @@ public function getContentLicense(LicenseRequest $request, string $access_token, $end_point = $this->_config->getEndPoints()['license']; $request_url = $end_point . '?' . http_build_query($request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); - + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } + if ($request->getLicenseReference() != null) { $raw_response = $http_client->doPost($request_url, $headers, $request->getLicenseReference()); } else { @@ -157,6 +163,9 @@ public function getMemberProfile(LicenseRequest $request, string $access_token, $end_point = $this->_config->getEndPoints()['user_profile']; $request_url = $end_point . '?' . http_build_query($request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } $raw_response = $http_client->doGet($request_url, $headers); $license_response = new LicenseResponse(json_decode($raw_response, true)); return $license_response; @@ -177,6 +186,9 @@ public function abandonLicense(LicenseRequest $request, string $access_token, Ht $end_point = $this->_config->getEndPoints()['abandon']; $request_url = $end_point . '?' . http_build_query($request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } $response = $http_client->doGet($request_url, $headers); $code = (int) $response->getContents(); @@ -241,6 +253,9 @@ public function downloadAssetRequest(LicenseRequest $request, string $access_tok $url = $purchase_details->getUrl(); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } $headers['allow_redirects'] = false; $client_handler = $http_client->getHandlerStack(); //adds middleware in the client which controls the redirection behaviour. diff --git a/src/Client/LicenseHistory.php b/src/Client/LicenseHistory.php index 5bb1cdf..fa531d0 100644 --- a/src/Client/LicenseHistory.php +++ b/src/Client/LicenseHistory.php @@ -140,6 +140,9 @@ private function _doOnError() private function _getFiles(LicenseHistoryRequest $license_file_request, string $access_token = null) : LicenseHistoryResponse { $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($license_file_request->getRequestId())) { + $headers['headers']['x-request-id'] = $license_file_request->getRequestId(); + } $end_point = $this->_config->getEndPoints()['license_history']; $request_url = $end_point . '?' . http_build_query($license_file_request); $offset_value = $this->_offset; diff --git a/src/Client/SearchCategory.php b/src/Client/SearchCategory.php index 9f70557..e08afcb 100644 --- a/src/Client/SearchCategory.php +++ b/src/Client/SearchCategory.php @@ -52,6 +52,9 @@ public function getCategory(SearchCategoryRequest $request, string $access_token $end_point = $this->_config->getEndPoints()['category']; $request_url = $end_point . '?' . http_build_query($request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } $raw_response = $http_client->doGet($request_url, $headers); $search_category_response = new SearchCategoryResponse(json_decode($raw_response, true)); return $search_category_response; @@ -75,6 +78,9 @@ public function getCategoryTree(SearchCategoryRequest $request, string $access_t $end_point = $this->_config->getEndPoints()['category_tree']; $request_url = $end_point . '?' . http_build_query($request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($request->getRequestId())) { + $headers['headers']['x-request-id'] = $request->getRequestId(); + } $raw_response = $http_client->doGet($request_url, $headers); return $this->_createSearchCategoryResponseArray(json_decode($raw_response, true)); } diff --git a/src/Client/SearchFiles.php b/src/Client/SearchFiles.php index 8f50cdf..7d5d022 100644 --- a/src/Client/SearchFiles.php +++ b/src/Client/SearchFiles.php @@ -82,7 +82,7 @@ class SearchFiles /** * Custom http client object - * @var HttpInterface + * @var HttpClientInterface */ private $_http_client; @@ -134,6 +134,9 @@ public function getFiles(SearchFilesRequest $search_file_request, string $access { $this->_http_method = $this->_getHttpMethod($search_file_request); $headers = APIUtils::generateCommonAPIHeaders($this->_config, $access_token); + if (!empty($search_file_request->getRequestId())) { + $headers['headers']['x-request-id'] = $search_file_request->getRequestId(); + } $end_point = $this->_config->getEndPoints()['search']; $request_url = $end_point . '?' . http_build_query($search_file_request); @@ -180,7 +183,7 @@ private function _doOnSuccess(SearchFilesResponse $response) { $this->_current_response = $response; $this->_api_in_progress = false; - + if ($this->_initial_invalid_state) { $this->_initial_invalid_state = false; } diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php new file mode 100644 index 0000000..7909e8c --- /dev/null +++ b/src/Request/AbstractRequest.php @@ -0,0 +1,81 @@ +locale; + } + + /** + * Setter for Locale. + * + * @param string $locale Language location code. + * @return AbstractRequest + * @throws StockApiException if locale is empty. + */ + public function setLocale(string $locale) : AbstractRequest + { + if (!empty($locale)) { + $this->locale = $locale; + } else { + throw StockApiException::withMessage('Locale cannot be empty string'); + } + + return $this; + } + + /** + * Set x-request-id header + * + * @param string $requestId + * @return AbstractRequest + */ + public function setRequestId(string $requestId): AbstractRequest + { + $this->requestId = $requestId; + return $this; + } + + /** + * Get x-request-id header + * + * @return string|null + */ + public function getRequestId(): ?string + { + return $this->requestId; + } +} diff --git a/src/Request/License.php b/src/Request/License.php index 0ecb26f..a3d4ab3 100644 --- a/src/Request/License.php +++ b/src/Request/License.php @@ -13,14 +13,8 @@ use \AdobeStock\Api\Models\LicenseReference; use \AdobeStock\Api\Core\Constants as CoreConstants; -class License +class License extends AbstractRequest { - /** - * Language location code - * @var string - */ - public $locale; - /** * Asset's unique identifer * @var int @@ -53,32 +47,6 @@ class License */ public $license_reference; - /** - * Getter for Locale. - * @return string Language location code. - */ - public function getLocale(): string - { - return $this->locale; - } - - /** - * Setter for Locale. - * @param string $locale Language location code. - * @return License - * @throws StockApiException if locale is empty. - */ - public function setLocale(string $locale): License - { - if (!empty($locale)) { - $this->locale = $locale; - } else { - throw StockApiException::withMessage('Locale cannot be empty string'); - } - - return $this; - } - /** * Getter for ContentId. * @return int|null Asset identifier for an existing asset. diff --git a/src/Request/LicenseHistory.php b/src/Request/LicenseHistory.php index fca4d42..32f7acd 100644 --- a/src/Request/LicenseHistory.php +++ b/src/Request/LicenseHistory.php @@ -11,13 +11,8 @@ use \AdobeStock\Api\Exception\StockApi as StockApiException; use \AdobeStock\Api\Models\SearchParamLicenseHistory as SearchParamLicenseHistoryModel; -class LicenseHistory +class LicenseHistory extends AbstractRequest { - /** - * @var string Language location code - */ - public $locale; - /** * @var SearchParamLicenseHistoryModel search params. */ @@ -27,32 +22,7 @@ class LicenseHistory * @var array result column constants */ public $result_columns; - - /** - * Getter for Locale. - * @return string|null Language location code. - */ - public function getLocale() : ?string - { - return $this->locale; - } - - /** - * Setter for Locale. - * @param string $locale Language location code. - * @throws StockApiException if locale is null - * @return LicenseHistory - */ - public function setLocale(string $locale = null) : LicenseHistory - { - if ($locale === null) { - throw StockApiException::withMessage('Locale cannot be null'); - } - - $this->locale = $locale; - return $this; - } - + /** * Get SearchParameters array that consists of various search params * @return SearchParamLicenseHistoryModel|null diff --git a/src/Request/SearchCategory.php b/src/Request/SearchCategory.php index 52083d5..3267da7 100644 --- a/src/Request/SearchCategory.php +++ b/src/Request/SearchCategory.php @@ -10,46 +10,14 @@ use \AdobeStock\Api\Exception\StockApi as StockApiException; -class SearchCategory +class SearchCategory extends AbstractRequest { - /** - * Language location code - * @var string - */ - public $locale; - /** * Unique identifier for an existing category. * @var integer */ public $category_id; - /** - * Getter for Locale. - * @return string Language location code. - */ - public function getLocale() - { - return $this->locale; - } - - /** - * Setter for Locale. - * @param string $locale Language location code. - * @return SearchCategory - * @throws StockApiException if locale is empty. - */ - public function setLocale(string $locale) : SearchCategory - { - if (!empty($locale)) { - $this->locale = $locale; - } else { - throw StockApiException::withMessage('Locale cannot be empty string'); - } - - return $this; - } - /** * Getter for CategoryId. * @return int|null Unique identifier for an existing category. diff --git a/src/Request/SearchFiles.php b/src/Request/SearchFiles.php index cf9fd29..c38cd4a 100644 --- a/src/Request/SearchFiles.php +++ b/src/Request/SearchFiles.php @@ -12,13 +12,8 @@ use \AdobeStock\Api\Core\Constants as Constants; use \AdobeStock\Api\Models\SearchParameters as SearchParametersModel; -class SearchFiles +class SearchFiles extends AbstractRequest { - /** - * @var string Language location code - */ - public $locale; - /** * @var SearchParametersModel search params. */ @@ -33,31 +28,7 @@ class SearchFiles * @var string Similar Image Path */ public $similar_image; - - /** - * Getter for Locale. - * @return string|null Language location code. - */ - public function getLocale() : ?string - { - return $this->locale; - } - - /** - * Setter for Locale. - * @param string $locale Language location code. - * @return SearchFiles - */ - public function setLocale(string $locale = null) : SearchFiles - { - if ($locale == null) { - throw StockApiException::withMessage('Locale cannot be null'); - } - - $this->locale = $locale; - return $this; - } - + /** * Get SearchParameters array that consists of various search params * @return SearchParametersModel|null diff --git a/test/src/Api/Client/LicenseFactoryTest.php b/test/src/Api/Client/LicenseFactoryTest.php index d1bbe2a..533c5fa 100644 --- a/test/src/Api/Client/LicenseFactoryTest.php +++ b/test/src/Api/Client/LicenseFactoryTest.php @@ -316,6 +316,7 @@ public function getAbandonLicenseShouldThrowExceptionIfResponseCodeIsNot204() */ public function downloadAssetRequestShouldReturnValidRequestObject() { + $url = 'https://primary.staging.adobestock.com/Rest/Libraries/Download/84071201/1'; $response = [ 'member' => [ @@ -335,11 +336,10 @@ public function downloadAssetRequestShouldReturnValidRequestObject() 'state' => 'purchased', 'license' => 'Standard', 'date' => '2017-06-21 10:38:52', - 'url' => 'https://primary.staging.adobestock.com/Rest/Libraries/Download/84071201/1', + 'url' => $url, 'content_type' => 'image/jpeg', 'width' => 4000, 'height' => 3928, - ], ], ], @@ -349,6 +349,7 @@ public function downloadAssetRequestShouldReturnValidRequestObject() $request = new LicenseRequest(); $request->setContentId(84071201)->setLicenseState('STANDARD'); $guzzle_request = $this->_license_factory->downloadAssetRequest($request, 'access_token', $this->_mocked_http_client); + $this->assertEquals($guzzle_request, new Request('GET', $url . '?token=access_token')); } /** diff --git a/test/src/Api/Client/SearchFilesTest.php b/test/src/Api/Client/SearchFilesTest.php new file mode 100644 index 0000000..bac5912 --- /dev/null +++ b/test/src/Api/Client/SearchFilesTest.php @@ -0,0 +1,65 @@ +getMockBuilder(\GuzzleHttp\Psr7\Stream::class) + ->disableOriginalConstructor() + ->setMethods(['__toString']) + ->getMock(); + $stream_mock->expects($this->once()) + ->method('__toString') + ->willReturn('{"item": 1}'); + $client_mock = $this->getMockBuilder(\AdobeStock\Api\Client\Http\HttpInterface::class) + ->setMethods(['doGet']) + ->getMockForAbstractClass(); + $client_mock->expects($this->once()) + ->method('doGet') + ->with( + $this->stringStartsWith('https://stock-stage.adobe.io/Rest/Media/1/Search/Files'), + [ + 'headers' => [ + 'x-api-key' => 'APIKey', + 'x-product' => 'Product', + 'Authorization' => null, + 'x-request-id' => $requestId + ] + ] + ) + ->willReturn($stream_mock); + + + $adobe_client = new AdobeStock('APIKey', 'Product', 'STAGE', $client_mock); + $request = new SearchFilesRequest(); + + $search_params = new SearchParameters(); + $search_params->setWords('tree'); + + $request->setLocale('En_US'); + $request->setSearchParams($search_params); + $request->setResultColumns(['id']); + $request->setRequestId($requestId); + + $client = $adobe_client->searchFilesInitialize($request, 'access_token'); + $client->getNextResponse(); + } +} diff --git a/test/src/Api/Request/LicenseHistoryRequestTest.php b/test/src/Api/Request/LicenseHistoryRequestTest.php index 17ef6a1..46e364b 100644 --- a/test/src/Api/Request/LicenseHistoryRequestTest.php +++ b/test/src/Api/Request/LicenseHistoryRequestTest.php @@ -67,7 +67,7 @@ public function setSearchParamsThrowException() */ public function setLocaleThrowException() { - $this->_request->setLocale(null); + $this->_request->setLocale(''); } /** diff --git a/test/src/Api/Request/SearchFilesRequestTest.php b/test/src/Api/Request/SearchFilesRequestTest.php index 64f3f83..3c7e3b7 100644 --- a/test/src/Api/Request/SearchFilesRequestTest.php +++ b/test/src/Api/Request/SearchFilesRequestTest.php @@ -16,7 +16,7 @@ class SearchFilesRequestTest extends TestCase { /** * Request object for search Files. - * @var SearchFiles + * @var SearchFilesRequest */ private $_request; @@ -50,7 +50,7 @@ public function testAllTheGettersSettersReturnandSetTheProperValue() $this->assertInstanceOf(SearchParametersModels::class, $this->_request->getSearchParams()); $this->_request->setResultColumns($result_column_array); $this->assertEquals($result_column_array, $this->_request->getResultColumns()); - $this->_request->setSimilarImage('test/resources/TestFile.png'); + $this->_request->setSimilarImage($this->getAbsolutePath('test/resources/TestFile.png')); } /** @@ -69,7 +69,7 @@ public function setSearchParamsThrowException() */ public function setLocaleThrowException() { - $this->_request->setLocale(null); + $this->_request->setLocale(''); } /** @@ -89,4 +89,13 @@ public function setSimilarImageThrowExceptionIfFileDoesntExist() { $this->_request->setSimilarImage(''); } + + /** + * @param string $path + * @return string + */ + private function getAbsolutePath(string $path): string + { + return dirname(dirname(dirname(dirname(__DIR__)))) . '/' . $path; + } }