Skip to content

Commit

Permalink
Upload endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Valdeci Jr committed Jun 1, 2018
1 parent cb2ac5c commit ea0cdf0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Samba Videos API

PHP library for Samba Videos API. http://dev.sambatech.com/documentation/sambavideos/index.html
PHP library for Samba Videos API. http://dev.sambatech.com/


## Requirements
Expand Down
26 changes: 22 additions & 4 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,42 @@
*/
require_once '../vendor/autoload.php';

$access_token = 'ACCESS-TOKEN';
$access_token = 'ACCESS_TOKEN';
$media_id = 'MEDIA_ID';

try {
// Projects
$project = \SambaVideos\Resource\Project::instance($access_token);
$projects = $project->search();
$project_id = $projects[0]['id'];
var_dump($projects);
print_r($projects);

// Categories
$category = \SambaVideos\Resource\Category::instance($access_token);
$categories = $category->search(['pid' => $project_id]);
var_dump($categories);
print_r($categories);

// Medias
$media = \SambaVideos\Resource\Media::instance($access_token);
$medias = $media->search(['pid' => $project_id]);
var_dump($medias);
print_r($medias);

// Upload Media
$media = \SambaVideos\Resource\Media::instance($access_token);
$response = $media->create(['qualifier' => 'VIDEO'], ['pid' => $project_id]);
print_r($response);

$response = $media->upload($response['uploadUrl'], 'video.mp4');
print_r($response);


// Upload Thumbnail
$media = \SambaVideos\Resource\Media::instance($access_token);
$response = $media->createThumbnail($media_id, ['pid' => $project_id]);
print_r($response);

$response = $media->upload($response['uploadUrl'], 'thumb.png');
print_r($response);
} catch (Exception $e) {
var_dump($e);
}
Expand Down
Binary file added example/thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/video.mp4
Binary file not shown.
3 changes: 3 additions & 0 deletions src/SambaVideos/Resource/AbstractAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class AbstractAnalytics extends AbstractResource
{
/**
* @inheritDoc
* @throws InvalidOperationException
*/
public function create(array $params, array $body = [])
{
Expand All @@ -38,6 +39,7 @@ public function create(array $params, array $body = [])

/**
* @inheritDoc
* @throws InvalidOperationException
*/
public function update($identifier, array $params, array $body = [])
{
Expand All @@ -46,6 +48,7 @@ public function update($identifier, array $params, array $body = [])

/**
* @inheritDoc
* @throws InvalidOperationException
*/
public function delete($identifier, array $params = [])
{
Expand Down
91 changes: 90 additions & 1 deletion src/SambaVideos/Resource/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
namespace SambaVideos\Resource;

use SambaVideos\AbstractResource;
use SambaVideos\Exception\RequestException;
use Unirest\Exception;
use Unirest\Request;

/**
* Media
Expand All @@ -27,6 +30,92 @@
*/
class Media extends AbstractResource
{
/** @var string */
/** @var string */
protected $uri = '/medias';


/**
* @param int $identifier
* @param array $params
*
* @return array
* @throws RequestException
*/
public function createThumbnail($identifier, array $params = [])
{
try {
$body = ['qualifier' => 'THUMBNAIL'];

$body = Request\Body::Json($body);
$headers = ['Content-Type' => 'application/json'];

$this->response = Request::post($this->buildUrl($params, $identifier), $headers, $body);

if (!in_array($this->response->code, [200, 201, 204])) {
throw $this->createRequestException('Fail to create thumbnail');
}

return $this->response->body;
} catch (Exception $e) {
throw $this->createRequestException($e->getMessage(), Request::getInfo(CURLINFO_HTTP_CODE), $e);
}
}

/**
* @param int $identifier
* @param array $body
* @param array $params
*
* @return array
* @throws RequestException
*/
public function createCaption($identifier, array $body, array $params = [])
{
try {
$body = ['qualifier' => 'CAPTION'] + $body;

$body = Request\Body::Json($body);
$headers = ['Content-Type' => 'application/json'];

$this->response = Request::post($this->buildUrl($params, $identifier), $headers, $body);

if (!in_array($this->response->code, [200, 201, 204])) {
throw $this->createRequestException('Fail to create caption');
}

return $this->response->body;
} catch (Exception $e) {
throw $this->createRequestException($e->getMessage(), Request::getInfo(CURLINFO_HTTP_CODE), $e);
}
}

/**
* @param string $uploadUrl
* @param string $filename
* @param string $mimetype
* @param string $postName
*
* @return mixed
* @throws RequestException
*/
public function upload($uploadUrl, $filename, $mimetype = '', $postName = '')
{
try {
$headers = ['Accept' => 'application/json'];

$body = [
'file' => Request\Body::File($filename, $mimetype, $postName),
];

$this->response = Request::post($uploadUrl, $headers, $body);

if (!in_array($this->response->code, [200, 201, 204])) {
throw $this->createRequestException('Fail to upload file');
}

return $this->response->body;
} catch (Exception $e) {
throw $this->createRequestException($e->getMessage(), Request::getInfo(CURLINFO_HTTP_CODE), $e);
}
}
}

0 comments on commit ea0cdf0

Please sign in to comment.