All Projects → ezdeliveryco → Snorlax

ezdeliveryco / Snorlax

Licence: mit
A lightweight REST client that gives you full control of your resources

Projects that are alternatives of or similar to Snorlax

Restana
Super fast and minimalist framework for building REST micro-services.
Stars: ✭ 341 (+164.34%)
Mutual labels:  microservices, restful
Go Relax
Framework for building RESTful API's in Go
Stars: ✭ 155 (+20.16%)
Mutual labels:  microservices, restful
Adnc
微服务框架,同时也适用于单体架构系统的开发。支持经典三层与DDD架构开发模式、集成了一系列主流稳定的微服务配套技术栈。一个前后端分离的框架,前端基于Vue、后端基于.Net Core 3.1构建。
Stars: ✭ 223 (+72.87%)
Mutual labels:  microservices, restful
Go Api Boilerplate
Go Server/API boilerplate using best practices DDD CQRS ES gRPC
Stars: ✭ 373 (+189.15%)
Mutual labels:  microservices, restful
Ganesha
🐘 A Circuit Breaker pattern implementation for PHP applications.
Stars: ✭ 384 (+197.67%)
Mutual labels:  microservices, guzzle
Nakadi
A distributed event bus that implements a RESTful API abstraction on top of Kafka-like queues
Stars: ✭ 734 (+468.99%)
Mutual labels:  microservices, restful
Phpboot
☕️ 🚀 tiny & fast PHP framework for building Microservices/RESTful APIs, with useful features: IOC, Hook, ORM, RPC, Swagger, Annotation, Parameters binding, Validation, etc.
Stars: ✭ 638 (+394.57%)
Mutual labels:  microservices, restful
Microservices With Lumen
A Lumen based microservice ready to deploy with guzzle for consumption of api and OAuth 2
Stars: ✭ 115 (-10.85%)
Mutual labels:  microservices, guzzle
Guzzle Advanced Throttle
A Guzzle middleware that can throttle requests according to (multiple) defined rules. It is also possible to define a caching strategy, e.g. get the response from cache when the rate limit is exceeded or always get a cached value to spare your rate limits. Using wildcards in host names is also supported.
Stars: ✭ 120 (-6.98%)
Mutual labels:  guzzle
Okhttp Okgo
OkGo - 3.0 震撼来袭,该库是基于 Http 协议,封装了 OkHttp 的网络请求框架,比 Retrofit 更简单易用,支持 RxJava,RxJava2,支持自定义缓存,支持批量断点下载管理和批量上传管理功能
Stars: ✭ 10,407 (+7967.44%)
Mutual labels:  restful
Nirum
Nirum: IDL compiler and RPC/distributed object framework for microservices
Stars: ✭ 119 (-7.75%)
Mutual labels:  microservices
Rebus
🚌 Simple and lean service bus implementation for .NET
Stars: ✭ 1,733 (+1243.41%)
Mutual labels:  microservices
Backstage
Backstage is an open platform for building developer portals
Stars: ✭ 14,296 (+10982.17%)
Mutual labels:  microservices
Apisix Docker
the docker for Apache APISIX
Stars: ✭ 119 (-7.75%)
Mutual labels:  microservices
Phantoscope
Open Source, Cloud Native, RESTful Search Engine Powered by Neural Networks
Stars: ✭ 127 (-1.55%)
Mutual labels:  microservices
Knotx
Knot.x is a highly-efficient and scalable integration framework designed to build backend APIs
Stars: ✭ 119 (-7.75%)
Mutual labels:  microservices
White Jotter
白卷是一款使用 Vue+Spring Boot 开发的前后端分离项目,附带全套开发教程。(A simple CMS developed by Spring Boot and Vue.js with development tutorials)
Stars: ✭ 1,838 (+1324.81%)
Mutual labels:  restful
Basalt
高性能的分布式的专门空间优化的 Bitmap 服务, 高效检查数据是否存在,日活统计,签到,打点等等
Stars: ✭ 128 (-0.78%)
Mutual labels:  microservices
Eshoponcontainersddd
Fork of dotnet-architecture/eShopOnContainers in full DDD/CQRS design using my own patterns
Stars: ✭ 126 (-2.33%)
Mutual labels:  microservices
Watsonwebserver
Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
Stars: ✭ 125 (-3.1%)
Mutual labels:  restful

