All Projects → JustSteveKing → laravel-transporter

JustSteveKing / laravel-transporter

Licence: MIT license
Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handling API requests.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-transporter

buttercms-go
Golang CMS and blog engine https://buttercms.com
Stars: ✭ 37 (-86.88%)
Mutual labels:  api-client
Calendarific
Calendarific holiday sensor for Home Assistant
Stars: ✭ 14 (-95.04%)
Mutual labels:  api-client
webflow-ruby
🕸 🌀Webflow API bindings for Ruby
Stars: ✭ 20 (-92.91%)
Mutual labels:  api-client
apiron
🍳 apiron is a Python package that helps you cook a tasty client for RESTful APIs. Just don't wash it with SOAP.
Stars: ✭ 106 (-62.41%)
Mutual labels:  api-client
basecampy3
A Python API for Basecamp 3
Stars: ✭ 31 (-89.01%)
Mutual labels:  api-client
Battleship
An Object-Oriented VBA experiment
Stars: ✭ 66 (-76.6%)
Mutual labels:  oop
docker
R Package For Accessing Docker via Docker APIs
Stars: ✭ 23 (-91.84%)
Mutual labels:  api-client
content-api-scala-client
A Scala client library for the Guardian's Content API
Stars: ✭ 37 (-86.88%)
Mutual labels:  api-client
laravel-tmdb
Interact with TMDB data in your Laravel application.
Stars: ✭ 25 (-91.13%)
Mutual labels:  api-client
nytimes
nytimes: Interacting with New York TImes APIs
Stars: ✭ 23 (-91.84%)
Mutual labels:  api-client
twinfield
PHP 7.3+ Library for using the Twinfield API.
Stars: ✭ 28 (-90.07%)
Mutual labels:  api-client
Mastering-Javascript
📚 PinterCoding University. Author : Gun Gun Febrianza
Stars: ✭ 43 (-84.75%)
Mutual labels:  oop
synadm
Command line admin tool for Synapse (Matrix reference homeserver)
Stars: ✭ 93 (-67.02%)
Mutual labels:  api-client
nyxx
Wrapper around Discord API for Dart
Stars: ✭ 217 (-23.05%)
Mutual labels:  api-client
euler2D-kfvs-Fortran2003
2D solver for Euler equations in quadrilateral grid, using kinetic flux vector splitting scheme, written in OOP F2003
Stars: ✭ 17 (-93.97%)
Mutual labels:  oop
platformsh-client-php
Platform.sh API client for PHP
Stars: ✭ 24 (-91.49%)
Mutual labels:  api-client
JavaScript-Bootcamp
Complete Documentation For JavaScript Bootcamp Course By Osama Elzero.
Stars: ✭ 27 (-90.43%)
Mutual labels:  oop
client-js
JS client for polygon.io api
Stars: ✭ 81 (-71.28%)
Mutual labels:  api-client
StudentVue.py
Python StudentVue Library
Stars: ✭ 17 (-93.97%)
Mutual labels:  api-client
midtrans-python-client
Official Midtrans Payment API Client for Python | https://midtrans.com
Stars: ✭ 24 (-91.49%)
Mutual labels:  api-client

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)

Checking the payload

I had a request in an issue to be able to see the request data for a request, so I have added a helper method called payload which will return whatever has been stored in the request data property.

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

$data = $request->payload(); // ['title' => 'Build a package']

Concurrent Requests

$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 a 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.

Sending XML

Thanks to a fantastic suggestion by @jessarcher we can use a Trait to allow for easy use of XML in your requests. Using this as a trait makes a lot of sense as most APIs these days use JSON, so it is purely opt in. To use this, simply use the trait on your request:

<?php

declare(strict_types=1);

namespace App\Transporter\Requests;

use JustSteveKing\Transporter\Concerns\SendsXml;
use JustSteveKing\Transporter\Request;

class XmlRequest extends Request
{
    use SendsXml;
    
    protected string $method = 'POST';
    
    protected string $path = '/your-endpoint';
}

Then all you need to do is call the methods:

XmlRequest::build()->withXml(
    xml: '<todo><name>Send an XML Requets</name><completed>false</completed></todo>'
)->send();

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.

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].