Skip to content

Commit

Permalink
Merged in feature/34741_account_api (pull request #147)
Browse files Browse the repository at this point in the history
Feature/34741 account api

Approved-by: Martin Beckmann
Approved-by: Andre Moelle
  • Loading branch information
Bohdan-Medv committed Apr 29, 2024
2 parents be02538 + bdd6cc4 commit 5ce89da
Show file tree
Hide file tree
Showing 102 changed files with 4,961 additions and 216 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.7.6
* Account API implementation

## 1.7.5
* use orderarticle title as fallback if article of orderarticle was deleted
* unittest: get manifest for next week returns error code (greater 0)
Expand Down
317 changes: 172 additions & 145 deletions composer.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/modules/mo/mo_dhl/Adapter/DHLAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ public function buildParcelShipping()
return $this->getSdk()->buildParcelShipping();
}


/**
* @return \Mediaopt\DHL\Api\MyAccount\Client
*/
public function buildMyAccount()
{
return $this->getSdk()->buildMyAccount();
}

/**
* @return \Mediaopt\DHL\Api\Retoure
*/
Expand Down
56 changes: 45 additions & 11 deletions src/modules/mo/mo_dhl/Adapter/DHLConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ class DHLConfigurator extends \Mediaopt\DHL\Configurator

const STANDORTSUCHE_API_KEY_NAME = 'DHL-API-Key';

const TEST_STANDORTSUCHE_API_PASSWORD = 'kAPjq3yHFgY6QD3sHEtv61dQCAgoXLyK';
const DHL_DEVELOPER_API_KEY = 'kAPjq3yHFgY6QD3sHEtv61dQCAgoXLyK';

const DHL_DEVELOPER_API_SECRET = 'YdZnAljhgbcOXOKD';

const TEST_AUTHENTICATION_USERNAME = 'user-valid';

const TEST_AUTHENTICATION_PASSWORD = 'SandboxPasswort2023!';

/**
* @return mixed
Expand Down Expand Up @@ -117,17 +123,9 @@ protected function getStandortsucheKeyName()
/**
* @return string
*/
protected function getProdStandortsuchePassword()
{
return \OxidEsales\Eshop\Core\Registry::getConfig()->getConfigParam('mo_dhl__prod_standortsuche_password') ?: '';
}

/**
* @return string
*/
protected function getSandboxStandortsuchePassword()
protected function getStandortsuchePassword()
{
return self::TEST_STANDORTSUCHE_API_PASSWORD;
return self::DHL_DEVELOPER_API_KEY;
}

/**
Expand Down Expand Up @@ -179,6 +177,42 @@ protected function getCustomerParcelShippingPassword()
: self::TEST_PARCEL_SHIPPING_PASSWORD;
}

/**
* @return string
*/
protected function getAuthenticationClientId()
{
return self::DHL_DEVELOPER_API_KEY;
}

/**
* @return string
*/
protected function getAuthenticationClientSecret()
{
return self::DHL_DEVELOPER_API_SECRET;
}

/**
* @return string
*/
protected function getAuthenticationUsername()
{
return $this->isProductionEnvironment()
? (\OxidEsales\Eshop\Core\Registry::getConfig()->getConfigParam('mo_dhl__account_user') ?: '')
: self::TEST_AUTHENTICATION_USERNAME;
}

/**
* @return string
*/
protected function getAuthenticationPassword()
{
return $this->isProductionEnvironment()
? (\OxidEsales\Eshop\Core\Registry::getConfig()->getConfigParam('mo_dhl__account_password') ?: '')
: self::TEST_AUTHENTICATION_PASSWORD;
}

/**
* @return string
*/
Expand Down
29 changes: 29 additions & 0 deletions src/modules/mo/mo_dhl/Api/Authentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace Mediaopt\DHL\Api;

