Skip to content
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

feat(nodejs) upload with token and videoId #165

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion templates/nodejs/doc/api/api_doc.md.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ All URIs are relative to *{{basePath}}*
| Name | Type | Required | Description |
| ------------- | ------------- | ------------- | ------------- |{{/-last}}{{/allParams}}
{{#allParams}} | **{{paramName}}** | {{#isPrimitiveType}}**{{#isFile}}string \| Readable \| Buffer{{/isFile}}{{^isFile}}{{dataType}}{{/isFile}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isContainer}}**{{dataType}}**{{/isContainer}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}{{^isContainer}}[**{{dataType}}**](../model/{{baseType}}.md){{/isContainer}}{{/isFile}}{{/isPrimitiveType}}| {{#required}}**yes**{{/required}}{{^required}}no{{/required}}| {{description}} |
{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}| **progressListener** | **(event: UploadProgressEvent) => void \| undefined** | no | Optional upload progress listener |{{/vendorExtensions.x-client-chunk-upload}}
{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}| **progressListener** | **(event: UploadProgressEvent) => void \| undefined** | no | Optional upload progress listener |{{/vendorExtensions.x-client-chunk-upload}}{{#vendorExtensions.x-client-copy-from-response}}
| **{{paramName}}** | **string \| undefined** | no | Optional {{paramName}} |{{/vendorExtensions.x-client-copy-from-response}}

### Return type

Expand Down
48 changes: 39 additions & 9 deletions templates/nodejs/src/api/api.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ export default class {{classname}} {
{{#summary}}
* {{&summary}}
* This will create a progressive upload session.{{/summary}}{{#allParams}}{{^isFile}}
* @param {{paramName}} {{description}}{{/isFile}}{{/allParams}}
* @param {{paramName}} {{description}}{{/isFile}}{{/allParams}}{{#vendorExtensions.x-client-copy-from-response}}
* @param {{paramName}} optional {{paramName}} value{{/vendorExtensions.x-client-copy-from-response}}
*/
public create{{#titlecase}}{{nickname}}{{/titlecase}}ProgressiveSession({{#allParams}}{{^isFile}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#vendorExtensions.x-optional-object}} = {}{{/vendorExtensions.x-optional-object}}{{^-last}}, {{/-last}}{{/isFile}}{{/allParams}}): ProgressiveSession<{{{returnType}}}> {
public create{{#titlecase}}{{nickname}}{{/titlecase}}ProgressiveSession({{#allParams}}{{^isFile}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#vendorExtensions.x-optional-object}} = {}{{/vendorExtensions.x-optional-object}}{{^-last}}, {{/-last}}{{/isFile}}{{/allParams}}{{#vendorExtensions.x-client-copy-from-response}} {{paramName}}?: string, {{/vendorExtensions.x-client-copy-from-response}}): ProgressiveSession<{{{returnType}}}> {
class {{#titlecase}}{{nickname}}{{/titlecase}}ProgressiveSession<Type> {
private httpClient: HttpClient;
private currentPart = 1;{{#vendorExtensions.x-client-copy-from-response}}
private {{paramName}}?: string;{{/vendorExtensions.x-client-copy-from-response}}

constructor(httpClient: HttpClient) {
this.httpClient = httpClient;
constructor(httpClient: HttpClient{{#vendorExtensions.x-client-copy-from-response}}, {{paramName}}?: string{{/vendorExtensions.x-client-copy-from-response}}) {
this.httpClient = httpClient;{{#vendorExtensions.x-client-copy-from-response}}
this.{{paramName}} = {{paramName}};{{/vendorExtensions.x-client-copy-from-response}}
}

uploadPart(file: string, progressListener?: (event: UploadProgressEvent) => void) {
Expand Down Expand Up @@ -174,7 +176,7 @@ export default class {{classname}} {
}
}

return new {{#titlecase}}{{nickname}}{{/titlecase}}ProgressiveSession<{{{returnType}}}>(this.httpClient);
return new {{#titlecase}}{{nickname}}{{/titlecase}}ProgressiveSession<{{{returnType}}}>(this.httpClient{{#vendorExtensions.x-client-copy-from-response}}, {{paramName}}{{/vendorExtensions.x-client-copy-from-response}});
}
{{/vendorExtensions.x-client-chunk-upload}}
{{#vendorExtensions.x-group-parameters}}
Expand Down Expand Up @@ -204,10 +206,15 @@ export default class {{classname}} {
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{#isFile}}{{#vendorExtensions.x-client-chunk-upload}}string{{/vendorExtensions.x-client-chunk-upload}}{{^vendorExtensions.x-client-chunk-upload}}string | Readable | Buffer{{/vendorExtensions.x-client-chunk-upload}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}}{{#vendorExtensions.x-optional-object}} = {}{{/vendorExtensions.x-optional-object}}{{^-last}}, {{/-last}}{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}, progressListener?: (event: UploadProgressEvent) => void{{/vendorExtensions.x-client-chunk-upload}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}} {{^returnType}}void{{/returnType}}> {
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{#isFile}}{{#vendorExtensions.x-client-chunk-upload}}string{{/vendorExtensions.x-client-chunk-upload}}{{^vendorExtensions.x-client-chunk-upload}}string | Readable | Buffer{{/vendorExtensions.x-client-chunk-upload}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}}{{#vendorExtensions.x-optional-object}} = {}{{/vendorExtensions.x-optional-object}}{{^-last}}, {{/-last}}{{/allParams}}{{#vendorExtensions.x-client-chunk-upload}}, progressListener?: (event: UploadProgressEvent) => void, {{/vendorExtensions.x-client-chunk-upload}}{{#vendorExtensions.x-client-copy-from-response}}{{paramName}}?: string, {{/vendorExtensions.x-client-copy-from-response}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}} {{^returnType}}void{{/returnType}}> {
{{/vendorExtensions.x-group-parameters}}
const queryParams: QueryOptions = {};
queryParams.headers = {};
{{#vendorExtensions.x-client-chunk-upload}}
const toRelay: { [key: string]: string | undefined } = {
{{#vendorExtensions.x-client-copy-from-response}}{{paramName}}, {{/vendorExtensions.x-client-copy-from-response}}
};
{{/vendorExtensions.x-client-chunk-upload}}
{{#allParams}}
{{#required}}
{{^isFile}}
Expand Down Expand Up @@ -301,8 +308,20 @@ export default class {{classname}} {

{{#hasFormParams}}
const formData = new FormData();
{{/hasFormParams}}

{{/hasFormParams}}{{#vendorExtensions.x-client-copy-from-response}}
Object.keys(toRelay).forEach((key) => {
if (toRelay[key] !== undefined) {
formData.append(key, toRelay[key]);
}
});
{{/vendorExtensions.x-client-copy-from-response}}
{{#formParams}}
{{#vendorExtensions.x-client-copy-from-response}}
if ({{paramName}}) {
formData.append('{{paramName}}', {{paramName}});
}
{{/vendorExtensions.x-client-copy-from-response}}
{{#isArray}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
Expand All @@ -316,7 +335,6 @@ export default class {{classname}} {
}
{{/isArray}}
{{^isArray}}

{{^isFile}}
if (typeof {{paramName}} !== undefined) {
formData.append('{{baseName}}', {{paramName}});
Expand Down Expand Up @@ -362,7 +380,7 @@ export default class {{classname}} {
) as {{{returnType}}});
}
let uploadChunkSize = chunkSize;
let lastBody;
let lastBody: {{{returnType}}} | undefined = undefined;
let stream;
let chunkNumber = 0;

Expand All @@ -383,6 +401,12 @@ export default class {{classname}} {
stream = createReadStream(file, { start: offset, end: uploadChunkSize + offset - 1});
chunkFormData.append(filename, stream, filename);

Object.keys(toRelay).forEach((key) => {
if (toRelay[key] !== undefined) {
chunkFormData.append(key, toRelay[key]);
}
});

queryParams.body = chunkFormData;
queryParams.headers['Content-Range'] = `part ${part}/${partsCount}`;
part++;
Expand All @@ -407,6 +431,12 @@ export default class {{classname}} {
"{{{returnType}}}", "{{returnFormat}}"
) as {{{returnType}}});

Object.keys(toRelay).forEach((key) => {
if ((lastBody as unknown as any)[key] !== undefined) {
toRelay[key] = (lastBody as unknown as any)[key];
}
});

stream.close();
}

Expand Down
6 changes: 3 additions & 3 deletions templates/php/src/VideoUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function uploadWithUploadToken(string $token, \SplFileObject $file, strin
return $this->execute(
'/upload?token='.$token,
$file,
$this->getChunkSize($contentRange)
$this->getChunkSize($contentRange),
$videoId
);
}

Expand All @@ -65,14 +66,13 @@ public function uploadWithUploadToken(string $token, \SplFileObject $file, strin
* @return Video
* @throws ClientExceptionInterface
*/
private function execute(string $path, \SplFileObject $file, int $chunkSize): Video
private function execute(string $path, \SplFileObject $file, int $chunkSize, string $videoId = null): Video
{
$start = 0;
$fileSize = filesize($file->getRealPath());
$handle = fopen($file->getRealPath(), 'r');

$response = null;
$videoId = null;
$part = 1;
$partsCount = ceil($fileSize / $chunkSize);

Expand Down
4 changes: 2 additions & 2 deletions templates/php/statics/.github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
- name: Install dependancies
uses: php-actions/composer@v5
with:
php_version: 7.3
php_version: 8.1
- name: PHPUnit tests
uses: php-actions/phpunit@v2
env:
API_KEY: ${{ secrets.API_KEY }}
BASE_URI: https://sandbox.api.video
with:
args: tests
args: tests --log-junit output.xml
29 changes: 29 additions & 0 deletions templates/php/tests/Api/VideosApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,33 @@ public function testUploadThumbnail()
$this->assertEquals('thumbnail.jpg', basename($video->getAssets()->getThumbnail()));
}

public function testUploadWithUploadTokenWithVideoId()
{
$token = (new Helper($this->client))->createUploadToken();

$video = $this->client->videos()->create((new VideoCreationPayload())
->setTitle('Test video creation')
->setDescription('Test description'));

// No authorization needed for this endpoint
$client = new Client(
$_ENV['BASE_URI'],
null,
new Psr18Client()
);

$uploadedVideo = $client->videos()->uploadWithUploadToken(
$token->getToken(),
new SplFileObject(__DIR__ . '/../resources/558k.mp4'),
null,
$video->getVideoId()
);

$this->client->uploadTokens()->deleteToken($token->getToken());

$this->assertNotNull($uploadedVideo->getAssets()->getPlayer());
}

public function testUploadWithUploadToken()
{
$token = (new Helper($this->client))->createUploadToken();
Expand All @@ -274,6 +301,8 @@ public function testUploadWithUploadToken()
new SplFileObject(__DIR__ . '/../resources/558k.mp4')
);

$this->client->uploadTokens()->deleteToken($token->getToken());

$this->assertNotNull($uploadedVideo->getAssets()->getPlayer());
}
}