iPaaS package for Laravel
This package includes
* Driver for Google stack logging
* Exception handler for Google error reporting
* Log-info (iLog
) helper
* To collect info on runtime through laravel service container interface
* To render collected info and attach to each log context
* Middleware
* To authenticate request
* To capture initial request for logger
* Request
* To provide additional methods with request
* Exceptions
* To report exception with Log-info context
* To render exception according to iPaaS set standards
* Response
* To all context information with response
* To render error and response according to iPaaS set standards
* Other helpers
* Converter
* [more coming soon], (*1)
Setup
i. Add Package
Run composer update after adding composer package, (*2)
ipaas/gapp-laravel: ~1.1.0
OR; by running, (*3)
composer require ipaas/gapp-laravel // considering v2+
Make sure that the
ENV: GAPP_SECURE is set to true
; and
ENV: LOG_CHANNEL on gcloud environment is set to stack-driver
;, (*4)
ii. Migration and Artisan Command
If you are using the version 2.0 or later, you will have access to the migration and artisan command:, (*5)
php artisan vendor:publish --tag=gapp
command will push the middleware to the application, and furthermore using GAPP_SECURE
set to true
, the security will be applied., (*6)
php artisan migrate
command will create a new partner_apps
table in your application, which will be used to verify the X-Api-Key
when passing the middleware partner
., (*7)
php artisan create-partner-app {provider}
command will create a new row in your new partner_apps
table with a provider name as optional argument., (*8)
API Documentation
Log-info (ilog)
Helper to add context information to all log entries., (*9)
Once context is added to ilog it will append to all future logs entries
ilog refresh with each request and; have same life cycle as of request(), (*10)
ilog()
is a helper method returning singleton class Ipaas\Gapp\Logger\Client.php
To add context info just call ilog()
and chain any method available.
Following methods are available:, (*11)
Method |
Usage |
setClientId (string) |
set client id/name |
setClientKey (string) |
set client key/token |
setRequestId (string) |
set request id/token |
setType (string) |
type of request |
prop ((string)value, (string)name) |
any custom key and value |
setDate ((stringā®Carbon)value, (string)name) |
any custom date key and value |
setDateFrom (stringā®Carbon) |
sync/request date from |
setDateTo (stringā®Carbon) |
sync/request date to |
setUuid (stringā®null) |
universal unique identifier |
toArray() |
get all info as array |
iLog([data-set])
can be use to re-init* log data.
*can be use to pass log-info to queue jobs, (*12)
Example
Following example will write log in GCloud Logging with all provided context, (*13)
/* ------ Class A ------- */
function validateUser(Request $request){
// validate request
$request->validate(['user_id'=>'integer|required']);
// get user details
$user = User::get($data->user_id);
if ($user) {
/********LOG-INFO PROVIDER*******/
iLog() // add user details to context
->setClientId($user->id) // client id to context
->setClientKey($user->key); // client key to context
// add request details context
iLog()->setType('Validate user name');
// Calling other class to resolve request
return ClassB::validateUserName($user);
}
}
/* ------ Class B ------- */
// all set context still exist
function validateUserName(User $user){
// log event info
// will be logged with context
Log::info('Validating user name')
// validate user name
$name = $user->name;
if(empty($name) || is_null($name))
{
// log warning that name is not valid
// will be logged all context
Log::warning('Name is null or empty');
return false;
}
}
Middleware
Validation
By default this library try to validate request by checking headers:
* X-Api-Key (set on the partner_apps
table)
the system will try to match the header X-Api-Key
with the partner_apps
table.
To enable, just add the middleware partner
on your desirable route Route::get('foo', FooController@bar)->middleware('partner')
, (*14)
Logging
By default library try to translate and log following details:, (*15)
$request->header('Authorization') // Authorization value from header
$request->header('X-Api-Key') // Client ID from header
$request->header('Gapp-Request-ID') // Gapp Request ID from header
$request->uuid // request uuid
$request->dateFrom // date from
$request->dateTo // date to
Request
Request is resolved using Ipass/Request
controller
to use see the given example:, (*16)
use Ipaas\Gapp\Request;
use Ipass\Response;
class Accounts extends Response;
{
public function index(Request $request)
{
$rules = ['name' => 'required|string'];
$request->validate($rules);
}
}
all given function are chain-able when extend method is used, (*17)
$request
->boolify('EnablePaymentsToAccount')
->arrify('Type')
->validate($rules);
Validate
Validate request based on given rules set., (*18)
// $rules is laravel validation rule set
Func: validate(array $rules)
Return: REQUEST if all sucessfull
Throw: Unprocessable Entity (422) if validation fails
Arrify
Convert request csv parameter to php array., (*19)
// $item is request csv param
Func: arrify(string $item)
Return: modified REQUEST
Boolify
Convert request string 'true/false' parameter to php boolean., (*20)
// $item is request string 'true/false'
Func: boolify(string $item)
Return: modified REQUEST
Requestify
Replace request given parameter value., (*21)
// $item is request parameter name
// $value is new value
Func: requestify(string $item, mixed $value)
Return: modified REQUEST
Response
Response helper iresponse
or use by extending base controller [YOUR CONTROLLER] extends Ipaas/Response.php
, with that helper you can access sendError() method too, making an exception easier., (*22)
Set Meta
Chain-able function to set response meta data, (*23)
return $this->meta(['client_id' => 'unknown'])->sendResponse($data);
Set header
Chain-able function to set response header data, (*24)
return $this->header(['content-type' => 'application/json'])->sendResponse($data);
Other Helpers
Converter
Ipaas/Helper/Converter-Helpers, (*25)
Normalized Name
Replace ASCII space unicode with
space., (*26)
Input: te \n sting
, (*27)
Response: te sting
, (*28)
// $name is unicode string
Func: normalizedName(string $name)
Return: normalized string
Boolify List
Convert given string 'true/false' parameter to php boolean in provided array., (*29)
Input: ['true', 'false', 'TRUE', 'FALSE', true, false, TRUE, FALSE, 0, 1, '0', '1', '', ' test']
, (*30)
Response:
[true, false, true, false, true, false, true, false, false, true, false, true, false, true]
, (*31)
// $list is haystack array
// $item is needle name
Func: boolifyList(array $list, string $item)
Return: modified list
Note
ps. google/cloud package is required to run application on google app engine flex environment, (*32)
Troubleshooting - Upgrade v1.* to v2.*
-
ilog()->data()
was changed to ilog()->appendData()
;
-
iresponse()
method was removed, use Ipaas\Gapp\Response()
instead;
- you do not need to instance the provider
Ipaas\IpaasServiceProvider::class
anymore, it is now automatically injected by composer;
-
stackdriver
logging channel was changed to stack-driver
- all the
ilog()
setters were changed too:
- client is now setClientId;
- key is now setClientKey;
- type is now setType;
- dateFrom is now setDateFrom;
- dateTo is now setDateTo;
- uuid is now setUuid;
- all the exception helpers were removed:
- iThrow;
- UnauthorizedException;
- BadRequestException;
- TooManyRequestException;
- NotFoundException;
- InternalServerException;
- all the response helpers were removed:
- errorValidation;
- errorUnauthorized;
- errorBadRequest;
- errorTooManyRequest;
- errorNotFound;
- errorNotImplemented;
- errorInternalServer;