Internal communication between services with useful tools
Make request by laravel http client
composer require behamin/service-proxy
php artisan vendor:publish --provider="Behamin\ServiceProxy\Providers\ProxyServiceProvider" --tag config
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')),
];
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');
use Behamin\ServiceProxy\Proxy;
use Illuminate\Http\Request;
public function index(Request $request) {
$serviceName = 'test-service';
Proxy::request($request, $serviceName);
}
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();
...
});
use Behamin\ServiceProxy\Proxy;
use Behamin\ServiceProxy\Exceptions\ProxyException;
Proxy::get('api/articles/1')->onSuccess(function (ProxyException $proxyException) {
$proxyResponse = $proxyException->proxyResponse;
$trace = $proxyException->getTraceAsString();
...
});
use Behamin\ServiceProxy\Proxy;
Proxy::get('api/articles/1')->onDataSuccess(function (array $data) {
$id = $data['id'];
});
use Behamin\ServiceProxy\Proxy;
Proxy::get('api/articles/1')->onCollectionSuccess(function (array $items, int $count) {
...
});
use Behamin\ServiceProxy\Proxy;
$proxyResponse = Proxy::get('api/articles/1');
Method | Description |
---|---|
data() | given data |
items() | give items |
count() | given items count |
errors() | given errors if there is |
message() | given message |
onSuccess($closure) | When http request is successful |
onError($closure) | When http request is with error |
onCollectionSuccess($closure) | Get collection when http request is successful |
onDataSuccess($closure) | Get data when http request is successful |
throw() | Throw error if http request failed |
toException() | Get exception if http request failed |
Method | Return Type |
---|---|
fake($callback = null) | \Illuminate\Http\Client\Factory |
accept(string $contentType) | \Behamin\ServiceProxy\Http |
acceptJson() | \Behamin\ServiceProxy\Http |
asForm() | \Behamin\ServiceProxy\Http |
asJson() | \Behamin\ServiceProxy\Http |
asMultipart() | \Behamin\ServiceProxy\Http |
async() | \Behamin\ServiceProxy\Http |
attach(string array $name, string $contents = '', string null $filename = null, array $headers = []) | \Behamin\ServiceProxy\Http |
baseUrl(string $url) | \Behamin\ServiceProxy\Http |
beforeSending(callable $callback) | \Behamin\ServiceProxy\Http |
bodyFormat(string $format) | \Behamin\ServiceProxy\Http |
contentType(string $contentType) | \Behamin\ServiceProxy\Http |
dd() | \Behamin\ServiceProxy\Http |
dump() | \Behamin\ServiceProxy\Http |
retry(int $times, int $sleep = 0) | \Behamin\ServiceProxy\Http |
sink(string | resource $to) |
stub(callable $callback) | \Behamin\ServiceProxy\Http |
timeout(int $seconds) | \Behamin\ServiceProxy\Http |
withBasicAuth(string $username, string $password) | \Behamin\ServiceProxy\Http |
withBody(resource | string $content, string $contentType) |
withCookies(array $cookies, string $domain) | \Behamin\ServiceProxy\Http |
withDigestAuth(string $username, string $password) | \Behamin\ServiceProxy\Http |
withHeaders(array $headers) | \Behamin\ServiceProxy\Http |
withMiddleware(callable $middleware) | \Behamin\ServiceProxy\Http |
withOptions(array $options) | \Behamin\ServiceProxy\Http |
withToken(string $token, string $type = 'Bearer') | \Behamin\ServiceProxy\Http |
withUserAgent(string $userAgent) | \Behamin\ServiceProxy\Http |
withoutRedirecting() | \Behamin\ServiceProxy\Http |
withoutVerifying() | \Behamin\ServiceProxy\Http |
pool(callable $callback) | array |
request(Request $request, string $service) | \Behamin\ServiceProxy\Responses\ProxyResponse |
get(string $url, array | string |
delete(string $url, array $data = []) | \Behamin\ServiceProxy\Responses\ProxyResponse |
head(string $url, array | string |
patch(string $url, array $data = []) | \Behamin\ServiceProxy\Responses\ProxyResponse |
post(string $url, array $data = []) | \Behamin\ServiceProxy\Responses\ProxyResponse |
put(string $url, array $data = []) | \Behamin\ServiceProxy\Responses\ProxyResponse |
send(string $method, string $url, array $options = []) | \Behamin\ServiceProxy\Responses\ProxyResponse |
fakeSequence(string $urlPattern = '*') | \Illuminate\Http\Client\ResponseSequence |
assertSent(callable $callback) | void |
assertNotSent(callable $callback) | void |
assertNothingSent() | void |
assertSentCount(int $count) | void |
assertSequencesAreEmpty() | void |
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');