Download the PHP package laragear/api-manager without Composer
On this page you can find all versions of the php package laragear/api-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api-manager
Api Manager
Manage multiple REST servers to make requests in few lines and fluently. No more verbose HTTP Requests!
Become a sponsor
Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can spread the word!
Requirements
- Laravel 10 or later
Installation
Require this using Composer into your project:
Usage
Creating an API Server
To make use of an API server, define a class that extends Laragear\ApiManager\ApiServer
. You may use the make:api
Artisan command to make a ready-made stub in the app\Http\Apis
directory.
You will receive a file with a base URL and actions, and space to add some headers and a bearer token. You're free to adjust it to your needs.
[!NOTE]
You can override the API Server stub creating one in
stubs/api.stub
.
Inline Actions
Setting actions in the API class solves the problem of having multiple endpoints and preparing each one every time across your application, which can led to errors or convoluted functions full of text.
The easiest way to define actions is to use the $actions
array using the syntax verb:route/{parameters}
, being the key the action name you will invoke later. If you don't define a verb, get
will be inferred.
For example, to update a chirp, we could call edit
directly from our ChirpApi
.
While you're at it, add the PHPDoc manually to your API Server to take advantage of autocompletion (intellisense).
Then, call the action name in camelCase notation. Arguments will be passed down to the HTTP Request.
If the route has named parameters, you can set them as arguments when invoking the server.
Also, you can call an action without arguments as it were a property.
Method actions
For more complex scenarios, you may use a class methods. Just be sure to type-hint the PendingRequest
on any parameter if you need to customize the request.
Then later, you can invoke the class methods like any monday morning.
[!NOTE]
Method actions take precedence over inline actions.
As with inline actions, method actions can be also executed as it where properties if these don't require arguments.
Authentication
An API Server supports the three types of authentication of the HTTP Client in Laravel: Basic, Digest and Bearer Token. You may define each of them as an array of username and password using authBasic()
or authDigest()
, and authToken()
with the token, respectively.
[!WARNING]
Don't use an associative array to match the underlying methods. Since Laravel doesn't warranty consistency on named arguments, you should opt for simple arrays.
Before & After building a requests
You have the option to modify the request before and after it's bootstrapped using the beforeBuild()
and afterBuild()
respectively. The beforeBuild()
is executed after the PendingRequest
instance receives the base URL, and the afterBuild()
is called after the headers and authentication are incorporated.
You're free here to tap into the request instance and modify it for all endpoints, or return an entirely new PendingRequest
instance.
[!TIP]
If you're using the old
build()
method from previous versions, it will still work since thebeforeBuild()
will call tobuild()
.
Overriding a request
The API request can be overridden as usual. All methods are passed down to the Illuminate\Http\Client\PendingRequest
instance if these don't exist on the API Class.
[!NOTE]
If the method exists in your API Class, it will take precedence.
Dependency Injection
All API Servers are resolved using the Service Container, so you can add any service you need to inject in your object through the constructor.
You can also create a callback to resolve your API Server in your AppServiceProvider
if you need more deep customization to create it.
Concurrent Requests
To add an API Server Request to a pool, use the onPool()
method for each concurrent request. There is no need to make all requests to the same API server, as you can mix and match different destinations.
You may also name the requests using a second argument to on()
.
Wrapping into custom responses
You may find yourself receiving a response and having to map the data to your own class manually. Instead of juggling your way to do that, you can automatically wrap the incoming response into a custom "API Response".
First, create a custom response for an api using make:api-response
, the API you want to use, and name the custom response with the same name of the endpoint. Ideally, you would want to name it the same as the action or method you plan to use it for.
You will receive a file like this:
[!NOTE]
You can override the API Response stub creating one in
stubs/api-response.stub
.
In this class you can make any method you want. Since your class will extend the base Laravel HTTP Client Response
class, you will have access to all its convenient methods.
Once you finish up customizing your custom API Response, you may map it to the actions and methods using the $responses
array of your Api class.
This will enable the API Manager to wrap the response into your own every time you call that method to receive a response. For example, if you call view()
, you will receive a new ViewResponse
instance.
[!TIP]
When using
async()
requests, custom responses are automatically wrapped once resolved.
Testing
You can easily test if an API Server action works or not by using the fake()
method of the HTTP facade.
Laravel Octane Compatibility
- There are no singletons using a stale application instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.
- There are no static properties written.
There should be no problems using this package with Laravel Octane.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
This specific package version is licensed under the terms of the MIT License, at time of publishing.
Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2024 Laravel LLC.
All versions of api-manager with dependencies
guzzlehttp/guzzle Version ^7.5
illuminate/http Version 10.*|11.*
illuminate/config Version 10.*|11.*
illuminate/container Version 10.*|11.*
illuminate/support Version 10.*|11.*