diff --git a/Examples/callback_test/README.md b/Examples/callback_test/README.md new file mode 100644 index 0000000..c1440db --- /dev/null +++ b/Examples/callback_test/README.md @@ -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://: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. + diff --git a/Examples/callback_test/callback_handler.js b/Examples/callback_test/callback_handler.js new file mode 100644 index 0000000..80334a7 --- /dev/null +++ b/Examples/callback_test/callback_handler.js @@ -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}`); +}); diff --git a/Examples/callback_test/testCallback.js b/Examples/callback_test/testCallback.js new file mode 100644 index 0000000..1774a28 --- /dev/null +++ b/Examples/callback_test/testCallback.js @@ -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) + }) + } + + +})(); +