From 317fedce1b3c97b527603f76cd78d7a34fce58c4 Mon Sep 17 00:00:00 2001 From: Mikhail Mingalev Date: Tue, 23 Jul 2019 22:56:23 +0300 Subject: [PATCH] Added possibility to change base API url (https://onesignal.com/api/v1) via config --- config/onesignal.php | 11 ++++++++++- src/OneSignalClient.php | 18 ++++++++++-------- src/OneSignalServiceProvider.php | 11 +++++++++-- tests/test.php | 5 +++-- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/config/onesignal.php b/config/onesignal.php index 8eb3448..0238a6c 100644 --- a/config/onesignal.php +++ b/config/onesignal.php @@ -1,6 +1,15 @@ 'YOUR-API-URL-HERE', // or empty for https://onesignal.com/api/v1 + /* |-------------------------------------------------------------------------- | One Signal App Id @@ -20,4 +29,4 @@ */ 'rest_api_key' => 'YOUR-REST-API-KEY-HERE', 'user_auth_key' => 'YOUR-USER-AUTH-KEY' -); \ No newline at end of file +); diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index 4d600c6..0b093dd 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -24,6 +24,7 @@ class OneSignalClient protected $appId; protected $restApiKey; protected $userAuthKey; + protected $apiUrl; protected $additionalParams; /** @@ -69,11 +70,12 @@ public function callback(Callable $requestCallback) return $this; } - public function __construct($appId, $restApiKey, $userAuthKey) + public function __construct($appId, $restApiKey, $userAuthKey, $apiUrl = null) { $this->appId = $appId; $this->restApiKey = $restApiKey; $this->userAuthKey = $userAuthKey; + $this->apiUrl = $apiUrl ?: self::API_URL; $this->client = new Client([ 'handler' => $this->createGuzzleHandler(), @@ -499,29 +501,29 @@ private function sendPlayer(Array $parameters, $method, $endpoint) public function post($endPoint) { if($this->requestAsync === true) { - $promise = $this->client->postAsync(self::API_URL . $endPoint, $this->headers); + $promise = $this->client->postAsync($this->apiUrl . $endPoint, $this->headers); return (is_callable($this->requestCallback) ? $promise->then($this->requestCallback) : $promise); } - return $this->client->post(self::API_URL . $endPoint, $this->headers); + return $this->client->post($this->apiUrl . $endPoint, $this->headers); } public function put($endPoint) { if($this->requestAsync === true) { - $promise = $this->client->putAsync(self::API_URL . $endPoint, $this->headers); + $promise = $this->client->putAsync($this->apiUrl . $endPoint, $this->headers); return (is_callable($this->requestCallback) ? $promise->then($this->requestCallback) : $promise); } - return $this->client->put(self::API_URL . $endPoint, $this->headers); + return $this->client->put($this->apiUrl . $endPoint, $this->headers); } public function get($endPoint) { - return $this->client->get(self::API_URL . $endPoint, $this->headers); + return $this->client->get($this->apiUrl . $endPoint, $this->headers); } public function delete($endPoint) { if($this->requestAsync === true) { - $promise = $this->client->deleteAsync(self::API_URL . $endPoint, $this->headers); + $promise = $this->client->deleteAsync($this->apiUrl . $endPoint, $this->headers); return (is_callable($this->requestCallback) ? $promise->then($this->requestCallback) : $promise); } - return $this->client->delete(self::API_URL . $endPoint, $this->headers); + return $this->client->delete($this->apiUrl . $endPoint, $this->headers); } } diff --git a/src/OneSignalServiceProvider.php b/src/OneSignalServiceProvider.php index 828219a..a995683 100644 --- a/src/OneSignalServiceProvider.php +++ b/src/OneSignalServiceProvider.php @@ -31,12 +31,19 @@ public function boot() public function register() { $this->app->singleton('onesignal', function ($app) { - $config = isset($app['config']['services']['onesignal']) ? $app['config']['services']['onesignal'] : null; + $config = isset($app['config']['services']['onesignal']) + ? $app['config']['services']['onesignal'] + : null; + if (is_null($config)) { $config = $app['config']['onesignal'] ?: $app['config']['onesignal::config']; } - $client = new OneSignalClient($config['app_id'], $config['rest_api_key'], $config['user_auth_key']); + $apiUrl = isset($config['api_url']) + ? $config['api_url'] + : null; + + $client = new OneSignalClient($config['app_id'], $config['rest_api_key'], $config['user_auth_key'], $apiUrl); return $client; }); diff --git a/tests/test.php b/tests/test.php index 2e7ea7a..1ff1c49 100644 --- a/tests/test.php +++ b/tests/test.php @@ -8,7 +8,8 @@ $client = new Berkayk\OneSignal\OneSignalClient( getenv('APP_ID'), getenv('REST_API_KEY'), - getenv('USER_AUTH_KEY')); + getenv('USER_AUTH_KEY'), + getenv('API_URL')); echo $client->testCredentials(); -$client->sendNotificationToUser(".","4bc5da02-1722-4fee-943d-c8b5ccd507a2"); \ No newline at end of file +$client->sendNotificationToUser("Test",getenv('USER_ID') ?: "4bc5da02-1722-4fee-943d-c8b5ccd507a2");