Tutorial intergrading Kwiktec API

Introduction
The Kwik-Platform is brand new core of KwikFlix streaming system.
API Methods list can be found in API documentation API documentation.
This Tutorial describes Kwik-Platform API integration to your workflow. Examples prepared using php.


Authorization
For authorization using OAuth2 standard (for example: oauth2-client).
You need to follow next steps to have access to API methods:

Get refresh and access token using password grant authentication. See /oauth/access_token method in API 
documentation. The parameters should be in the array.
For example:
$url = 'https://api.infoview.tv/oauth/access_token';
$user_data = array(
 "client_id" => '<application client id>',
 "client_secret" => '<application client secret>',
 "username" => '<username>',
 "password" => '<user password>',
 "grant_type" => 'password',
 );
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_POST => TRUE,
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_POSTFIELDS => $user_data
));
$response = curl_exec($ch);
Sample output:
{
 "access_token": "<access_token>",
 "token_type": "Bearer",
 "expires_in": 3600,
 "refresh_token": "<refresh_token>"
}
Make requests to API methods with header 'Authorization: Bearer <access_token>'.
When access token will expires request new one using ‘refresh_token’ or ‘password’ grant:
$user_data = array(
 "client_id" => '<application client id>',
 "client_secret" => '<application client secret>',
 "refresh_token" => '<refresh_token>',
 "grant_type" => 'refresh_token',
 );

Products
Create Product
Before create product need to upload files using Upload API /v1.0/upload/flow method in API documentation and 
Flow.js library. 
To see the list of uploaded files you can use GET /v1.0/upload method.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/upload';
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header)
));
$response = curl_exec($ch);
Sample output:
{
 "total": 100,
 "per_page": 10,
 "current_page": 1,
 "last_page": 10,
 "next_page_url": "http://api.infoview.tv/v1.0/enduser/?page=2",
 "prev_page_url": null,
 "from": 1,
 "to": 10,
 "data": [
 {
 "id": "1",
 "filename": "queen_smiled.mp4",
 "path": "/platform/uploads/2/queen_smiled.mp4",
 "created_at": "2015-09-11T15:43:10+0000",
 "updated_at": "2015-09-11T15:43:10+0000",
 },
 {
 "...": "..."
 }
 ]
}



To create Product you can use POST /v1.0/product method in API documentation. The parameters should be in the 
json format.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/product';
$file_data = array(
 "title" => 'Queen smiled',
 "upload_id" => '1',
 "status" => 'public'
 );
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_POST => TRUE,
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header),
 CURLOPT_POSTFIELDS => json_encode($file_data)
));
$response = curl_exec($ch);
Sample output:
{
 "message": "resource created successfully"
}
Products list
You can get the list of existing products by GET /v1.0/product method in API documentation. Don't forget about 
access_token in the header.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/product';
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header)
));
$response = curl_exec($ch);
Also you can use request parameters full list which you can see in the API documentation.
For example you can get second page list of 20 public products per page which contains ‘queen’ and sort it by name:
$url = 'https://api.infoview.tv/v1.0/product?
page=2&limit=20&search=queen&sort=name&status=public';


Sample output:
{
 "total": 35,
 "per_page": 20,
 "current_page": 2,
 "last_page": 2,
 "next_page_url": null,
 "prev_page_url": "http://api.infoview.tv/v1.0/product/?page=1",
 "from": 21,
 "to": 35,
 "data": [
 {
 "id": "12",
 "name": "pack00000000000000079",
 "title": "Queen smiled",
 "status": "public",
 "type": "vod",
 "created_at": "2015-09-15T16:51:45+0000",
 "updated_at": "2015-09-15T16:51:45+0000"
 },
 {
 "id": "6",
 "name": "pack0000000000000202",
 "title": "Playing the Queen",
 "status": "public",
 "type": "live",
 "live_upstream_url": "http://example.com/mylive.m3u8",
 "created_at": "2015-09-15T16:51:45+0000",
 "updated_at": "2015-09-15T16:51:45+0000"
 },
 {
 "...": "..."
 }
 ]
}

In the output you get the data array with products information and parameters for building pagination:
total - the total number of products which corresponding request;
per_page - number of products which you get in the output (default - 10);
current_page - the current page (default - 1);
last_page - total number of pages;
from - first item on page (default - 1);
to - last item on page.
To get the one product info you should add the product id to the URL:
$url = 'https://api.infoview.tv/v1.0/product/12';
End Users
Create End User

To adding new End User you can use POST /v1.0/enduser method in API documentation. The parameters should be in the 
json format.
For example:

$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/enduser';
$end_user_data = array(
 "email" => 'Lessie44@OReilly.com',
 "name" => 'Bruce Howell',
 );
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_POST => TRUE,
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header),
 CURLOPT_POSTFIELDS => json_encode($end_user_data)

));
$response = curl_exec($ch);
Sample output:
{
 "message": "resource created successfully"
}
End Users list
You can get the list of existing End Users by GET /v1.0/enduser method.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/enduser';
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header)
));
$response = curl_exec($ch);



