Skip to content

Remove Guzzle requirement and add Laravel 10 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
},
"require": {
"ext-json": "*",
"firebase/php-jwt": "^5.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.0"
"firebase/php-jwt": "^5.5",
"laravel/framework": "^10.0 || ^9.0 || ^8.0"
}
}
96 changes: 47 additions & 49 deletions src/CloudflareStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@
class CloudflareStream
{
private $accountId;

private $authKey;

private $authEMail;

private $privateKeyId;

private $privateKeyToken;

private $guzzle;

/**
* CloudflareStream constructor.
*
* @param string $accountId
* @param string $authKey
* @param string $authEMail
* @param null $privateKey
* @param null $privateKeyToken
* @param null $privateKey
* @param null $privateKeyToken
*
* @throws NoCredentialsException
*/
public function __construct(string $accountId, string $authKey, string $authEMail, $privateKey = null, $privateKeyToken = null)
Expand All @@ -39,10 +42,10 @@ public function __construct(string $accountId, string $authKey, string $authEMai
$this->authKey = $authKey;
$this->authEMail = $authEMail;
$this->guzzle = new Client([
'base_uri' => 'https://api.cloudflare.com/client/v4/'
'base_uri' => 'https://api.cloudflare.com/client/v4/',
]);

if (!empty($privateKey) && !empty($privateKeyToken)) {
if (! empty($privateKey) && ! empty($privateKeyToken)) {
$this->privateKeyId = $privateKey;
$this->privateKeyToken = $privateKeyToken;
}
Expand All @@ -51,8 +54,9 @@ public function __construct(string $accountId, string $authKey, string $authEMai
/**
* Get a list of videos.
*
* @param array $customParameters
* @param array $customParameters
* @return string
*
* @throws GuzzleException
*/
public function list($customParameters = [])
Expand All @@ -61,41 +65,39 @@ public function list($customParameters = [])
$parameters = [
'include_counts' => true,
'limit' => 1000,
'asc' => false
'asc' => false,
];

// Set custom parameters
if (!empty($customParameters) && is_array($customParameters)) {
if (! empty($customParameters) && is_array($customParameters)) {
$parameters = array_merge($parameters, $customParameters);
}

return $this->request('accounts/' . $this->accountId . '/stream?' . http_build_query($parameters))->getBody()->getContents();
return $this->request('accounts/'.$this->accountId.'/stream?'.http_build_query($parameters))->getBody()->getContents();
}

/**
* Fetch details of a single video.
*
* @param string $uid
* @return string
*
* @throws GuzzleException
*/
public function video(string $uid)
{
return $this->request('accounts/' . $this->accountId . '/stream/' . $uid)->getBody()->getContents();
return $this->request('accounts/'.$this->accountId.'/stream/'.$uid)->getBody()->getContents();
}

/**
* Get embed code. Could be returned with signed token if necessary.
*
* @param string $uid
* @param bool $addControls
* @param bool $useSignedToken
* @return string
*
* @throws NoPrivateKeyOrTokenException|GuzzleException
*/
public function embed(string $uid, bool $addControls = false, bool $useSignedToken = true)
{
$embed = $this->request('accounts/' . $this->accountId . '/stream/' . $uid . '/embed')->getBody();
$embed = $this->request('accounts/'.$this->accountId.'/stream/'.$uid.'/embed')->getBody();
$requireSignedToken = false;

// Require signed token?
Expand All @@ -106,12 +108,12 @@ public function embed(string $uid, bool $addControls = false, bool $useSignedTok

// Add controls attribute?
if ($addControls) {
return str_replace('src="' . $uid . '"', 'src="' . ($useSignedToken && $requireSignedToken ? $this->getSignedToken($uid) : $uid) . '" controls', $embed);
return str_replace('src="'.$uid.'"', 'src="'.($useSignedToken && $requireSignedToken ? $this->getSignedToken($uid) : $uid).'" controls', $embed);
}

// Signed URL necessary?
if ($useSignedToken && $requireSignedToken) {
return str_replace('src="' . $uid . '"', 'src="' . $this->getSignedToken($uid) . '"', $embed);
return str_replace('src="'.$uid.'"', 'src="'.$this->getSignedToken($uid).'"', $embed);
}

// Return embed code
Expand All @@ -121,20 +123,20 @@ public function embed(string $uid, bool $addControls = false, bool $useSignedTok
/**
* Delete video.
*
* @param string $uid
* @return string
*
* @throws GuzzleException
*/
public function delete(string $uid)
{
return $this->request('accounts/' . $this->accountId . '/stream/' . $uid, 'delete')->getBody()->getContents();
return $this->request('accounts/'.$this->accountId.'/stream/'.$uid, 'delete')->getBody()->getContents();
}

/**
* Get meta data for a specific video.
*
* @param string $uid
* @return array
*
* @throws GuzzleException
*/
public function getMeta(string $uid)
Expand All @@ -150,20 +152,19 @@ public function getMeta(string $uid)
/**
* Set meta data for a specific video.
*
* @param string $uid
* @param array $meta
* @return string
*
* @throws GuzzleException
*/
public function setMeta(string $uid, array $meta)
{
// Merge meta data
$meta = [
'meta' => array_merge($this->getMeta($uid), $meta)
'meta' => array_merge($this->getMeta($uid), $meta),
];

// Request
$response = $this->request('accounts/' . $this->accountId . '/stream/' . $uid, 'post', $meta);
$response = $this->request('accounts/'.$this->accountId.'/stream/'.$uid, 'post', $meta);

// Return result
return $response->getBody()->getContents();
Expand All @@ -172,17 +173,16 @@ public function setMeta(string $uid, array $meta)
/**
* Remove meta data by key.
*
* @param string $uid
* @param string $metaKey
* @return string
*
* @throws GuzzleException
*/
public function removeMeta(string $uid, string $metaKey)
{

// Merge meta data
$meta = [
'meta' => array_merge($this->getMeta($uid))
'meta' => array_merge($this->getMeta($uid)),
];

// Remove key
Expand All @@ -191,29 +191,29 @@ public function removeMeta(string $uid, string $metaKey)
}

// Request
return $this->request('accounts/' . $this->accountId . '/stream/' . $uid, 'post', $meta)->getBody()->getContents();
return $this->request('accounts/'.$this->accountId.'/stream/'.$uid, 'post', $meta)->getBody()->getContents();

}

/**
* Get name of a video.
*
* @param string $uid
* @return string
*
* @throws GuzzleException
*/
public function getName(string $uid)
{
$meta = $this->getMeta($uid);

return $meta['name'];
}

/**
* Rename a video.
*
* @param string $uid
* @param string $name
* @return string
*
* @throws GuzzleException
*/
public function setName(string $uid, string $name)
Expand All @@ -224,25 +224,24 @@ public function setName(string $uid, string $name)
/**
* Set whether a specific video requires signed URLs or not.
*
* @param string $uid
* @param bool $required
* @return string
*
* @throws GuzzleException
*/
public function setSignedURLs(string $uid, bool $required)
{
return $this->request('accounts/' . $this->accountId . '/stream/' . $uid, 'post', [
return $this->request('accounts/'.$this->accountId.'/stream/'.$uid, 'post', [
'uid' => $uid,
'requireSignedURLS' => $required
'requireSignedURLS' => $required,
])->getBody()->getContents();
}

/**
* Get playback URLs of a specific video.
*
* @param string $uid
* @param bool $useSignedToken
* @param bool $useSignedToken
* @return string
*
* @throws GuzzleException
* @throws NoPrivateKeyOrTokenException
*/
Expand Down Expand Up @@ -270,9 +269,8 @@ public function getPlaybackURLs(string $uid, $useSignedToken = true)
/**
* Get signed token for a video.
*
* @param string $uid
* @param int $addHours
* @return string
*
* @throws NoPrivateKeyOrTokenException
*/
public function getSignedToken(string $uid, int $addHours = 4)
Expand All @@ -284,15 +282,15 @@ public function getSignedToken(string $uid, int $addHours = 4)
return JWT::encode([
'kid' => $this->privateKeyId,
'sub' => $uid,
"exp" => time() + ($addHours * 60 * 60)
'exp' => time() + ($addHours * 60 * 60),
], base64_decode($this->privateKeyToken), 'RS256');
}

/**
* Get width and height of a video.
*
* @param string $uid
* @return string
*
* @throws GuzzleException
*/
public function getDimensions(string $uid)
Expand All @@ -307,10 +305,10 @@ public function getDimensions(string $uid)
/**
* Request wrapper function.
*
* @param string $endpoint
* @param string $method
* @param array $data
* @param string $method
* @param array $data
* @return ResponseInterface
*
* @throws GuzzleException
*/
private function request(string $endpoint, $method = 'get', $data = [])
Expand All @@ -319,14 +317,14 @@ private function request(string $endpoint, $method = 'get', $data = [])
$headers = [
'X-Auth-Key' => $this->authKey,
'X-Auth-Email' => $this->authEMail,
'Content-Type' => 'application/json'
'Content-Type' => 'application/json',
];

// Define options for post request method...
if (count($data) && $method === "post") {
if (count($data) && $method === 'post') {
$options = [
'headers' => $headers,
RequestOptions::JSON => $data
RequestOptions::JSON => $data,
];
} // ...or define options for all other request methods
else {
Expand Down
2 changes: 1 addition & 1 deletion src/CloudflareStreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CloudflareStreamServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__ . '/config/cloudflare-stream.php' => config_path('cloudflare-stream.php'),
__DIR__.'/config/cloudflare-stream.php' => config_path('cloudflare-stream.php'),
]);
}

Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/NoCredentialsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class NoCredentialsException extends Exception
{

}
1 change: 0 additions & 1 deletion src/Exceptions/NoPrivateKeyOrTokenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class NoPrivateKeyOrTokenException extends Exception
{

}