Skip to content
Merged
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
25 changes: 25 additions & 0 deletions Examples/callback_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Callback sample scripts
Example of sending job with callback_url and payload

- testCallback.js: launches a transcoding job specifying a "callback_url" param and payload for the job.
- callback_handler.js: server side script to accept the callback request. The callback_url param for the job should contain a valid HTTP(S) URL where the callback_handler.js script is working.

## Workflow
1. Install prerequisites for callback_handler.js:
```
npm install express body-parser
```
2. Launch callback_handler.js:
```
node callback_handler.js
```
By default it will start the HTTP service on port 3000, the callback url will be http://<your_host>:3000/callback
You can optionally use [ngrok](https://ngrok.com/) tool on localhost to provide you with a public URL:
```
ngrok http 3000
```
ngrok will provide a public URL (e.g., https://abc123.ngrok.io). Use this URL as the callback URL in Qencode's settings.
3. Excute testCallback.js to run a test job. Don't forget to specify your Qencode API Key and your callback url.

You should see notifications on callback events in the callback_handler.js output.

31 changes: 31 additions & 0 deletions Examples/callback_test/callback_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const port = 3000;

// Middleware to parse URL-encoded bodies (as sent by HTML forms)
app.use(bodyParser.urlencoded({ extended: true }));

// POST endpoint to handle the callback
app.post('/callback', (req, res) => {
// Extracting the task token and payload from the request body
const taskToken = req.body.task_token;
const event = req.body.event;
const payload = req.body.payload ? JSON.parse(req.body.payload) : null;

// Logging the task token and payload
console.log('Received Callback');
console.log('Task Token:', taskToken);
console.log('Event:', event)
if (payload) {
console.log('Payload:', payload);
}

// Sending a response back to the caller
res.send('Callback received');
});

app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
54 changes: 54 additions & 0 deletions Examples/callback_test/testCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(async function () {

const QencodeApiClient = require('qencode-api');

const apiKey = "your_api_key_here";

const payload = '{"test": "payload data"}';

let transcodingParams = {
"source": "https://nyc3.s3.qencode.com/qencode/bbb_30s.mp4",
"format": [
{
"output": "metadata"
}
],
"callback_url": "https://abc123.ngrok-free.app/callback"
};



const qencodeApiClient = await new QencodeApiClient(apiKey);
console.log("AccessToken: ", qencodeApiClient.AccessToken);

let task = await qencodeApiClient.CreateTask();
console.log("Created new task: ", task.taskToken);

await task.StartCustom(transcodingParams, payload);
console.log("Status URL: ", task.statusUrl);


// example on how to get status
CheckTaskStatus();

async function CheckTaskStatus(){
let statusObject = await task.GetStatus()
let status = statusObject.status
while (status != "completed") {
statusObject = await task.GetStatus()
status = statusObject.status;
progress = statusObject.percent;
console.log(`task: ${task.taskToken} | status: ${status} | progress: ${progress}`);
await sleep(10000);
}
}

function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}


})();