Skip to content

Commit

Permalink
Allow connecting with JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
herkyl committed Feb 23, 2017
1 parent 0a0c903 commit c074115
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ Then update your dependencies:
$ php composer.phar update
```

##Usage
## Usage
Create a new instance of ScaleDrone passing it the `channel_id` and `secret_key` that you can find from the channel's page
```php
$auth = array(
'channel_id' => 'G3TYvCzXatrIuEtQ',
'secret_key' => 'M7Oc1DYXFgkCaUh4XQFC3TRV1R3RThPd'
'channel_id' => 'CHANNEL_ID',
'secret_key' => 'SECRET_KEY'
);

$client = new ScaleDrone\Client($auth);
```

If you wish to connect using a [JSON Web Token](https://www.scaledrone.com/docs/jwt-authentication) you can set it like this:
```php
$auth = array(
'channel_id' => 'CHANNEL_ID',
'bearer' => 'GENERATED_JWT'
);

$client = new ScaleDrone\Client($auth);
Expand Down
15 changes: 12 additions & 3 deletions lib/ScaleDrone/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ public function __construct($options = array())
$this->guzzle = new GuzzleHttp\Client([
'base_uri' => 'https://api2.scaledrone.com'
]);
$this->auth = [$options['channel_id'], $options['secret_key']];
if ($options['bearer']) {
$this->headers = [
'Authorization' => "Bearer {$options['bearer']}"
];
} else {
$this->auth = [$options['channel_id'], $options['secret_key']];
}
$this->channel_id = $options['channel_id'];
}

Expand All @@ -23,6 +29,7 @@ public function publish($room, $message)
$url = $this->channel_id . '/' . $room . '/publish';
$response = $this->guzzle->request('POST', $url, [
'auth' => $this->auth,
'headers' => $this->headers,
'json' => $message
]);
return $response;
Expand All @@ -32,7 +39,8 @@ public function channel_stats()
{
$url = $this->channel_id . '/stats';
$response = $this->guzzle->request('GET', $url, [
'auth' => $this->auth
'auth' => $this->auth,
'headers' => $this->headers
]);
return $response;
}
Expand All @@ -41,7 +49,8 @@ public function users_list()
{
$url = $this->channel_id . '/users';
$response = $this->guzzle->request('GET', $url, [
'auth' => $this->auth
'auth' => $this->auth,
'headers' => $this->headers
]);
return $response;
}
Expand Down

0 comments on commit c074115

Please sign in to comment.