suitcasephp / builder
A simple to use SDK builder for PHP.
Requires
- php: ~7.4
- guzzlehttp/guzzle: ^6.5
- tightenco/collect: ^6.16
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-11-19 22:54:59 UTC
README
Suitcase Builder SDK
A simple to use SDK builder for PHP.
Install
Via Composer
$ composer require suitcasephp/builder
How to use
Building the SDK
use Suitcase\Builder\SDK; $sdk = SDK::make('https://api.example.com');
Adding Resources to the SDK:
$sdk->add('posts', [ 'endpoint' => 'posts', 'allows' => [ 'get', 'find', 'create', 'update', 'delete' ] ]);
If you want to pass in a resource that follows the defaults:
$sdk->add('posts');
What this will do is use the string passed in as a name and an endpoint, and pass through the default allows options - basically allowing all operations.
Selecting a resource is pretty simple:
$sdk->use('posts');
What this will do is set the active resource on the SDK allowing you to use the allowed actions. A MethodNotAllowed
is throw if the action is not registered in the allows
array on the resource.
Performing actions on a resource:
$sdk->use('posts')->get(); // return all posts $sdk->use('posts')->find(1); // return the post with an identifier of 1 $sdk->use('posts')->create([]); // create a new post $sdk->use('posts')->update(1, []); // update a post with an identifier of 1 $sdk->use('posts')->delete(1); // delet the post with a identifier of 1
You also have the ability to append sub-resources onto the query using the new append
method:
$sdk->use('posts')->append('comments')->find(1);
This will return a streamed response from the server from the URL: /posts/1/comments
Need to handle authenticated endpoints?
There is a relatively simple approach to this using this package, first you must have an API token or be using basic auth:
$sdk->use('users')->withAuthHeaders('your-api-token', 'Bearer')->get();
What happens here is the auth header will be set to: Authorization: Bearer your-api-token
and added as a header in Guzzle.
By default the type is not needed as it defaults to Bearer
so omit this if you are using a bearer token.
Please note that API authentication is not the main aim of this package, it is just a welcomed addition for some scenarios.