pramod / api-consumer
Api consumer
Requires
- php: ^7.1
- guzzlehttp/guzzle: ^6.5 || ^7.0
- illuminate/support: 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0
- symfony/yaml: ^5.1
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
README
This Package developed to request various external api without creating external service or traits.
Installation
You can install the package via composer:
composer require pramod/api-consumer
Usage
Copy Config
php artisan vendor:publish --provider="Pramod\ApiConsumer\ApiConsumerServiceProvider" --tag="config"
If you are using Lumen Copy Config
cp vendor/pramod/ApiConsumer/config/api-consumer.php config/
Autoload Config
$app->configure('api-consumer');
// Register Service Provider $this->app->register(ApiConsumerServiceProvider::class);
Please Refer Test Case For Complete Reference which is located in tests/ExampleTest.php
Basic Syntax
Api::consume('ebill') ->via('customers/pramodk_home/documents') ->with([ 'headers'=>[ 'Authorization'=>'xx', 'Content-Type'=>'', 'Accept'=>'' ], 'method'=>'POST', 'version'=>'v1', 'payload'=>[ 'name'=>'name is here' ] ]) ->superChargedBy([ 'cache'=>[ 'ttl'=>20, 'key_prefix'=>'documents' ] ]) ->toArray();
Define your custom settings[yml]
You can create your own config to manage and get proper visualization of endpoint used in your app.
-
Create Folder inside App eg, app/Consumers/covid.yml or you can create your own folder where you have to separate yml as per your service name.
-
Sample For yaml
filename:app/Consumers/covid.yml
summary: uri: summary story: this is summary uri countries: uri: countriesRoute story: All countries Route
Where **uri** must be your request path and story for short descriptions or uri.
-
Final Request
Sample Code
//add second param on your via method //for static uri eg, canvasenx.com.np/users return Api::consume('covid19api') ->via('summary', true) ->toCollection(); //for dynamic uri you must add your uri payload with value as return Api::consume('covid19api') ->via('countryDayOneRoute', [ 'country'=>'nepal' ]) ->toCollection();
Note: your configuration should look like this
summary: uri: summary story: this is summary uri countries: uri: countriesRoute story: All countries Route countryDayOneRoute: uri: dayone/country/{country} story: Returns all cases by case type for a country from the first recorded case. Country must be the country_slug from /countries. Cases must be one of confirmed, recovered, deaths"
Note: Your Dynamic uri must be covered with curly brackets {}.
-
Final Words for Via injections
Adding true or array on second parameters will observe your custom class which is defined in config file as custom_consumer array file
'covid19api'=>[ 'baseUri'=>'https://api.covid19api.com/', 'custom_consumer' => [ 'Consumers/covid' ], 'timeout'=>10 ]
Monitor Your Request Log
You can monitor external call request, response and execution time which is stored as json format in your default log. To activate log monitor you should enable middleware on your application
$app->middleware([ Pramod\ApiConsumer\Middleware\LogRequestMiddleware::class ]);
Call Your Graphql Api
- Add Your Service setting on <<config/api-consumer.php>>
<?php return [ 'default'=>[ 'timeout'=>10, 'ssl_verification'=>true, 'method'=>"GET" ], 'consumer' => [ 'graphqlTest'=>[ 'baseUri'=>'https://countries.trevorblades.com/', 'timeout'=>60, // 'ssl_verification'=>false //my default its true ] ] ];
- Add your endpoint on your request class
- In case of graphQl key inside with auto set Accept and Content-Type headers as application/json
- Method is set as POST
- Need to pass your graphql query in query key
- Default It call toJson()
return Api::consume('graphqlTest') ->via('fcm/graphql') ->with([ 'graphQl'=>[ 'query'=>'query{ continents{ code name countries{ code name native } } }' ] ])
List of all public graphql Api to test
https://github.com/APIs-guru/graphql-apis
Descriptions
-
consume
Request Or find Service Name mapped in config file having basic application url,version definitions.
-
via
This method is used to point service end or function name written in service Namespace
- Available Method
- first parameter: Api endpoint or uri
- Second parameter: if second param is true, first param must be method name defined inside your services Folder's class
- Available Method
-
with
Includes all parameter including headers, payload, query string and method name
-
Alert developer on error
-
Action on error, return code with message else message only
-
superChargedBy
Its advance method to keep your response on cache with defined cache name and time. It pick default cache Setting from your env
-
conversion method [By Default it call toJson() method]
- toArray
- toJson
- toCollection
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email pramodktwl@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.