Skip to content

Restmold, Model-based HTTP client for Laravel applications.

Notifications You must be signed in to change notification settings

MrLijan/Restmold

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Restmold (pronounced /restː/ /mold:/) is a Laravel package for modeling 3rd party HTTP services and as an HTTP client. Using this package will allow you to model your external HTTP services. The magic behind Restmold is that you don't need to repeatedly re-code your API class services.

Inspiration

The inspiration for this project started when the company I worked for migrated our giant monolith into a bunch of small microservices. Time passed, and we found ourselves writing API services repeatedly as the number of services grew. So, inspired by the flow of Laravel's models, Restmold has born out.


⚠️ The package supports Laravel 7 and above.


Installation

composer require mrlijan/restmold

Usage

Restmold is so easy-to-use; you just need to run the generate command to create a new API model. As a result, a new folder named "ApiModels" will be created under the \App folder, including the new concrete class.

php artisan restmold:generate <service name>



Class Structure

Each restmold concrete class will derive its props and methods from the RestModel abstract class and should also implement the following properties and methods to work properly:

Type Name Description
Property baseURI The base uri for that specific service
Method headers Return the request's headers. This can be used for authentication
Method routes Used to construct the service's structure



Class Configuration

the routes method is being used to construct the service's structure. This means that every array index listed below will be determined as a method for this service.

Each index of that array will be constructed as follows:

protected function routes(): array
{
    return [
        'methodName' => [
            'method' => 'GET', // The reuqest's method
            'path' => '/index' // The reuqest's endpoint
        ],
    ];
}

Config with Query Params

Your route includes query params? We've got your back! Just use the regular syntax, excluding the values. For example:

protected function routes(): array
{
    return [
        'list' => [
            'method' => 'GET',
            'path' => '/students?name&age'
        ],
    ];
}



Implementations

Using Query Params

Once everything is ready and configured, the service is prepared to use.

use App\ApiModels\StudentsAPIModel;

class StudentsController extends BaseController
{
    public function listByParams(StudentsAPIModel $students_api)
    {
        return $students_api->list([
            'query' => [
                'name' => 'Brendon',
                'age' => '22'
            ]
        ]);
    }
}

Using request body

use App\ApiModels\StudentsAPIModel;

class StudentsController extends BaseController
{
    public function createNewStudent(StudentsAPIModel $students_api)
    {
        return $students_api->create([
            'body' => [
                'firstName' => 'John',
                'lastName' => 'Dow'
                'age' => '24',
                'email' => '[email protected]'
            ]
        ]);
    }
}



Piping Requests & Responses

Sometimes, collective pipes are necessary in some services, and for these cases, a request and response pipes have been created. In order to use them just override the following methods:

protected function requestPipe(Request $request): Request
{
    // ... manipulate the data, and return the request
    return $request;
}


protected function responsePipe(Response $response): Response
{
    // ... manipulate the data, and return the response
    return $response;
}