Sample output:
{
 "total": 10000,
 "per_page": 10,
 "current_page": 1,
 "last_page": 1000,
 "next_page_url": "http://api.infoview.tv/v1.0/enduser/?page=2",
 "prev_page_url": null,
 "from": 1,
 "to": 10,
 "data": [
 {
 "id": "2",
 "name": "Bruce Howell",
 "email": "Lessie44@OReilly.com",
 "created_at": "2015-09-11T15:43:10+0000",
 "updated_at": "2015-09-11T15:43:10+0000",
 "purchases_qty": 0
 },
 {
 "id": "3",
 "name": "Kiana Ryan III",
 "email": "Rick23@Lind.biz",
 "created_at": "2015-09-11T15:43:10+0000",
 "updated_at": "2015-09-11T15:43:10+0000",
 "purchases_qty": 117
 },
 {
 "...": "..."
 }
 ]
}
To get the one End User info you should add the End User id to the URL:
$url = 'https://api.infoview.tv/v1.0/enduser/2';

Purchases
Create Purchase

To adding new Purchase use POST /v1.0/purchase method in API documentation. The parameters should be in the json 
format.
The expires_at should be in UTC time.
To create unlimited purchase don’t set product_id parameter.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/purchase';
$purchase_data = array(
 "enduser_id" => '2',
 "product_id" => '12',
 "expires_at" => '2015-12-31T21:10:32+0000'
 );
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_POST => TRUE,
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header),
 CURLOPT_POSTFIELDS => json_encode($purchase_data)

));
$response = curl_exec($ch);

Sample output:
{
 "message": "resource created successfully"
}
Purchases list
You can get the list of existing Purchases by GET /v1.0/purchase method.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/purchase';
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header)
));
$response = curl_exec($ch);
Sample output:
{
 "total": 10000,
 "per_page": 10,
 "current_page": 1,
 "last_page": 1000,
 "next_page_url": "http://api.infoview.tv/v1.0/purchase/?page=2",
 "prev_page_url": null,
 "from": 1,
 "to": 10,
 "data": [
 {
 "id": "332",
 "enduser_id": "2",
 "product_id": "12",
 "is_expired": "false",
 "expires_at": "2015-12-31T21:10:32+0000",
 "created_at": "2015-11-24T21:10:32+0000",
 "updated_at": "2015-11-24T21:10:32+0000",
 "enduser": {
 "id": "2",
 "name": "Bruce Howell"
 },
 "product": {
 "id": "12",
 "name": "pack00000000000000079",
 "title": "Queen smiled",
 "type": "vod"
 },
 },
 {
 "...": "..."
 }
 ]
}
To get the one Purchase info you should add the Purchase id to the URL:
$url = 'https://api.infoview.tv/v1.0/purchase/332';
Get purchases for certain End User
You can get purchases list for certain End User by adding parameters to the URL. Also you can sort the output data 
by types of products. If product is null then purchase is unlimited (for all products). It’s means that such 
purchase allow to watch any product.
To prevent pagination set limit to 999.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$url = 'https://api.infoview.tv/v1.0/purchase?enduser_id=2&limit=999';
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header)
));
$response = curl_exec($ch);
/* $result will contain all purchases for certain user */
$result = json_decode($response, TRUE);
/* Sort output array by types */
$live = [];
$vod = [];
$unlim = [];
foreach ($result['data'] as $item) {
 if ($item['product'] === null) {
 array_push($unlim, $item);
 } else if ($item['product']['type'] === 'vod') {
 array_push($vod, $item);
 } else if ($item['product']['type'] === 'live') {
 array_push($live, $item);
 }
}






Product play URL
To get the product play URL use method GET /v1.0/product/{id}/play.
For example:
$auth_header = 'Authorization: Bearer <access_token>';
$product_id = '12';
$enduser_id = '2';
$url = 'https://api.infoview.tv/v1.0/product/'
. $product_id . '/play?enduser_id=' . $enduser_id;
$ch = curl_init($url);
curl_setopt_array($ch, array(
 CURLOPT_RETURNTRANSFER => TRUE,
 CURLOPT_HTTPHEADER => array($auth_header)
));
$response = curl_exec($ch);
Sample output:
{
 "url": "http://players.infoview.tv/pack00000000000000079"
}

If you get message "Record not found" check the following:
the Product with requested id exist;
the Product have public status;
the End User with requested id exist;
the Purchase for this End User with this Product or Unlim exist and not expired.

This works with VOD and Live Product types.

One thought on “Tutorial intergrading Kwiktec API

Leave a Reply

Your email address will not be published. Required fields are marked *