Skip to content

Commit

Permalink
Merge pull request #154 from tomaszmrozinski/master
Browse files Browse the repository at this point in the history
Add option to control timeout of the Guzzle Client
  • Loading branch information
berkayk authored Mar 25, 2022
2 parents 495f87b + b88d0ad commit 4b3bc2f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ You need to fill in your OneSignal *App ID* and *REST API Key* inside your
ONESIGNAL_APP_ID=xxxxxxxxxxxxxxxxxxxx
ONESIGNAL_REST_API_KEY=xxxxxxxxxxxxxxxxxx
```
You can control timeout of the Guzzle client used by OneSignalClient by adding following into your .env file
```
ONESIGNAL_GUZZLE_CLIENT_TIMEOUT=integer_value
```
This param is useful when you are planning to send push notification via [Laravel queues](https://divinglaravel.com/always-set-a-timeout-for-guzzle-requests-inside-a-queued-job)

## Usage

Expand Down
12 changes: 11 additions & 1 deletion config/onesignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
|
*/
'rest_api_key' => env('ONESIGNAL_REST_API_KEY'),
'user_auth_key' => env('USER_AUTH_KEY')
'user_auth_key' => env('USER_AUTH_KEY'),

/*
|--------------------------------------------------------------------------
| Guzzle Timeout
|--------------------------------------------------------------------------
|
|
|
*/
'guzzle_client_timeout' => env('ONESIGNAL_GUZZLE_CLIENT_TIMEOUT', 0),
);
9 changes: 8 additions & 1 deletion src/OneSignalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ public function callback(Callable $requestCallback)
return $this;
}

public function __construct($appId, $restApiKey, $userAuthKey)
/**
* @param $appId
* @param $restApiKey
* @param $userAuthKey
* @param int $guzzleClientTimeout
*/
public function __construct($appId, $restApiKey, $userAuthKey, $guzzleClientTimeout = 0)
{
$this->appId = $appId;
$this->restApiKey = $restApiKey;
$this->userAuthKey = $userAuthKey;

$this->client = new Client([
'handler' => $this->createGuzzleHandler(),
'timeout' => $guzzleClientTimeout,
]);
$this->headers = ['headers' => []];
$this->additionalParams = [];
Expand Down
4 changes: 1 addition & 3 deletions src/OneSignalServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public function register()
$config = $app['config']['onesignal'] ?: $app['config']['onesignal::config'];
}

$client = new OneSignalClient($config['app_id'], $config['rest_api_key'], $config['user_auth_key']);

return $client;
return new OneSignalClient($config['app_id'], $config['rest_api_key'], $config['user_auth_key'] , $config['guzzle_client_timeout']);
});

$this->app->alias('onesignal', 'Berkayk\OneSignal\OneSignalClient');
Expand Down

0 comments on commit 4b3bc2f

Please sign in to comment.