Skip to content

Commit c0255e8

Browse files
committed
commit before token auth
1 parent a750d03 commit c0255e8

File tree

15 files changed

+660
-23
lines changed

15 files changed

+660
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# ref: https://github.com/github/gitignore/blob/master/Composer.gitignore
2+
# ref: https://mustache.github.io/mustache.5.html
23

34
composer.phar
45
/vendor/
@@ -13,3 +14,5 @@ composer.phar
1314

1415
# PHPUnit cache
1516
.phpunit.result.cache
17+
composer.lock
18+
/.env

.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.gitignore
2+
.gitignore
23
.php-cs-fixer.dist.php
34
.travis.yml
45
README.md
@@ -29,6 +30,7 @@ docs/Model/TransactionList.md
2930
docs/Model/TransactionListTransactionsInner.md
3031
git_push.sh
3132
lib/Accounts/DefaultApi.php
33+
lib/ApiClient.php
3234
lib/ApiException.php
3335
lib/Configuration.php
3436
lib/HeaderSelector.php

.openapi-generator/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ additionalProperties:
1818
developerOrganizationUrl: https://spojenet.cz
1919
variableNamingConvention: camelCase
2020
composerPackageName: spojenet/csas-accountsapi
21+
files:
2122
composer.mustache:
2223
folder: .
2324
destinationFilename: composer.json
2425
gitignore.mustache:
2526
folder: .
2627
destinationFilename: .gitignore
28+
ApiClient.mustache:
29+
folder: lib
30+
destinationFilename: ApiClient.php
2731

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* ApiClient
5+
* PHP version 7.4+
6+
*
7+
* @category Class
8+
* @package {{invokerPackage}}
9+
* @author OpenAPI Generator team
10+
* @link https://openapi-generator.tech
11+
*/
12+
13+
{{>partial_header}}
14+
namespace {{invokerPackage}};
15+
16+
/**
17+
* Description of ApiClient
18+
*
19+
* @author vitex
20+
*/
21+
class ApiClient extends \GuzzleHttp\Client
22+
{
23+
/**
24+
* ClientID obtained from Developer Portal - when you registered your app with us.
25+
* @var string
26+
*/
27+
protected string $apiKey;
28+
29+
/**
30+
* @var string $token
31+
*/
32+
protected string $token;
33+
34+
/**
35+
* Use sandbox for api calls ?
36+
* @var boolean
37+
*/
38+
protected bool $sandBoxMode = false;
39+
40+
/**
41+
* @inheritDoc
42+
*
43+
* $config['apikey'] - obtained from Developer Portal - when you registered your app with us.
44+
* $config['token'] - your access token
45+
* $config['sandbox'] = true to use //api/csas/public/sandbox/v3/* endpoints
46+
*
47+
* @param array $config
48+
* @throws \Exception ACCESS_TOKEN is not set
49+
* @throws \Exception API_KEY is not set
50+
*/
51+
public function __construct(array $config = [])
52+
{
53+
if (array_key_exists('apikey', $config) === false) {
54+
$this->apiKey = \Ease\Shared::cfg('API_KEY');
55+
} else {
56+
$this->apiKey = $config['apikey'];
57+
}
58+
59+
if (array_key_exists('token', $config) === false) {
60+
$this->token = \Ease\Shared::cfg('API_TOKEN');
61+
} else {
62+
$this->token = $config['token'];
63+
}
64+
65+
if (array_key_exists('debug', $config) === false) {
66+
$config['debug'] = \Ease\Shared::cfg('API_DEBUG', false);
67+
}
68+
69+
if (array_key_exists('sandbox', $config)) {
70+
$this->sandBoxMode = boolval($config['sandbox']);
71+
} else {
72+
$this->token = boolval(\Ease\Shared::cfg('SANDBOX_MODE'));
73+
}
74+
75+
$config['base_uri'] = ($this->sandBoxMode ? 'https://webapi.developers.erstegroup.com/api/csas/public/sandbox' : 'https://www.csas.cz/webapi/api') . '/v3/accounts/' ;
76+
$config['headers']['web-api-key'] = $this->apiKey;
77+
$config['headers']['authorization'] = 'Bearer '.$this->token;
78+
79+
parent::__construct($config);
80+
}
81+
82+
/**
83+
* ClientID obtained from Developer Portal
84+
*
85+
* @return string
86+
*/
87+
public function getApiKey()
88+
{
89+
return $this->apiKey;
90+
}
91+
92+
public function getAccessToken(): string
93+
{
94+
return $this->token;
95+
}
96+
97+
/**
98+
* Use mocking uri for api calls ?
99+
*
100+
* @return boolean
101+
*/
102+
public function getSandBoxMode(): bool
103+
{
104+
return $this->sandBoxMode;
105+
}
106+
107+
/**
108+
* Merges default options into the array.
109+
*
110+
* @param array $options Options to modify by reference
111+
*/
112+
private function prepareDefaults(array $options): array
113+
{
114+
$options['headers']['User-Agent'] = $this->config->getUserAgent();
115+
return parent::prepareDefaults($options);
116+
}
117+
118+
}

