Skip to content

Commit

Permalink
Merge pull request #174 from vimeo/promise-branch
Browse files Browse the repository at this point in the history
Adds Promise API
  • Loading branch information
hedyyytang authored Feb 22, 2023
2 parents 2fddf7c + 11e407f commit 69725c9
Show file tree
Hide file tree
Showing 14 changed files with 1,332 additions and 226 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ lib-cov
*.out
*.pid
*.gz
*.lcov

pids
logs
Expand All @@ -16,3 +17,5 @@ node_modules
package-lock.json

example/config.json
.nyc_output/
.husky/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [3.0.0] - 2023-02-22
## Added
- Adds Promise API ([#174](https://github.com/vimeo/vimeo.js/pull/174))

## [2.3.2] - 2023-01-23
## Changed
- package.json ([#166](https://github.com/vimeo/vimeo.js/pull/166))
Expand Down Expand Up @@ -70,6 +74,7 @@
### Added
- First release.

[3.0.0]: https://github.com/vimeo/vimeo.js/compare/2.3.1...3.0.0
[2.3.1]: https://github.com/vimeo/vimeo.js/compare/2.3.0...2.3.1
[2.3.0]: https://github.com/vimeo/vimeo.js/compare/2.2.0...2.3.0
[2.2.0]: https://github.com/vimeo/vimeo.js/compare/2.1.1...2.2.0
Expand Down
25 changes: 13 additions & 12 deletions example/auth_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
* limitations under the License.
*/

var Vimeo = require('../index').Vimeo
var utilModule = require('util')
const Vimeo = require('../index').Vimeo
const utilModule = require('util')
let config = {}

try {
var config = require('./config.json')
config = require('./config.json')
} catch (error) {
console.error('ERROR: For this example to run properly you must create an API app at ' +
'https://developer.vimeo.com/apps/new and set your callback url to ' +
Expand All @@ -30,25 +31,25 @@ try {
process.exit()
}

var httpModule = require('http')
var urlModule = require('url')
const httpModule = require('http')
const urlModule = require('url')

var stateData = {
const stateData = {
state: 'unauthorized'
}

// Here we have to build the Vimeo library using the configured `client_id` and `client_secret`. We
// do not need an access token here because we will generate one. If we already knew our access
// token, we can provide it as the third parameter.
var lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)

var scopes = ['public', 'private', 'edit', 'interact']
var callbackUrl = 'http://localhost:8080/oauth_callback'
const scopes = ['public', 'private', 'edit', 'interact']
const callbackUrl = 'http://localhost:8080/oauth_callback'

// The authorization process requires the user to be redirected back to a webpage, so we can start
// up a simple HTTP server here.
var server = httpModule.createServer(function (request, response) {
var url = urlModule.parse(request.url, true)
const server = httpModule.createServer(function (request, response) {
const url = urlModule.parse(request.url, true) // eslint-disable-line n/no-deprecated-api

// Once the user accepts your app, they will be redirected back to
// `http://localhost:8080/oauth_callback`. If they are not redirected you should check your apps
Expand Down Expand Up @@ -121,7 +122,7 @@ server.listen(8080, function () {
console.log('Server started on 8080. Open up http://localhost:8080 in your browser.')
})

var context = require('repl').start({}).context
const context = require('repl').start({}).context

/**
* This will upload the video to the authenticated account.
Expand Down
9 changes: 5 additions & 4 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
* limitations under the License.
*/

var Vimeo = require('../index').Vimeo
var util = require('util')
const Vimeo = require('../index').Vimeo
const util = require('util')
let config = {}

try {
var config = require('./config.json')
config = require('./config.json')
} catch (error) {
console.error('ERROR: For this example to run properly you must create an API app at ' +
'https://developer.vimeo.com/apps/new and set your callback url to ' +
Expand All @@ -35,7 +36,7 @@ try {
//
// For the request we make below (/channels) the access token can be a client access token instead
// of a user access token.
var lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)

if (config.access_token) {
lib.setAccessToken(config.access_token)
Expand Down
9 changes: 5 additions & 4 deletions example/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
* limitations under the License.
*/

var Vimeo = require('../index').Vimeo
var util = require('util')
const Vimeo = require('../index').Vimeo
const util = require('util')
let config = {}

try {
var config = require('./config.json')
config = require('./config.json')
} catch (error) {
console.error('ERROR: For this example to run properly you must create an API app at ' +
'https://developer.vimeo.com/apps/new and set your callback url to ' +
Expand Down Expand Up @@ -59,7 +60,7 @@ function makeRequest (lib) {
})
}

var lib = new Vimeo(config.client_id, config.client_secret, config.access_token)
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)

if (config.access_token) {
lib.setAccessToken(config.access_token)
Expand Down
21 changes: 11 additions & 10 deletions example/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
* limitations under the License.
*/

var Vimeo = require('../index').Vimeo
const Vimeo = require('../index').Vimeo
let config = {}

try {
var config = require('./config.json')
config = require('./config.json')
} catch (error) {
console.error('ERROR: For this example to run properly you must create an API app at ' +
'https://developer.vimeo.com/apps/new and set your callback url to ' +
Expand All @@ -34,16 +35,16 @@ if (!config.access_token) {
}

// Instantiate the library with your client id, secret and access token (pulled from dev site)
var client = new Vimeo(config.client_id, config.client_secret, config.access_token)
const client = new Vimeo(config.client_id, config.client_secret, config.access_token)

// Create a variable with a hard coded path to your file system
var filePath = '<full path to a video on the filesystem>'
const filePath = '<full path to a video on the filesystem>'

console.log('Uploading: ' + filePath)

var params = {
'name': 'Vimeo API SDK test upload',
'description': "This video was uploaded through the Vimeo API's NodeJS SDK."
const params = {
name: 'Vimeo API SDK test upload',
description: "This video was uploaded through the Vimeo API's NodeJS SDK."
}

client.upload(
Expand All @@ -65,8 +66,8 @@ client.upload(
method: 'PATCH',
path: uri,
params: {
'name': 'Vimeo API SDK test edit',
'description': "This video was edited through the Vimeo API's NodeJS SDK."
name: 'Vimeo API SDK test edit',
description: "This video was edited through the Vimeo API's NodeJS SDK."
}
}, function (error, body, statusCode, headers) {
if (error) {
Expand Down Expand Up @@ -94,7 +95,7 @@ client.upload(
})
},
function (bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
const percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
console.log(bytesUploaded, bytesTotal, percentage + '%')
},
function (error) {
Expand Down
86 changes: 86 additions & 0 deletions example/upload_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict'

/**
* Copyright 2023 Vimeo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const Vimeo = require('../index').Vimeo

let config = {}

try {
config = require('./config.json')
} catch (error) {
console.error('ERROR: For this example to run properly you must create an API app at ' +
'https://developer.vimeo.com/apps/new and set your callback url to ' +
'`http://localhost:8080/oauth_callback`.')
console.error('ERROR: Once you have your app, make a copy of `config.json.example` named ' +
'`config.json` and add your client ID, client secret and access token.')
process.exit()
}

if (!config.access_token) {
throw new Error('You can not upload a video without configuring an access token.')
}

// Instantiate the library with your client id, secret and access token (pulled from dev site)
const client = new Vimeo(config.client_id, config.client_secret, config.access_token)

// Create a variable with a hard coded path to your file system
const filePath = 'path'

const uploadVideo = async (filePath) => {
console.log('Uploading: ' + filePath)

const params = {
name: 'Vimeo API SDK test upload',
description: "This video was uploaded through the Vimeo API's NodeJS SDK."
}

const progressCallback = (bytesUploaded, bytesTotal) => {
const percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
console.log(bytesUploaded, bytesTotal, percentage + '%')
}

const uri = await client.upload(
filePath,
params,
progressCallback
).catch(e => console.log(e))

// Get the metadata response from the upload and log out the Vimeo.com url
const metadata = await client.request(uri + '?fields=link').catch(e => console.log(e))

console.log('The file has been uploaded to ' + metadata.link)

// Make an API call to edit the title and description of the video.
const editRes = await client.request({
method: 'PATCH',
path: uri,
params: {
name: 'Vimeo API SDK test edit',
description: "This video was edited through the Vimeo API's NodeJS SDK."
}
}).catch(e => console.log(e))

console.log(editRes.statusCode)
console.log('The title and description for ' + uri + ' has been edited')

// Make an API call to see if the video is finished transcoding.
const transcodeStatus = await client.request(uri + '?fields=transcode.status').catch(e => console.log(e))
console.log('The transcode status for ' + uri + ' is: ' + transcodeStatus.body.transcode.status)
}

uploadVideo(filePath)
91 changes: 91 additions & 0 deletions example/upload_texttrack_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'

/**
* Copyright 2023 Vimeo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const fs = require('fs')
const Vimeo = require('../index').Vimeo
let config

try {
config = require('./config.json')
} catch (error) {
console.error('ERROR: For this example to run properly you must create an API app at ' +
'https://developer.vimeo.com/apps/new and set your callback url to ' +
'`http://localhost:8080/oauth_callback`.')
console.error('ERROR: Once you have your app, make a copy of `config.json.example` named ' +
'`config.json` and add your client ID, client secret and access token.')
process.exit()
}
const lib = new Vimeo(config.client_id, config.client_secret, config.access_token)

// Documentation: https://developer.vimeo.com/api/upload/texttracks

const getUploadLink = async (path, token, type = 'captions', language = 'en', name = '') => {
const res = await lib.request(
{
hostname: 'api.vimeo.com',
path,
headers: {
Authorization: 'bearer ' + token,
'Content-Type': 'application/json'
},
method: 'POST',
query: {
type,
language,
name
}
}).catch(e => console.log(e))
console.log(res)
return res
}

const uploadTextTrack = async (textTrackUrl, token, filePath) => {
const vttSubs = fs.readFileSync(
filePath,
'utf-8'
)

const res = await lib.request(
{
hostname: 'captions.cloud.vimeo.com',
path: textTrackUrl.replace('https://captions.cloud.vimeo.com/', ''),
headers: {
Authorization: 'bearer ' + token,
'Content-Type': 'text/plain' // important!
},
method: 'PUT',
body: vttSubs
}
).catch(e => console.log(e))

return res
}

const uploadTextTrackCycle = async () => {
const token = config.access_token
const textTrackUri = '/videos/[video id]/texttracks'
const vttPath = 'path'

const uploadLink = await getUploadLink(textTrackUri, token)
console.log(uploadLink.body.link)

const res = await uploadTextTrack(uploadLink.body.link, token, vttPath)
console.log(res.statusCode)
}

uploadTextTrackCycle()
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var vimeoModule = require('./lib/vimeo')
const vimeoModule = require('./lib/vimeo')
module.exports.vimeo_module = vimeoModule
module.exports.Vimeo = vimeoModule.Vimeo
Loading

0 comments on commit 69725c9

Please sign in to comment.