Snorlax

Build Status StyleCI codecov MIT licensed

A light-weight RESTful client built on top of Guzzle that gives you full control of your API's resources. Its based on method definitions and parameters for your URLs. See the usage below.

Basic Usage

<?php

use Snorlax\RestResource;
use Snorlax\RestClient;

class PokemonResource extends RestResource
{
    public function getBaseUri()
    {
        // You don't want a raw value like this, use an environment variable :)
        return 'http://localhost/api/pokemons';
    }

    public function getActions()
    {
        return [
            'all' => [
                'method' => 'GET',
                'path' => '/',
            ],
            'get' => [
                'method' => 'GET',
                'path' => '/{0}.json',
            ],
            'create' => [
                'method' => 'POST',
                'path' => '/',
            ],
        ];
    }
}

$client = new RestClient([
    'resources' => [
        'pokemons' => PokemonResource::class,
    ],
]);

// GET http://localhost/api/pokemons?sort=id:asc
$response = $client->pokemons->all([
    'query' => [
        'sort' => 'id:asc',
    ],
]);

// GET http://localhost/api/pokemons/143.json?fields=id,name
$response = $client->pokemons->get(143, [
    'query' => [
        'fields' => 'id,name',
    ],
]);

// POST http://localhost/api/pokemons
$response = $client->pokemons->create([
    'body' => [
        'name' => 'Bulbasaur',
    ],
]);

As you can see, each action on your resource is defined an array with two keys, method, defining the HTTP method for the request and path, which defines the path from the base URI returned by the getBaseUri method. You can mock URLs using environment variables as you wish.

Snorlax assume your API returns JSON, so it already returns an StdClass object with the response, decoded by json_decode. If you want to get the raw object returned by Guzzle, use $client->resource->getLastResponse().

Amending the response

As noted above, Snorlax returns an StdClass object, however Resources may overwrite the ->parse() method to manipulate the returned response. This is useful when an API returns a nested set of data such as {'pokemon': {'name':'Mew'}} and you only want the actual data (in this case pokemon). In this example we could use

public function parse($method, $response)
{
    return $response->pokemon;
}

This would return the actual pokemon object. Another scario is that you may want to return a Laravel Collection (Illuminate\Support\Collection) of objects, you could simply do

public function parse($method, $response)
{
    return collect($response->pokemon);
}

The $method argument is the name of the method which was called to perform the request, such as 'all', or 'get'. This is useful to manipulate different response, such as

public function parse($method, $response)
{
    switch ($method) {
        case 'all':
            return collect($response->pokemon);
            break;
        case 'get':
            return $response->pokemon;
            break;
    }
}

Another usage could be to cast certain fields are data types. In this example, we'll cast any fields called created_at or updated_at to Carbon isntances

public function parse($action, $response)
{
    $date_fields = [
        'created_at',
        'updated_at',
    ];

    $response = $response->pokemon;

    foreach ($date_fields as $date_field) {
        if (property_exists($response, $date_field)) {
            $response->{$date_field} = Carbon::parse($response->{$date_field});
        }
    }

    return $response;
}

Sending parameters and headers

As said before, Snorlax is built on top of Guzzle, so it works basically the same way on passing headers, query strings and request bodies.

<?php

$pokemons = $client->pokemons->all([
    'query' => [
        'sort' => 'name',
        'offset' => 0,
        'limit' => 150,
    ],
    'headers' => [
        'X-Foo' => 'Bar',
    ],
]);

