This is the SDK for the reqres API.
Available resources:
- Users
This package is using saloon for handling requests.
You can install the package via composer:
composer require bpotmalnik/reqres-sdk
To get users you might use following method of users resource:
use Bpotmalnik\ReqresSdk\ReqresConnector;
use Illuminate\Support\LazyCollection;
/** @var LazyCollection $users */
$users = ReqresConnector::make()
->users()
->all();
$users->each(function(User $user){
// access to user data object
});
or
foreach($users as $user) {
// access to user data object
}
This method will automatically handle pagination for you and return Laravel Lazy collection for good DX. It can be also iterated an as array.
You can also specify how many users you want to get per page, how many pages you want to get and what page you want to start from. All of those parameters are optional as all method will handle pagination for you.
use Bpotmalnik\ReqresSdk\ReqresConnector;
use Bpotmalnik\ReqresSdk\Resources\Users\Data\User;
$users = ReqresConnector::make()
->users()
->all(
perPage: 1, // how many records per page
maxPages: 1, // how many pages you want to get
startPage: 1, // from which page you want to start
);
// it will get you one user from the first page
$users->each(function(User $user){
// access to user data object
});
To get a single user you might use following method of users resource:
use Bpotmalnik\ReqresSdk\ReqresConnector;
use Bpotmalnik\ReqresSdk\Resources\Users\Data\User;
/** @var User $user */
$user = ReqresConnector::make()
->users()
->get(id: '1');
To create single user you can use following method of users resource:
use Bpotmalnik\ReqresSdk\ReqresConnector;
use Bpotmalnik\ReqresSdk\Resources\Users\Data\User;
use Bpotmalnik\ReqresSdk\Resources\Users\Data\CreateUser;
/** @var User $user */
$user = ReqresConnector::make()
->users()
->create(
new CreateUser(
name: 'morpheus',
job: 'leader'
)
);
If you need access request data for more information you can skip using resources and manually execute requests:
use Bpotmalnik\ReqresSdk\ReqresConnector;
use Bpotmalnik\ReqresSdk\Resources\Users\Requests\GetUsersRequest;
$users = ReqresConnector::make()
->send(new GetUsersRequest);
For more information what is available on request you can check here
Package will throw one of the following exceptions if there is an error:
SaloonException
├── FatalRequestException (Connection Errors)
└── RequestException (Request Errors)
├── ServerException (5xx)
│ ├── InternalServerErrorException (500)
│ ├── ServiceUnavailableException (503)
│ └── GatewayTimeoutException (504)
└── ClientException (4xx)
├── UnauthorizedException (401)
├── ForbiddenException (403)
├── NotFoundException (404)
├── MethodNotAllowedException (405)
├── RequestTimeOutException (408)
├── UnprocessableEntityException (422)
└── TooManyRequestsException (429)
More information here
composer test
Tests are using snapshot testing. If you want to update snapshots you can use following command:
composer test-update-snapshots
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.