Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Extending the functionality of the Laravel HTTP client
License
TheDragonCode/laravel-http-macros
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
To get the latest version ofHTTP Macros, simply require the project usingComposer:
composer require dragon-code/laravel-http-macros
If desired, you can publish the configuration file using the console command:
php artisan vendor:publish --provider="DragonCode\\LaravelHttpMacros\\ServiceProvider"If your application already has aconfig/http.php file, then you can simply add a newmacros key from theconfiguration file to it.
Here you can specify a list of your classes for registering macros.Macro classes must inherit from the abstract classDragonCode\LaravelHttpMacros\Macros\Macro.
You can also redefine macro names using an associative array. For example:
// Configreturn ['macros' => ['request' => [ WithLoggerMacro::class, ],'response' => [ ToDataMacro::class, ], ],];// MacroHttp::withLogger('some')->get();Http::withLogger('some')->get()->toData(...);Http::get()->toData(...);
// Configreturn ['macros' => ['request' => ['qwerty' => WithLoggerMacro::class, ],'response' => ['qwerty' => ToDataMacro::class, ], ],];// MacroHttp::qwerty('some')->get();Http::qwerty('some')->get()->qwerty(...);Http::qwerty('some')->get()->toData(...);// method not foundHttp::get()->qwerty(...);Http::get()->toData(...);// method not found
Adds the ability to log HTTP requests and responses.
useIlluminate\Support\Facades\Http;Http::withLogger('some_channel')->get();
This method will log HTTP requests and responses.
It is also possible to use your own handler, message formatting and path to the log file.To do this, you need to specify the desired channel name from the log file and define the necessary parameters in it.
For example:
// config/logging.phpreturn [// ...'channels' => ['some' => ['driver' =>'single','level' =>env('LOG_LEVEL','debug'),'path' =>storage_path('logs/some.log'),'handler' => \App\Logging\SomeHandlerStack::class,'formatter' => \App\Logging\MessageFormatter::class, ], ],];// Usagereturn Http::withLogger('some')->...
The class instance will be returned.
useIlluminate\Support\Facades\Http;// Returns a SomeData objectreturn Http::get()->toData(SomeData::class);// Will return a SomeData object generated from the JSON pathreturn Http::get()->toData(SomeData::class,'data.item');// Returns the result of the callback executionreturn Http::get()->toData(fn (array$data) =>newSomeData($data['data']['item']['id'],$data['data']['item']['title'] ));// Returns the result of the callback execution from a custom JSON pathreturn Http::get()->toData(fn (array$data) =>newSomeData($data['id'],$data['title']),'data.item');
Note
If a
frommethod exists in a class, then it will be called to construct the object.Compatible withSpatie Laravel Data.
class SomeData{publicfunction__construct(publicint$id,publicstring$title ) {}publicstaticfunctionfrom(array$data):static {returnnewstatic(...$data); }}return Http::get()->toData(SomeData::class);
TheIlluminate\Support\Collection object or an object inherited from it will be returned.
useIlluminate\Support\Facades\Http;// Returns a collection of SomeData objectsreturn Http::get()->toDataCollection(SomeData::class);// Returns a collection of SomeData objects formed from the JSON pathreturn Http::get()->toDataCollection(SomeData::class,'data.item');// Returns the result of the callback executionreturn Http::get()->toDataCollection(fn (array$data) =>collect([newSomeData($data['data']['item']['id'],$data['data']['item']['title'] ), ]));// Returns the result of the callback execution from a custom JSON pathreturn Http::get()->toDataCollection(fn (array$data) =>collect([newSomeData(...$data), ]),'data.item');
Note
If a
collectmethod exists in a class, then it will be called to construct the collection.Compatible withSpatie Laravel Data.
useIlluminate\Support\Collection;class SomeData{publicfunction__construct(publicint$id,publicstring$title ) {}publicstaticfunctioncollect(array$items):Collection {returncollect($items)->map(fn (array$item) =>newstatic(...$item) ); }}return Http::get()->toDataCollection(SomeData::class);
You can generate helper files for the IDE using the console command:
php artisan http:macros-helper
This will help your IDE suggest methods.
This package is licensed under theMIT License.
About
Extending the functionality of the Laravel HTTP client
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