$pokemons = $client->pokemons->create([
    'body' => [
        'name' => 'Ivysaur',
        'attacks' => [
            'Tackle',
            'Leer',
        ],
    ],
]);

Changing client options

If you want to set default headers for every request you send to the default guzzle client, just use the headers options on your params config key, just like the Guzzle docs.

<?php

$client = new Snorlax\RestClient([
    'client' => [
        'params' => [
            'headers' => [
                'X-Foo' => 'Bar',
            ],
            'defaults' => [
                'debug' => true,
            ],
            'cache' => true,
        ],
    ],
]);

Setting a base URI

If all your resources are under the same base URI, you can pass it on the constructor instead of declaring on the resource class.

<?php

$client = new Snorlax\RestClient([
    'client' => [
        'params' => [
            'base_uri' => 'http://localhost/api',
        ],
    ],
]);

Using a custom client

If you don't want to use Guzzle's default client (or want to mock one), Snorlax accepts any class that implements GuzzleHttp\ClientInterface, so just pass your custom client in the constructor. Can be an instance or a callable.

<?php

class MyOwnClient implements GuzzleHttp\ClientInterface
{
    private $config;

    public function __construct(array $params)
    {
        $this->config = $params;
    }
}

// Using a callable to instantiate a new client everytime
$client = new Snorlax\RestClient([
    'client' => [
        'custom' => function(array $params) {
            return new MyOwnClient($params);
        },
        'params' => [
            'param1' => 'value',
        ],
    ],
]);

$client = new Snorlax\RestClient([
    'client' => [
        'custom' => new MyOwnClient([
            'param1' => 1,
        ]),
    ],
]);

Using a custom cache strategy & driver

If you don't want to use the VolatileRuntime cache driver or even want to change cache strategy, Snorlax allow you to inject any of the stragies and storages provided by kevinrob/guzzle-cache-middleware.

<?php

$cacheDriver = \Illuminate\Support\Facades\Cache::store('redis');
$cacheStorage = new \Kevinrob\GuzzleCache\Storage\LaravelCacheStorage($cacheDriver);
$cacheStrategy = new \Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy($cacheStorage);

$client = new Snorlax\RestClient([
    'client' => [
        'cacheStrategy' => $cacheStrategy,
        'params' => [
            'cache' => true,
        ],
    ],
]);

Using parallel requests

The parallel pool is made on the top of Guzzle, because of that, you should be using Guzzle.

<?php
use Snorlax\RestClient;
use Snorlax\RestPool;
use Snorlax\RestResource;

class PostResource extends RestResource
{
    public function getBaseUri()
    {
        return 'https://jsonplaceholder.typicode.com/posts';
    }

    public function getActions()
    {
        return [
            'all' => [
                'method' => 'GET',
                'path' => '/',
            ],
            'show' => [
                'method' => 'GET',
                'path' => '/{0}',
            ],
        ];
    }
}

$client = new RestClient([
    'resources' => [
        'posts' => PostResource::class,
    ],
]);

$pool = new RestPool();

$pool->addResource('postsByUserOne', $client, 'posts.all', [
    'query' => [
        'userId' => '1',
        'cache' => rand(11111, 99999), // bypass cache
    ],
]);

$pool->addResource('postsByUserTwo', $client, 'posts.show', [
    'parameters' => [
        2
    ],
    'query' => [
        'userId' => '2',
        'cache' => rand(11111, 99999), // bypass cache
    ],
]);

$requests = $pool->send();

The result of $requests is a StdClass with postsByUserOne and postsByUserTwo as properties.

Reconnections

Is possible to reconnect when Guzzle throws a GuzzleHttp\Exception\ConnectException. By default, Snorlax tries to reconnect 3 times.

If you want, you can change the number of tries with the retries parameter.

<?php

$pokemons = $client->pokemons->all([
    'retries' => 3,
    'query' => [
        'limit' => 150,
    ],
]);

Contributing

Please see the CONTRIBUTING.md file for guidelines.

License

Please see the LICENSE file for License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].