.openapi-generator/templates/Configuration.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Configuration
9292
*
9393
* @var string
9494
*/
95-
protected $userAgent = '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}{{^artifactVersion}}1.0.0{{/artifactVersion}}/PHP{{/httpUserAgent}}';
95+
protected $userAgent = '{{{httpUserAgent}}}{{^httpUserAgent}}{{invokerPackage}}/{{{artifactVersion}}}{{^artifactVersion}}0.0.0{{/artifactVersion}}/PHP{{/httpUserAgent}}';
9696
9797
/**
9898
* Debug switch (default set to false)
@@ -316,7 +316,7 @@ class Configuration
316316
*/
317317
public function getUserAgent()
318318
{
319-
return $this->userAgent;
319+
return str_replace('0.0.0', \Ease\Shared::appVersion(), $this->userAgent);
320320
}
321321

322322
/**

.openapi-generator/templates/api.mustache

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use GuzzleHttp\Exception\RequestException;
2626
use GuzzleHttp\Psr7\MultipartStream;
2727
use GuzzleHttp\Psr7\Request;
2828
use GuzzleHttp\RequestOptions;
29+
use {{invokerPackage}}\ApiClient;
2930
use {{invokerPackage}}\ApiException;
3031
use {{invokerPackage}}\Configuration;
3132
use {{invokerPackage}}\HeaderSelector;
@@ -61,6 +62,20 @@ use {{invokerPackage}}\ObjectSerializer;
6162
*/
6263
protected $hostIndex;
6364
65+
/**
66+
* ApiKey obtained from Developer Portal - when you registered your app with us.
67+
*
68+
* @link https://developers.erstegroup.com/portal/organizations/vitezslav-dvorak/applications/ Grab Your API key here
69+
* @var string
70+
*/
71+
protected string $apiKey;
72+
protected string $token;
73+
74+
/**
75+
* Use the /api/csas/public/sandbox/v3/* path for endpoints ?
76+
*/
77+
protected $sandBoxMode = false;
78+
6479
/** @var string[] $contentTypes **/
6580
public const contentTypes = [{{#operation}}
6681
'{{{operationId}}}' => [{{#consumes}}
@@ -82,10 +97,49 @@ use {{invokerPackage}}\ObjectSerializer;
8297
HeaderSelector $selector = null,
8398
$hostIndex = 0
8499
) {
85-
$this->client = $client ?: new Client();
100+
$this->client = $client ?: new ApiClient();
86101
$this->config = $config ?: Configuration::getDefaultConfiguration();
87102
$this->headerSelector = $selector ?: new HeaderSelector();
88103
$this->hostIndex = $hostIndex;
104+
if (method_exists($this->client, 'getApiKey')) {
105+
$config['headers']['web-api-key'] = $this->client->getApiKey();
106+
$this->setApiKey($this->client->getApiKey());
107+
}
108+
if (method_exists($this->client, 'getAccessToken')) {
109+
$config['headers']['authorization'] = 'Bearer '.$this->client->getAccessToken();
110+
$this->setAccessToken($this->client->getAccessToken());
111+
}
112+
if (method_exists($this->client, 'getSandBoxMode')) {
113+
$this->setSandBoxMode($this->client->getSandBoxMode());
114+
}
115+
116+
$config['base_uri'] = ($this->sandBoxMode ? 'https://webapi.developers.erstegroup.com/api/csas/public/sandbox' : 'https://www.csas.cz/webapi/api') . '/v3/accounts/' ;
117+
118+
}
119+
120+
/**
121+
* @param string $apiKey Set you API_KEY here
122+
*/
123+
public function setApiKey(string $apiKey): void
124+
{
125+
$this->config->setApiKey('web-api-key', $apiKey);
126+
}
127+
128+
/**
129+
* @param string $token set access token
130+
*/
131+
public function setAccessToken($token): void
132+
{
133+
$this->config->setAccessToken($token);
134+
}
135+
136+
/**
137+
* @param boolean $sandboxing Use mocking api for development purposes ?
138+
*/
139+
public function setSandBoxMode(bool $sandboxing): void
140+
{
141+
$this->sandBoxMode = $sandboxing;
142+
$this->config->setHost('https://'.($this->sandBoxMode ? 'webapi.developers.erstegroup.com' : 'www.csas.cz'));
89143
}
90144

91145
/**
@@ -622,7 +676,7 @@ use {{invokerPackage}}\ObjectSerializer;
622676
{{/minItems}}
623677
{{/hasValidation}}{{/allParams}}
624678

625-
$resourcePath = '{{{path}}}';
679+
$resourcePath = ($this->sandBoxMode ? '/api/csas/public/sandbox' : '/webapi/api').'/v3/accounts{{{path}}}';
626680
$formParams = [];
627681
$queryParams = [];
628682
$headerParams = [];
@@ -855,6 +909,9 @@ use {{invokerPackage}}\ObjectSerializer;
855909
}
856910
}
857911

912+
$options['headers']['web-api-key'] = $this->config->getApiKey('web-api-key');
913+
$options['headers']['authorization'] = 'Bearer '.$this->config->getAccessToken();
914+
858915
return $options;
859916
}
860917
}

Examples/auth.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$idpParams = [
2020
'client_id' => Shr::cfg('CLIENT_ID'),
2121
'response_type' => 'code',
22-
'prompt'=>'consent',
22+
'prompt' => 'consent',
2323
'redirect_uri' => Shr::cfg('REDIRECT_URI'),
2424
'state' => Fnc::randomString(),
2525
'scope' => implode('%20', [
@@ -32,7 +32,7 @@
3232

3333
$idpUri = Fnc::addUrlParams($idpLink.'/auth', $idpParams);
3434

35-
if(PHP_SAPI == 'cli'){
35+
if (PHP_SAPI === 'cli') {
3636
echo $idpUri;
3737
} else {
3838
echo '<a href='.$idpUri.'>'.$idpUri.'</a>';

Examples/balance.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
<?php
22

3-
require_once(dirname(__DIR__) . '/vendor/autoload.php');
3+
use Ease\Shared as Shr;
4+
use Ease\Functions as Fnc;
45

5-
$apiInstance = new \SpojeNET\Csas\Accounts\DefaultApi(
6-
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
7-
// This is optional, `GuzzleHttp\Client` will be used as default.
8-
new GuzzleHttp\Client()
9-
);
10-
$id = 'CZ1208000000000259459101'; // string | Opaque system ID of the account
6+
require_once dirname(__DIR__) . '/vendor/autoload.php';
7+
8+
Shr::init([], dirname(__DIR__) . '/.env');
9+
10+
$apiInstance = new \SpojeNET\Csas\Accounts\DefaultApi(new SpojeNET\Csas\ApiClient(
11+
[
12+
'apikey' => \Ease\Shared::cfg('API_KEY'),
13+
'token' => \Ease\Shared::cfg('API_TOKEN'),
14+
'debug' => \Ease\Shared::cfg('API_DEBUG', false),
15+
'sandbox' => \Ease\Shared::cfg('SANDBOX_MODE')
16+
]
17+
));
1118

1219
try {
13-
$result = $apiInstance->getAccountBalance($id);
20+
$result = $apiInstance->getAccountBalance(\Ease\Shared::cfg('ACCOUNT'));
1421
print_r($result);
1522
} catch (Exception $e) {
1623
echo 'Exception when calling DefaultApi->getAccountBalance: ', $e->getMessage(), PHP_EOL;

Examples/redirectedFromBank.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// Start session
2222
session_start();
2323

24-
if (PHP_SAPI == 'cli') {
24+
if (PHP_SAPI === 'cli') {
2525
parse_str($argv[1], $params);
2626
$code = array_key_exists('code', $params) ? $params['code'] : '';
2727
} else {

0 commit comments

Comments
 (0)