Skip to content

Commit

Permalink
feat: add oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb committed Jan 23, 2024
1 parent c5ac38d commit d1f9d69
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/Entity/ApiAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Adbros\Shoptet\Entity;

class ApiAccessToken
{

public function __construct(
public readonly string $accessToken,
public readonly int $expiresIn,
)
{
}

/**
* @param array<string,mixed> $json
*/
public static function fromJson(array $json): self
{
return new self(
accessToken: $json['access_token'],
expiresIn: $json['expires_in'],
);
}

}
38 changes: 38 additions & 0 deletions src/Entity/OAuthAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace Adbros\Shoptet\Entity;

class OAuthAccessToken
{

public function __construct(
public readonly string $accessToken,
public readonly ?int $expiresIn,
public readonly string $tokenType,
public readonly string $scope,
public readonly int $eshopId,
public readonly string $eshopUrl,
public readonly string $contactEmail,
public readonly bool $trial,
)
{
}

/**
* @param array<string,mixed> $json
*/
public static function fromJson(array $json): self
{
return new self(
accessToken: $json['access_token'],
expiresIn: $json['expires_in'],
tokenType: $json['token_type'],
scope: $json['scope'],
eshopId: $json['eshopId'],
eshopUrl: $json['eshopUrl'],
contactEmail: $json['contactEmail'],
trial: $json['trial'],
);
}

}
76 changes: 76 additions & 0 deletions src/OAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types = 1);

namespace Adbros\Shoptet;

use Adbros\Shoptet\Entity\ApiAccessToken;
use Adbros\Shoptet\Entity\OAuthAccessToken;
use Adbros\Shoptet\Exception\ClientException;
use Adbros\Shoptet\Exception\ServerException;
use GuzzleHttp\Client;
use Nette\Utils\Json;

class OAuth
{

private Client $httpClient;

public function __construct(
private readonly string $clientId,
private readonly string $clientSecret,
private readonly string $oAuthAccessTokenUrl,
private readonly string $apiAccessTokenUrl,
)
{
$this->httpClient = new Client();
}

public function getOAuthAccessToken(string $code, string $redirectUri): OAuthAccessToken // @phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps
{
/** @var array<string, mixed> $response */
$response = $this->request('POST', $this->oAuthAccessTokenUrl, [
'json' => [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirectUri,
'scope' => 'api',
],
]);

return OAuthAccessToken::fromJson($response);
}

public function getApiAccessToken(string $oAuthAccessToken): ApiAccessToken
{
/** @var array<string, mixed> $response */
$response = $this->request('POST', $this->apiAccessTokenUrl, [
'headers' => [
'Authorization' => sprintf('Bearer %s', $oAuthAccessToken),
],
]);

return ApiAccessToken::fromJson($response);
}

/**
* @param array<string, mixed> $options
* @return array<string, mixed>
*/
private function request(string $method, string $uri, array $options = []): ?array
{
try {
$response = $this->httpClient->request($method, $uri, $options);
} catch (\GuzzleHttp\Exception\ClientException | \GuzzleHttp\Exception\ServerException $e) {
$class = $e instanceof \GuzzleHttp\Exception\ClientException
? ClientException::class
: ServerException::class;
$json = Json::decode($e->getResponse()->getBody()->getContents(), Json::FORCE_ARRAY);

throw new $class('', $e->getCode(), $e, $json['errors'] ?? []);
}

return Json::decode($response->getBody()->getContents(), Json::FORCE_ARRAY);
}

}
2 changes: 1 addition & 1 deletion src/Sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Sdk
private Client $httpClient;

public function __construct(
string $apiBaseUri,
string $apiAccessToken,
string $apiBaseUri = 'https://api.myshoptet.com/api/',
)
{
$this->httpClient = new Client([
Expand Down

0 comments on commit d1f9d69

Please sign in to comment.