behamin / service-proxy
for proxy or sending requests to other services with useful utilities
Installs: 2 135
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 3
Forks: 5
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- behamin/bresources: ^1.5 || ^2.2
- guzzlehttp/guzzle: ^7.0
- laravel/framework: ^7.0 || ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- orchestra/testbench: ^v6.0 || ^v7.0
- dev-main
- 3.x-dev
- v3.10.0
- v3.9.4
- v3.9.3
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.0
- v3.7.0
- v3.6.1
- v3.6
- v3.5.1
- v3.5
- v3.4
- v3.3
- v3.2
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1
- v3.0.2
- v3.0.1
- v3.0
- 2.x-dev
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1
- v2.0.2
- v2.0.1
- v2.0
- 1.x-dev
- v1.5
- v1.4
- v1.3.3
- v1.3.2
- v1.3
- v1.2
- v1.1.1
- v1.1
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
This package is auto-updated.
Last update: 2024-11-06 13:05:37 UTC
README
Internal communication between services with useful tools
Make request by laravel http client
Installation
composer require behamin/service-proxy
Publish config
php artisan vendor:publish --provider="Behamin\ServiceProxy\Providers\ProxyServiceProvider" --tag config
Add services
Add your project's base url and global headers in proxy.php
config
return [ /** * Headers added to every request */ 'global_headers' => [ 'Accept' => 'application/json', ... ], 'base_url' => env('PROXY_BASE_URL', env('APP_URL')), ];
Usage
Normal usage
use Behamin\ServiceProxy\Proxy; // Http Get Proxy::withToken('Your bearer token') ->acceptJson() ->retry(3) ->withHeaders([ "Content-Type" => "application\json" ])->get('api/articles'); Proxy::post('api/articles', [ "title" => "Test title", "body" => "Test body" ]); Proxy::patch('api/articles/1', [ "title" => "Test title", "body" => "Test body" ]); Proxy::put('api/articles', [ "title" => "Test title", "body" => "Test body" ]); Proxy::delete('api/articles/1');
Using http request
use Behamin\ServiceProxy\Proxy; use Illuminate\Http\Request; public function index(Request $request) { $serviceName = 'test-service'; Proxy::request($request, $serviceName); }
Proxy events
On success
use Behamin\ServiceProxy\Proxy; use Behamin\ServiceProxy\Responses\ProxyResponse; Proxy::get('api/articles/1')->onSuccess(function (ProxyResponse $proxyResponse) { $data = $proxyResponse->data(); $message = $proxyResponse->message(); $response = $proxyResponse->response(); $items = $proxyResponse->items(); $count = $proxyResponse->count(); ... });
On error
use Behamin\ServiceProxy\Proxy; use Behamin\ServiceProxy\Exceptions\ProxyException; Proxy::get('api/articles/1')->onSuccess(function (ProxyException $proxyException) { $proxyResponse = $proxyException->proxyResponse; $trace = $proxyException->getTraceAsString(); ... });
On data success
use Behamin\ServiceProxy\Proxy; Proxy::get('api/articles/1')->onDataSuccess(function (array $data) { $id = $data['id']; });
On data collection success
use Behamin\ServiceProxy\Proxy; Proxy::get('api/articles/1')->onCollectionSuccess(function (array $items, int $count) { ... });
Proxy response methods
use Behamin\ServiceProxy\Proxy; $proxyResponse = Proxy::get('api/articles/1');
Proxy request methods
Mocking proxy response
You can use mock()
on Proxy class before calling http methods and pass the json path in your 'tests/mock' directory, to mock a json for faking your Proxy response in test mode.
Example:
use Behamin\ServiceProxy\Proxy; Proxy::mock('response.json')->get('address');