class Authentication
{
const DEFAULT_GRANT_TYPE = 'password';

/**
* @param Authentication\Client $client
* @param Credentials $credentials
* @param Credentials $userPass
* @return string
*/
public static function getToken(Authentication\Client $client, Credentials $credentials, Credentials $userPass): string
{
$tokenPostBody = new Authentication\Model\TokenPostBody();
$tokenPostBody->setClientId($credentials->getUsername());
$tokenPostBody->setClientSecret($credentials->getPassword());
$tokenPostBody->setUsername($userPass->getUsername());
$tokenPostBody->setPassword($userPass->getPassword());
$tokenPostBody->setGrantType(self::DEFAULT_GRANT_TYPE);

$token = $client->dispenseToken($tokenPostBody);

return $token->getAccessToken();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mediaopt\DHL\Api\Authentication\Authentication;

class ApiKeyAuthentication implements \Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin
{
private $apiKey;
public function __construct(string $apiKey)
{
$this->{'apiKey'} = $apiKey;
}
public function authentication(\Psr\Http\Message\RequestInterface $request) : \Psr\Http\Message\RequestInterface
{
$request = $request->withHeader('dhl-api-key', $this->{'apiKey'});
return $request;
}
public function getScope() : string
{
return 'apiKey';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Mediaopt\DHL\Api\Authentication\Authentication;

class BasicAuthAuthentication implements \Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin
{
private $username;
private $password;
public function __construct(string $username, string $password)
{
$this->{'username'} = $username;
$this->{'password'} = $password;
}
public function authentication(\Psr\Http\Message\RequestInterface $request) : \Psr\Http\Message\RequestInterface
{
$header = sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->{'username'}, $this->{'password'})));
$request = $request->withHeader('Authorization', $header);
return $request;
}
public function getScope() : string
{
return 'basicAuth';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Mediaopt\DHL\Api\Authentication\Authentication;

class BearerAuthAuthentication implements \Jane\Component\OpenApiRuntime\Client\AuthenticationPlugin
{
private $token;
public function __construct(string $token)
{
$this->{'token'} = $token;
}
public function authentication(\Psr\Http\Message\RequestInterface $request) : \Psr\Http\Message\RequestInterface
{
$header = sprintf('Bearer %s', $this->{'token'});
$request = $request->withHeader('Authorization', $header);
return $request;
}
public function getScope() : string
{
return 'bearerAuth';
}
}
84 changes: 84 additions & 0 deletions src/modules/mo/mo_dhl/Api/Authentication/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Mediaopt\DHL\Api\Authentication;

class Client extends \Mediaopt\DHL\Api\Authentication\Runtime\Client\Client
{
/**
* @param string $fetch Fetch mode to use (can be OBJECT or RESPONSE)
*
* @return null|\Mediaopt\DHL\Api\Authentication\Model\GetResponse200|\Psr\Http\Message\ResponseInterface
*/
public function get(string $fetch = self::FETCH_OBJECT)
{
return $this->executeEndpoint(new \Mediaopt\DHL\Api\Authentication\Endpoint\Get(), $fetch);
}
/**
* The client makes a request to the token endpoint by adding the following parameters
using the application/x-www-form-urlencoded format with a character
encoding of UTF-8 in the HTTP request entity-body:
* grant_type __REQUIRED__. Must be set to "password" or "refresh_token".
* client_id __REQUIRED__ (aka client_id (api key))
* client_secret __REQUIRED__ (aka client_secret)
Depending on the grant_type, __additional parameters__ must be provided:
<h3>grant_type=password</h3>
This currently is the only grant type supported.
* username __REQUIRED__. The resource owner username. Aka username for business customer portal
* password __REQUIRED__. The resource owner password. Aka password for business customer portal
*
* @param \Mediaopt\DHL\Api\Authentication\Model\TokenPostBody $requestBody
* @param string $fetch Fetch mode to use (can be OBJECT or RESPONSE)
* @throws \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenBadRequestException
* @throws \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenUnauthorizedException
* @throws \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenForbiddenException
*
* @return null|\Mediaopt\DHL\Api\Authentication\Model\TokenResponse|\Psr\Http\Message\ResponseInterface
*/
public function dispenseToken(\Mediaopt\DHL\Api\Authentication\Model\TokenPostBody $requestBody, string $fetch = self::FETCH_OBJECT)
{
return $this->executeEndpoint(new \Mediaopt\DHL\Api\Authentication\Endpoint\DispenseToken($requestBody), $fetch);
}
/**
* @param string $fetch Fetch mode to use (can be OBJECT or RESPONSE)
* @throws \Mediaopt\DHL\Api\Authentication\Exception\HelloBadRequestException
* @throws \Mediaopt\DHL\Api\Authentication\Exception\HelloUnauthorizedException
* @throws \Mediaopt\DHL\Api\Authentication\Exception\HelloForbiddenException
*
* @return null|\Mediaopt\DHL\Api\Authentication\Model\TokenResponse|\Psr\Http\Message\ResponseInterface
*/
public function hello(string $fetch = self::FETCH_OBJECT)
{
return $this->executeEndpoint(new \Mediaopt\DHL\Api\Authentication\Endpoint\Hello(), $fetch);
}
public static function create($httpClient = null, array $additionalPlugins = array(), array $additionalNormalizers = array())
{
if (null === $httpClient) {
$httpClient = \Http\Discovery\Psr18ClientDiscovery::find();
$plugins = array();
$uri = \Http\Discovery\Psr17FactoryDiscovery::findUrlFactory()->createUri('https://api-eu.dhl.com/parcel/de/account/auth/ropc/v1');
$plugins[] = new \Http\Client\Common\Plugin\AddHostPlugin($uri);
$plugins[] = new \Http\Client\Common\Plugin\AddPathPlugin($uri);
if (count($additionalPlugins) > 0) {
$plugins = array_merge($plugins, $additionalPlugins);
}
$httpClient = new \Http\Client\Common\PluginClient($httpClient, $plugins);
}
$requestFactory = \Http\Discovery\Psr17FactoryDiscovery::findRequestFactory();
$streamFactory = \Http\Discovery\Psr17FactoryDiscovery::findStreamFactory();
$normalizers = array(new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer(), new \Mediaopt\DHL\Api\Authentication\Normalizer\JaneObjectNormalizer());
if (count($additionalNormalizers) > 0) {
$normalizers = array_merge($normalizers, $additionalNormalizers);
}
$serializer = new \Symfony\Component\Serializer\Serializer($normalizers, array(new \Symfony\Component\Serializer\Encoder\JsonEncoder(new \Symfony\Component\Serializer\Encoder\JsonEncode(), new \Symfony\Component\Serializer\Encoder\JsonDecode(array('json_decode_associative' => true)))));
return new static($httpClient, $requestFactory, $serializer, $streamFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Mediaopt\DHL\Api\Authentication\Endpoint;

class DispenseToken extends \Mediaopt\DHL\Api\Authentication\Runtime\Client\BaseEndpoint implements \Mediaopt\DHL\Api\Authentication\Runtime\Client\Endpoint
{
/**
* The client makes a request to the token endpoint by adding the following parameters
using the application/x-www-form-urlencoded format with a character
encoding of UTF-8 in the HTTP request entity-body:
* grant_type __REQUIRED__. Must be set to "password" or "refresh_token".
* client_id __REQUIRED__ (aka client_id (api key))
* client_secret __REQUIRED__ (aka client_secret)
Depending on the grant_type, __additional parameters__ must be provided:
<h3>grant_type=password</h3>
This currently is the only grant type supported.
* username __REQUIRED__. The resource owner username. Aka username for business customer portal
* password __REQUIRED__. The resource owner password. Aka password for business customer portal
*
* @param \Mediaopt\DHL\Api\Authentication\Model\TokenPostBody $requestBody
*/
public function __construct(\Mediaopt\DHL\Api\Authentication\Model\TokenPostBody $requestBody)
{
$this->body = $requestBody;
}
use \Mediaopt\DHL\Api\Authentication\Runtime\Client\EndpointTrait;
public function getMethod() : string
{
return 'POST';
}
public function getUri() : string
{
return '/token';
}
public function getBody(\Symfony\Component\Serializer\SerializerInterface $serializer, $streamFactory = null) : array
{
if ($this->body instanceof \Mediaopt\DHL\Api\Authentication\Model\TokenPostBody) {
return array(array('Content-Type' => array('application/x-www-form-urlencoded')), http_build_query($serializer->normalize($this->body, 'json')));
}
return array(array(), null);
}
public function getExtraHeaders() : array
{
return array('Accept' => array('application/json'));
}
/**
* {@inheritdoc}
*
* @throws \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenBadRequestException
* @throws \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenUnauthorizedException
* @throws \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenForbiddenException
*
* @return null|\Mediaopt\DHL\Api\Authentication\Model\TokenResponse
*/
protected function transformResponseBody(\Psr\Http\Message\ResponseInterface $response, \Symfony\Component\Serializer\SerializerInterface $serializer, ?string $contentType = null)
{
$status = $response->getStatusCode();
$body = (string) $response->getBody();
if (is_null($contentType) === false && (200 === $status && mb_strpos($contentType, 'application/json') !== false)) {
return $serializer->deserialize($body, 'Mediaopt\\DHL\\Api\\Authentication\\Model\\TokenResponse', 'json');
}
if (400 === $status) {
throw new \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenBadRequestException($response);
}
if (401 === $status) {
throw new \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenUnauthorizedException($response);
}
if (403 === $status) {
throw new \Mediaopt\DHL\Api\Authentication\Exception\DispenseTokenForbiddenException($response);
}
}
public function getAuthenticationScopes() : array
{
return array('bearerAuth', 'basicAuth', 'apiKey');
}
}
Loading

0 comments on commit 5ce89da

Please sign in to comment.