Skip to content

Commit 1068981

Browse files
committed
prepairing for 0.4.0 release with refactored Client
1 parent b19cdd7 commit 1068981

File tree

6 files changed

+74
-93
lines changed

6 files changed

+74
-93
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ vendor
33
composer.lock
44

55
samples/client_tokens.json
6+
playground/*

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# CHANGELOG
22

3-
## [0.3.x (Unreleased)](https://github.com/onlime/bexio-api-client/compare/0.3.1...main)
3+
## [0.4.x (Unreleased)](https://github.com/onlime/bexio-api-client/compare/0.3.1...main)
4+
5+
## [0.4.0 (2022-03-24)](https://github.com/onlime/bexio-api-client/compare/0.3.1...0.4.0)
46

57
- Fix release comparison links in CHANGELOG.
68
- Refactored most `Client` methods into `AbstractClient` for better extensibility.
9+
- BC break: The redirect URL does now need to be passed as second argument of the `Client::authenticate($scopes, $redirectUrl)` method, no longer as constructor argument.
710

811
## [0.3.1 (2022-03-23)](https://github.com/onlime/bexio-api-client/compare/0.3.0...0.3.1)
912

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ use Bexio\Client;
4141
*/
4242
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
4343
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
44-
$redirectUrl = 'http://bexio-api-php-client.test/auth.php';
44+
$redirectUrl = 'http://localhost:8000/auth.php';
4545
$scopes = ['openid', 'profile', 'contact_edit', 'offline_access', 'kb_invoice_edit', 'bank_payment_edit'];
4646
$tokensFile = 'client_tokens.json';
4747

48-
$client = new Client($clientId, $clientSecret, $redirectUrl);
49-
$client->authenticate($scopes);
48+
$client = new Client($clientId, $clientSecret);
49+
$client->authenticate($scopes, $redirectUrl);
5050
$client->persistTokens($tokensFile);
5151
```
5252

@@ -76,6 +76,21 @@ $bexioContact = new Contact($client);
7676
$contacts = $bexioContact->getContacts();
7777
```
7878

79+
Usage:
80+
81+
1. Ensure you have allowed your local direct URL in [Bexio Developer Portal](https://developer.bexio.com/), e.g. `http://localhost:8000/auth.php`
82+
2. Fill `$clientId` and `$clientSecret` in both `samples/auth.php` and `samples/sample.php` with your Bexio API credentials.
83+
3. Fire up the local dev server (see below).
84+
4. Access http://localhost:8000/auth.php in your browser.
85+
5. Authenticate with your Bexio login to provide access to the app via access token.
86+
6. Bexio will redirect you back to http://localhost:8000/auth.php which will present: **Sucessfully authenticated. [Proceed to sample.php](http://localhost:8000/auth.php)**
87+
7. Once you access http://localhost:8000/sample.php, you should be already authenticated (using the current token stored in `client_tokens.json`) and your contacts are listed.
88+
89+
```bash
90+
$ cd samples/
91+
$ php -S localhost:8000
92+
```
93+
7994
## Authors
8095

8196
Author of this awesome package is [Philip Iezzi (Onlime GmbH)](https://www.onlime.ch/).

samples/auth.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
*/
1313
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
1414
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
15-
$redirectUrl = 'http://bexio-api-php-client.test/auth.php';
15+
$redirectUrl = 'http://localhost:8000/auth.php';
1616
$scopes = ['openid', 'profile', 'contact_edit', 'offline_access', 'kb_invoice_edit', 'bank_payment_edit'];
1717
$tokensFile = 'client_tokens.json';
1818

19-
$client = new Client($clientId, $clientSecret, $redirectUrl);
20-
$client->authenticate($scopes);
19+
$client = new Client($clientId, $clientSecret);
20+
$client->authenticate($scopes, $redirectUrl);
2121
$client->persistTokens($tokensFile);
2222
?>
2323
Sucessfully authenticated. <a href="sample.php">Proceed to sample.php</a>

src/Bexio/AbstractClient.php

Lines changed: 18 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,20 @@ abstract class AbstractClient
1515
const METHOD_DELETE = 'DELETE';
1616
const METHOD_PATCH = 'PATCH';
1717

18-
private array $config;
19-
private string $accessToken;
20-
private string $refreshToken;
21-
22-
/**
23-
* Client constructor.
24-
*
25-
* @param string $clientId
26-
* @param string $clientSecret
27-
* @param string|null $redirectUrl
28-
*/
29-
public function __construct(string $clientId, string $clientSecret, string $redirectUrl = null)
30-
{
31-
$this->config = [
32-
'clientId' => $clientId,
33-
'clientSecret' => $clientSecret,
34-
'redirectUrl' => $redirectUrl,
35-
];
36-
}
37-
38-
public function setClientId($clientId)
39-
{
40-
$this->config['clientId'] = $clientId;
41-
}
42-
43-
public function getClientId()
44-
{
45-
return $this->config['clientId'];
46-
}
47-
48-
public function setClientSecret($clientId)
49-
{
50-
$this->config['clientSecret'] = $clientId;
51-
}
18+
private ?string $accessToken = null;
19+
private ?string $refreshToken = null;
5220

53-
public function getClientSecret()
54-
{
55-
return $this->config['clientSecret'];
56-
}
57-
58-
public function setRedirectUrl($redirectUrl)
59-
{
60-
$this->config['redirectUrl'] = $redirectUrl;
61-
}
62-
63-
public function getRedirectUrl()
64-
{
65-
return $this->config['redirectUrl'];
66-
}
21+
public function __construct(
22+
public string $clientId,
23+
public string $clientSecret
24+
) {}
6725

6826
public function setAccessToken(string $accessToken)
6927
{
7028
$this->accessToken = $accessToken;
7129
}
7230

73-
public function getAccessToken()
31+
public function getAccessToken(): ?string
7432
{
7533
return $this->accessToken;
7634
}
@@ -80,7 +38,7 @@ public function setRefreshToken(string $refreshToken)
8038
$this->refreshToken = $refreshToken;
8139
}
8240

83-
public function getRefreshToken()
41+
public function getRefreshToken(): ?string
8442
{
8543
return $this->refreshToken;
8644
}
@@ -114,20 +72,21 @@ public function getOpenIDConnectClient(): OpenIDConnectClient
11472
{
11573
$oidc = new OpenIDConnectClient(
11674
self::PROVIDER_URL,
117-
$this->getClientId(),
118-
$this->getClientSecret()
75+
$this->clientId,
76+
$this->clientSecret
11977
);
12078
$oidc->setAccessToken($this->accessToken);
12179
return $oidc;
12280
}
12381

124-
public function authenticate($scopes)
82+
public function authenticate(string|array $scopes, string $redirectUrl)
12583
{
12684
if (!is_array($scopes)) {
12785
$scopes = explode(' ', $scopes);
12886
}
87+
12988
$oidc = $this->getOpenIDConnectClient();
130-
$oidc->setRedirectURL($this->getRedirectUrl());
89+
$oidc->setRedirectURL($redirectUrl);
13190
$oidc->addScope($scopes);
13291
$oidc->authenticate();
13392

@@ -163,35 +122,9 @@ public function getFullApiUrl(string $path = '')
163122
]));
164123
}
165124

