Giter VIP home page Giter VIP logo

laravel-transporter's Introduction

Transporter

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.

Installation

You can install the package via composer:

composer require juststeveking/laravel-transporter

You can publish the config file with:

php artisan vendor:publish --provider="JustSteveKing\Transporter\TransporterServiceProvider" --tag="transporter-config"

The contents of the published config file:

return [
    'base_uri' => env('TRANSPORTER_BASE_URI'),
];

Generating Request

To generate an API request to use with Transporter, you can use the Artisan make command:

php artisan make:api-request NameOfYourRequest

This will by default publish as: app/Transporter/Requests/NameOfYourRequest.php

Usage

Transporter Requests are an extention of Laravels PendingRequest so all of the methods available on a Pending Request is available to you on your requests.

Also when you send the request, you will receive a Illuminate\Http\Client\Response back, allowing you to do things such as collect($key) and json() and failed() very easily. We are simply just shifting how we send it into a class based approach.

TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ])
    ->send()
    ->json();

When building your request to send, you can override the following:

  • Request Data using withData(array $data)
  • Request Query Params using withQuery(array $query)
  • Request Path using setPath(string $path)

Concurrent

$responses = \JustSteveKing\Transporter\Facades\Concurrently::build()->setRequests([
    TestRequest::build()
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
]);

$responses[0]->json();
$responses[1]->json();
$responses[2]->json();

Concurrency with Custom key

$responses = \JustSteveKing\Transporter\Facades\Concurrently::build()->setRequests([
    TestRequest::build()
        ->as(
            key: 'first'
        )
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->as(
            key: 'second'
        )
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
    TestRequest::build()
        ->as(
            key: 'third'
        )
        ->withToken('foobar')
        ->withData([
        'title' => 'Build a package'
    ]),
]);

$responses['first']->json();
$responses['second']->json();
$responses['third']->json();

Optional Alias

Instead of the standard send() method, it is also possible to use the fun alias energize(). Please note, no sound effects are included.

TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ])
    ->energize()
    ->json();

Faking a Request or Concurrent

To fake a request, all you need to do is replace the build method with the fake method, which takes an optional status parameter, to set the status code being returned with the response:

TestRequest::fake(
    status: 200,
)->withToken('foobar')
->withData([
    'title' => 'Build a package'
])->withFakeData([
    'data' => 'faked'
])->send();
$responses = Concurrently::fake()->setRequests([
    TestRequest::fake()->setPath(
        path: '/todos/1',
    )->as(
        key: 'first'
    ),
    TestRequest::fake()->setPath(
        path: '/todos/2',
    )->as(
        key: 'second'
    ),
    TestRequest::fake()->setPath(
        path: '/todos/3',
    )->as(
        key: 'thirds'
    ),
])->run();

Which will return a response with the data you pass through to withFakeData, which internally will merge what is on the class with what you pass it. So you can build up an initial state of faked data per class.

Testing

To run the tests in parallel:

composer run test

To run the tests with a coverage report:

composer run test-coverage

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

laravel-transporter's People

Contributors

juststeveking avatar craigpotter avatar telkins avatar abdullahfaqeir avatar

Watchers

James Cloos 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.