Git Product home page Git Product logo

reqres-sdk's Introduction

Reqres SDK

This is the SDK for the reqres API.

Available resources:

  • Users

This package is using saloon for handling requests.

Installation

You can install the package via composer:

composer require bpotmalnik/reqres-sdk

Usage

Getting users

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
});

Getting single user

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');

Create a single user

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'
        )
    );

Accessing request data directly

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

Error handling

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

Testing

composer test

Tests are using snapshot testing. If you want to update snapshots you can use following command:

composer test-update-snapshots

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

reqres-sdk's People

Contributors

bpotmalnik avatar dependabot[bot] avatar github-actions[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.