166-
public function get(string $path, array $queryParams = [])
167-
{
168-
return $this->request($path, self::METHOD_GET, queryParams: $queryParams);
169-
}
170-
171-
public function post(string $path, array $data = [], array $queryParams = [])
172-
{
173-
return $this->request($path, self::METHOD_POST, $data, $queryParams);
174-
}
175-
176-
public function put(string $path, array $data = [], array $queryParams = [])
177-
{
178-
return $this->request($path, self::METHOD_PUT, $data, $queryParams);
179-
}
180-
181-
public function delete(string $path, array $data = [], array $queryParams = [])
182-
{
183-
return $this->request($path, self::METHOD_DELETE, $data, $queryParams);
184-
}
185-
186-
public function patch(string $path, array $data = [], array $queryParams = [])
187-
{
188-
return $this->request($path, self::METHOD_PATCH, $data, $queryParams);
189-
}
190-
191-
abstract protected function request(
192-
string $path = '',
193-
string $method = self::METHOD_GET,
194-
array $data = [],
195-
array $queryParams = []
196-
);
125+
abstract public function get(string $path, array $queryParams = []);
126+
abstract public function post(string $path, array $data = [], array $queryParams = []);
127+
abstract public function put(string $path, array $data = [], array $queryParams = []);
128+
abstract public function delete(string $path, array $data = [], array $queryParams = []);
129+
abstract public function patch(string $path, array $data = [], array $queryParams = []);
197130
}

src/Bexio/Client.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,37 @@
77

88
class Client extends AbstractClient
99
{
10-
protected function request(string $path = '', string $method = self::METHOD_GET, array $data = [], array $queryParams = [])
10+
public function get(string $path, array $queryParams = [])
1111
{
12+
return $this->request($path, queryParams: $queryParams);
13+
}
14+
15+
public function post(string $path, array $data = [], array $queryParams = [])
16+
{
17+
return $this->request($path, self::METHOD_POST, $data, $queryParams);
18+
}
19+
20+
public function put(string $path, array $data = [], array $queryParams = [])
21+
{
22+
return $this->request($path, self::METHOD_PUT, $data, $queryParams);
23+
}
24+
25+
public function delete(string $path, array $data = [], array $queryParams = [])
26+
{
27+
return $this->request($path, self::METHOD_DELETE, $data, $queryParams);
28+
}
29+
30+
public function patch(string $path, array $data = [], array $queryParams = [])
31+
{
32+
return $this->request($path, self::METHOD_PATCH, $data, $queryParams);
33+
}
34+
35+
protected function request(
36+
string $path = '',
37+
string $method = self::METHOD_GET,
38+
array $data = [],
39+
array $queryParams = []
40+
) {
1241
$options = [
1342
'headers' => [
1443
'Authorization' => 'Bearer ' . $this->getAccessToken(),

0 commit comments

Comments
 (0)