Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22: Refactors Xero Client based on guzzle changes #23

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
php-versions: [8.0, 8.1, 8.2]
php-versions: [8.1, 8.2, 8.3]
experimental: [false]
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ Thumbs.db
composer.lock
/coverage

example.php
example.php
.phpunit.result.cache
/.phpunit.cache/
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ Ultimately it is up to the software that uses xeroclient to deal with [serializa

## Dependencies

* PHP 8.0 or greater
* (Deprecated) [guzzlehttp/oauth-subscriber](https://packagist.org/packages/guzzlehttp/oauth-subscriber)
* PHP 8.1 or greater
* [league/oauth2-client](https://packagist.org/packages/league/oauth2-client)
* [guzzlehttp/guzzle](https://packagist.org/packages/guzzlehttp/guzzle)

Expand All @@ -38,15 +37,15 @@ $provider = new \Radcliffe\Xero\XeroProvider([
$url = $provider->getAuthorizationUrl();
```

### Create a guzzle client from an authorization code (see above)
### Create with a guzzle client from an authorization code (see above)

```php
$client = \Radcliffe\Xero\XeroClient::createFromToken('my consumer key', 'my consumer secret', $code, 'authorization_code', 'accounting');
// Store the access token for the next 30 minutes or so if making additional requests.
$tokens = $client->getRefreshedToken();
```

### Create a guzzle client with an access token
### Create with a guzzle client with an access token

```php
$client = \Radcliffe\Xero\XeroClient::createFromToken(
Expand All @@ -61,7 +60,7 @@ $client = \Radcliffe\Xero\XeroClient::createFromToken(
);
```

### Create a guzzle client with a refresh token
### Create with a guzzle client with a refresh token

Access tokens expire after 30 minutes so you can create a new client with a stored refresh token too.

Expand All @@ -88,7 +87,7 @@ try {
'query' => ['where' => 'Name.StartsWith("John")'],
'headers' => ['Accept' => 'application/json'],
];
$response = $client->get('Accounts', $options);
$response = $client->request('GET', 'Accounts', $options);

// Or use something like Symfony Serializer component.
$accounts = json_decode($response->getBody()->getContents());
Expand All @@ -98,6 +97,12 @@ try {

```

### Error handling

If the configured client does not have a valid Xero API URL or if an auth_token is not provided, then XeroRequestException is thrown as part of the Guzzle request.

Previously XeroClient would throw an exception on instantiation, but this is no longer the case. If the initialize method is used directly, XeroClient will probably fail for other reasons.

### Use with a legacy OAuth1 application

Please see the 0.2 branch and versions < 0.3.0.
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "mradcliffe/xeroclient",
"description": "Provides a Guzzle client for use with the Xero Accounting and Payroll APIs.",
"minimum-stability": "stable",
"license": "MIT",
"license": [
"MIT",
"GPL-2.0-or-later"
],
"authors": [
{
"name": "Matthew Radcliffe",
Expand All @@ -21,10 +24,10 @@
}
},
"require": {
"php": "^8",
"league/oauth2-client": "^2.4",
"guzzlehttp/oauth-subscriber": "0.6.0",
"ext-json": "*"
"php": "^8.1",
"league/oauth2-client": "^2",
"ext-json": "*",
"guzzlehttp/guzzle": "^7"
},
"require-dev": {
"ext-openssl": "*",
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/InvalidOptionsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace Radcliffe\Xero\Exception;

/**
* An exception to throw when the client has invalid options.
*
* @deprecated in 0.5.0 and removed in 0.6.0. Use a guzzle request exception
* instead.
* @see \Radcliffe\Xero\Exception\XeroRequestException
*/
class InvalidOptionsException extends \Exception
{
}
9 changes: 9 additions & 0 deletions src/Exception/XeroRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Radcliffe\Xero\Exception;

use GuzzleHttp\Exception\RequestException;

class XeroRequestException extends RequestException
{
}
Loading
Loading