Skip to content

Commit

Permalink
Merge pull request #4 from PAYONE-GmbH/refactor/simplify-sdk-interface
Browse files Browse the repository at this point in the history
Refactor/simplify sdk interface
  • Loading branch information
lrosenfeldt authored Jul 29, 2024
2 parents bfcb38d + 71aeee6 commit fe5ac13
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 861 deletions.
72 changes: 1 addition & 71 deletions src/PayoneCommercePlatform/Sdk/ApiClient/BaseApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use PayoneCommercePlatform\Sdk\CommunicatorConfiguration;
Expand Down Expand Up @@ -73,25 +71,6 @@ public function getConfig(): CommunicatorConfiguration
return $this->config;
}

/**
* Create http client option
*
* @throws \RuntimeException on file opening failure
* @return array<string, resource> of http client options
*/
protected function createHttpClientOption(): array
{
$options = [];
if ($this->config->getDebug()) {
$options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a');
if (!$options[RequestOptions::DEBUG]) {
throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile());
}
}

return $options;
}

/**
*
* @template T
Expand All @@ -103,10 +82,8 @@ protected function createHttpClientOption(): array
protected function makeApiCall(Request $request, ?string $type = null): array
{
$request = $this->requestHeaderGenerator->generateAdditionalRequestHeaders($request);
$options = $this->createHttpClientOption();

try {
$response = $this->client->send($request, $options);
$response = $this->client->send($request, []);
$this->handleError($response);
} catch (RequestException $e) {
throw new ApiResponseRetrievalException(
Expand Down Expand Up @@ -152,53 +129,6 @@ protected function makeApiCall(Request $request, ?string $type = null): array
}
}

protected function makeAsyncApiCall(Request $request, ?string $type): PromiseInterface
{
$request = $this->requestHeaderGenerator->generateAdditionalRequestHeaders($request);
$options = $this->createHttpClientOption();
$returnType = $type ?: '';

return $this->client
->sendAsync($request, $options)
->then(
function ($response) use ($returnType) {
$this->handleError($response);

if ($returnType === '') {
return [null, $response->getStatusCode(), $response->getHeaders()];
}

$contents = "";
try {
$contents = $response->getBody()->getContents();
return [
self::$serializer->deserialize($contents, $returnType, 'json'),
$response->getStatusCode(),
$response->getHeaders()
];
} catch (NotEncodableValueException | UnexpectedValueException $exception) {
throw new ApiResponseRetrievalException(
message: 'Error JSON decoding server response',
statusCode: $response->getStatusCode(),
responseBody: $contents,
previous: $exception,
);
}
},
function ($exception) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiResponseRetrievalException(
statusCode: $statusCode,
message: sprintf('[%d] Error communicating with the API', $statusCode),
responseBody: $response->getBody()->getContents(),
previous: $exception,
);
}
);
}


protected function handleError(ResponseInterface $response): void
{
$statusCode = (int) ($response->getStatusCode());
Expand Down
165 changes: 9 additions & 156 deletions src/PayoneCommercePlatform/Sdk/ApiClient/CheckoutApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PayoneCommercePlatform\Sdk\ApiClient;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Promise\PromiseInterface;
use PayoneCommercePlatform\Sdk\Models\CheckoutResponse;
use PayoneCommercePlatform\Sdk\Models\CreateCheckoutResponse;
use PayoneCommercePlatform\Sdk\Models\PatchCheckoutRequest;
Expand All @@ -13,14 +12,6 @@
use PayoneCommercePlatform\Sdk\Errors\ApiErrorResponseException;
use PayoneCommercePlatform\Sdk\Errors\ApiResponseRetrievalException;

/**
* CheckoutApi Class Doc Comment
*
* @category Class
* @package PayoneCommercePlatform\Sdk
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class CheckoutApiClient extends BaseApiClient
{
/**
Expand All @@ -43,29 +34,6 @@ public function createCheckout(string $merchantId, string $commerceCaseId, Creat
return $response;
}

/**
* Operation createCheckoutAsync
*
* Add a Checkout to an existing Commerce Case
*
* @param string $merchantId The merchantId identifies uniquely the merchant. A Checkout has exactly one merchant. (required)
* @param string $commerceCaseId Unique identifier of a Commerce Case. (required)
* @param \PayoneCommercePlatform\Sdk\Models\CreateCheckoutRequest $createCheckoutRequest (required)
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function createCheckoutAsync(string $merchantId, string $commerceCaseId, CreateCheckoutRequest $createCheckoutRequest): PromiseInterface
{
$request = $this->createCheckoutRequest($merchantId, $commerceCaseId, $createCheckoutRequest);

return $this->makeAsyncApiCall($request, CreateCheckoutResponse::class)
->then(
function ($response) {
return $response[0];
}
);
}

/**
* Create request for operation 'createCheckout'
*
Expand All @@ -75,7 +43,7 @@ function ($response) {
*
* @return \GuzzleHttp\Psr7\Request
*/
public function createCheckoutRequest(
protected function createCheckoutRequest(
string $merchantId,
string $commerceCaseId,
CreateCheckoutRequest $createCheckoutRequest,
Expand All @@ -97,11 +65,7 @@ public function createCheckoutRequest(


/** @var array<string, string> */
$headers = [];
if ($this->config->getUserAgent()) {
$headers['User-Agent'] = $this->config->getUserAgent();
}
$headers['Content-Type'] = self::MEDIA_TYPE_JSON;
$headers = ['Content-Type' => self::MEDIA_TYPE_JSON];

$httpBody = self::$serializer->serialize($createCheckoutRequest, 'json');

Expand Down Expand Up @@ -133,29 +97,6 @@ public function deleteCheckout(string $merchantId, string $commerceCaseId, strin
$this->makeApiCall($request);
}

/**
* Operation deleteCheckoutAsync
*
* Delete a Checkout
*
* @param string $merchantId The merchantId identifies uniquely the merchant. A Checkout has exactly one merchant. (required)
* @param string $commerceCaseId Unique identifier of a Commerce Case. (required)
* @param CreateCheckoutRequest $createCheckoutRequest Content of the newly created checkout (required)
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function deleteCheckoutAsync(string $merchantId, string $commerceCaseId, CreateCheckoutRequest $createCheckoutRequest): PromiseInterface
{
$request = $this->createCheckoutRequest($merchantId, $commerceCaseId, $createCheckoutRequest);

return $this->makeAsyncApiCall($request, CreateCheckoutResponse::class)
->then(
function ($response) {
return $response[0];
}
);
}

/**
* Create request for operation 'deleteCheckout'
*
Expand All @@ -165,7 +106,7 @@ function ($response) {
*
* @return \GuzzleHttp\Psr7\Request
*/
public function deleteCheckoutRequest(string $merchantId, string $commerceCaseId, string $checkoutId): Request
protected function deleteCheckoutRequest(string $merchantId, string $commerceCaseId, string $checkoutId): Request
{
$resourcePath = '/v1/{merchantId}/commerce-cases/{commerceCaseId}/checkouts/{checkoutId}';
$contentType = 'application/json';
Expand All @@ -189,11 +130,7 @@ public function deleteCheckoutRequest(string $merchantId, string $commerceCaseId
);

/** @var array<string, string> */
$headers = [];
if ($this->config->getUserAgent()) {
$headers['User-Agent'] = $this->config->getUserAgent();
}
$headers['Content-Type'] = self::MEDIA_TYPE_JSON;
$headers = ['Content-Type' => self::MEDIA_TYPE_JSON];

$operationHost = $this->config->getHost();
return new Request(
Expand Down Expand Up @@ -223,29 +160,6 @@ public function getCheckout(string $merchantId, string $commerceCaseId, string $
return $response;
}

/**
* Operation getCheckoutAsync
*
* Get Checkout Details
*
* @param string $merchantId The merchantId identifies uniquely the merchant. A Checkout has exactly one merchant. (required)
* @param string $commerceCaseId Unique identifier of a Commerce Case. (required)
* @param string $checkoutId Unique identifier of a Checkout (required)
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function getCheckoutAsync(string $merchantId, string $commerceCaseId, string $checkoutId): PromiseInterface
{
$request = $this->getCheckoutRequest($merchantId, $commerceCaseId, $checkoutId);

return $this->makeAsyncApiCall($request, CheckoutResponse::class)
->then(
function ($response) {
return $response[0];
}
);
}

/**
* Create request for operation 'getCheckout'
*
Expand All @@ -255,7 +169,7 @@ function ($response) {
*
* @return \GuzzleHttp\Psr7\Request
*/
public function getCheckoutRequest(string $merchantId, string $commerceCaseId, string $checkoutId): Request
protected function getCheckoutRequest(string $merchantId, string $commerceCaseId, string $checkoutId): Request
{

$resourcePath = '/v1/{merchantId}/commerce-cases/{commerceCaseId}/checkouts/{checkoutId}';
Expand All @@ -278,12 +192,7 @@ public function getCheckoutRequest(string $merchantId, string $commerceCaseId, s


/** @var array<string, string> */
$headers = [];
if ($this->config->getUserAgent()) {
$headers['User-Agent'] = $this->config->getUserAgent();
}
$headers['Content-Type'] = self::MEDIA_TYPE_JSON;

$headers = ['Content-Type' => self::MEDIA_TYPE_JSON];

$operationHost = $this->config->getHost();
return new Request(
Expand Down Expand Up @@ -313,30 +222,6 @@ public function getCheckouts(
return $response;
}

/**
* Operation getCheckoutsAsync
*
* Get a list of Checkouts based on Search Parameters
*
* @param string $merchantId The merchantId identifies uniquely the merchant. A Checkout has exactly one merchant. (required)
* @param GetCheckoutsQuery $getCheckoutsQuery query parameter to filter checkouts
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function getCheckoutsAsync(
string $merchantId,
GetCheckoutsQuery $getCheckoutsQuery = new GetCheckoutsQuery(),
): PromiseInterface {
$request = $this->getCheckoutsRequest($merchantId, $getCheckoutsQuery);

return $this->makeAsyncApiCall($request, CheckoutsResponse::class)
->then(
function ($response) {
return $response[0];
}
);
}

/**
* Create request for operation 'getCheckouts'
*
Expand All @@ -358,11 +243,7 @@ protected function getCheckoutsRequest(
);

/** @var array<string, string> */
$headers = [];
$headers['Content-Type'] = self::MEDIA_TYPE_JSON;
if ($this->config->getUserAgent()) {
$headers['User-Agent'] = $this->config->getUserAgent();
}
$headers = ['Content-Type' => self::MEDIA_TYPE_JSON];

$operationHost = $this->config->getHost();
return new Request(
Expand Down Expand Up @@ -392,30 +273,6 @@ public function updateCheckout(string $merchantId, string $commerceCaseId, strin
$this->makeApiCall($request, null);
}

/**
* Operation updateCheckoutAsync
*
* Modify a Checkout
*
* @param string $merchantId The merchantId identifies uniquely the merchant. A Checkout has exactly one merchant. (required)
* @param string $commerceCaseId Unique identifier of a Commerce Case. (required)
* @param string $checkoutId Unique identifier of a Checkout (required)
* @param \PayoneCommercePlatform\Sdk\Models\PatchCheckoutRequest $patchCheckoutRequest (required)
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function updateCheckoutAsync($merchantId, $commerceCaseId, $checkoutId, $patchCheckoutRequest): PromiseInterface
{
$request = $this->updateCheckoutRequest($merchantId, $commerceCaseId, $checkoutId, $patchCheckoutRequest);

return $this->makeAsyncApiCall($request, null)
->then(
function ($response) {
return $response[0];
}
);
}

/**
* Create request for operation 'updateCheckout'
*
Expand All @@ -426,7 +283,7 @@ function ($response) {
*
* @return \GuzzleHttp\Psr7\Request
*/
public function updateCheckoutRequest(string $merchantId, string $commerceCaseId, string $checkoutId, PatchCheckoutRequest $patchCheckoutRequest): Request
protected function updateCheckoutRequest(string $merchantId, string $commerceCaseId, string $checkoutId, PatchCheckoutRequest $patchCheckoutRequest): Request
{
$resourcePath = '/v1/{merchantId}/commerce-cases/{commerceCaseId}/checkouts/{checkoutId}';
$httpBody = '';
Expand All @@ -449,11 +306,7 @@ public function updateCheckoutRequest(string $merchantId, string $commerceCaseId
);

/** @var array<string, string> */
$headers = [];
if ($this->config->getUserAgent()) {
$headers['User-Agent'] = $this->config->getUserAgent();
}
$headers['Content-Type'] = self::MEDIA_TYPE_JSON;
$headers = ['Content-Type' => self::MEDIA_TYPE_JSON];

// json_encode the body
$httpBody = self::$serializer->serialize($patchCheckoutRequest, 'json');
Expand Down
Loading

0 comments on commit fe5ac13

Please sign in to comment.