Skip to content

Commit

Permalink
Issue ekstep#18 feat: NTP integration class in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
gsbajaj72 committed Jul 31, 2017
1 parent e1a9cad commit a639452
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ntp-integration/php/ntp/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[API]
api_base_url = 'https://dev.open-sunbird.org/api/'
api_org_create = 'org/v1/create'
api_org_update = 'org/v1/update'
api_user_create = 'user/v1/create'
api_user_update = 'user/v1/update'
api_user_associate = 'org/v1/member/add'
api_user_disassociate = 'org/v1/member/remove'

header_content_type = 'application/json'
header_authorization = ''
provider = ''

[CURL]
curl_header_out = true
curl_returntransfer = true
curl_post = true
curl_verify_ssl = true
207 changes: 207 additions & 0 deletions ntp-integration/php/ntp/ntp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

/**
* This class helps a partner organization integrate with NTP
*
* @author G S Bajaj <[email protected]>
* @see https://github.com/project-sunbird/sunbird-commons/wiki/Partner-Organization-and-User-APIs
*/
class Ntp
{
private $_config;

public function __construct()
{
$this->_config = parse_ini_file('config.ini');
}

/**
* This function helps create an organization with NTP for a partner
*
* @param Array $data Array of values needed by the API
*
* @return Array Status and response (optional)
*/
public function createOrganization($data)
{
$apiUrl = $this->_config['api_base_url'] . $this->_config['api_org_create'];
$apiBody = '{
"request":{
"orgName": "' . $data['name'] . '",
"description": "' . $data['description'] . '",
"externalId":' . $data['id'] . ',
"source":"' . $this->_config['provider'] . '"
}
}';

return $this->exec($apiUrl, $apiBody);
}

/**
* This function helps update an organization with NTP for a partner
*
* @param Array $data Array of values needed by the API
*
* @return Array Status and response (optional)
*/
public function updateOrganization($data)
{
$apiUrl = $this->_config['api_base_url'] . $this->_config['api_org_update'];
$apiBody = '{
"request":{
"orgName": "' . $data['name'] . '",
"description": "' . $data['description'] . '",
"externalId":' . $data['id'] . ',
"source":"' . $this->_config['provider'] . '"
}
}';

return $this->exec($apiUrl, $apiBody);
}

/**
* This function helps create a user with NTP for a partner
*
* @param Array $data Array of values needed by the API
*
* @return Array Status and response (optional)
*/
public function createUser($data)
{
$apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_create'];
$apiBody = '{
"request":{
"firstName": "' . $data['fname'] . '",
"lastName": "' . $data['lname'] . '",
"provider":"' . $this->_config['provider'] . '",
"email": "' . $data['email'] . '",
"emailVerified":' . $data['emailVerified'] . ',
"userName":"' . $data['userName'] . '",
"phone": "' . $data['phone'] . '",
"phoneNumberVerified":' . $data['phoneVerified'] . '
}
}';

return $this->exec($apiUrl, $apiBody);
}

/**
* This function helps update a user with NTP for a partner
*
* @param Array $data Array of values needed by the API
*
* @return Array Status and response (optional)
*/
public function updateUser($data)
{
$apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_update'];
$apiBody = '{
"request":{
"firstName": "' . $data['fname'] . '",
"lastName": "' . $data['lname'] . '",
"provider":"' . $this->_config['provider'] . '",
"email": "' . $data['email'] . '",
"emailVerified":' . $data['emailVerified'] . ',
"userName":"' . $data['userName'] . '",
"phone": "' . $data['phone'] . '",
"phoneNumberVerified":' . $data['phoneVerified'] . '
}
}';

return $this->exec($apiUrl, $apiBody);
}

/**
* This function helps associate a user and Organization with NTP for a partner
*
* @param Array $data Array of values needed by the API
*
* @return Array Status and response (optional)
*/
public function addUserToOrganization($data)
{
$apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_associate'];
$apiBody = '{
"request":{
"externalId": "' . $data['orgId'] . '",
"source":"' . $this->_config['provider'] . '",
"userName": "' . $data['userName'] . '",
"role":' . $data['userRole'] . '
}
}';

return $this->exec($apiUrl, $apiBody);
}

/**
* This function helps dis-associate a user and Organization with NTP for a partner
*
* @param Array $data Array of values needed by the API
*
* @return Array Status and response (optional)
*/
public function removeUserFromOrganization($data)
{
$apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_disassociate'];
$apiBody = '{
"request":{
"externalId": "' . $data['orgId'] . '",
"source":"' . $this->_config['provider'] . '",
"userName": "' . $data['userName'] . '"
}
}';

return $this->exec($apiUrl, $apiBody);
}

/**
* Execute the CURL call to the NTP API
*
* @param String $url API Endpoint
* @param Array $data Array of data required by API
* @param Array $headers Array of header. If passed, default headers will be REPLACED by this.
*
* @return Array Status of CURL, response from NTP (if Status is success)
*/
protected function exec($url, $data = '', $headers = '')
{
// Prepare headers
$chHeaders = "";
if ($headers) {
$chHeaders = $headers;
} else {
$chHeaders = array("Content-Type: " . $this->_config['header_content_type'],
"Authorization: " . $this->_config['header_authorization']);
}

// Prepare CURL options
$chOptions = array(CURLOPT_URL => $url,
CURLINFO_HEADER_OUT => $this->_config['curl_header_out'],
CURLOPT_RETURNTRANSFER => $this->_config['curl_returntransfer'],
CURLOPT_POST => $this->_config['curl_post'],
CURLOPT_SSL_VERIFYPEER => $this->_config['curl_verify_ssl']);

if ($chHeaders) {
$chOptions[CURLOPT_HTTPHEADER] = $chHeaders;
}

if ($data) {
$chOptions[CURLOPT_POSTFIELDS] = $data;
}

// Make a call
$ch = curl_init();
curl_setopt_array($ch, $chOptions);
$response = curl_exec($ch);

// Return
if (curl_error($ch)) {
return array('status' => 'error');;
} else {
return array('status' => 'success',
'response' => $response);;
}

curl_close($ch);
}
}
38 changes: 38 additions & 0 deletions ntp-integration/php/sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

require_once 'ntp/ntp.php';

/************* Create user *************/
$data = array('fname' => 'A V S S SUBBA',
'email' => '[email protected]',
'emailVerified' => 'true',
'userName' => '9341229258',
'phone' => '9341229258',
'phoneVerified' => 'true',
);

$ntp = new Ntp();
$resp = $ntp->createUser($data);

echo "<pre>";
var_dump($resp);
echo "</pre>";


/*
ERROR RESPONSE
array(2) {
["status"]=>
string(7) "success"
["response"]=>
string(327) "{"id":"api.user.create","ver":"v1","ts":"2017-07-31 04:42:15:103+0000","params":{"resmsgid":null,"msgid":"13cb6a7e-16fa-44ec-9634-6a1d9abc112f","err":"USERNAME_EMAIL_IN_USE","status":"USERNAME_EMAIL_IN_USE","errmsg":"Username is already in use. Please try with a different username."},"responseCode":"CLIENT_ERROR","result":{}}"
}
SUCCESS RESPONSE
array(2) {
["status"]=>
string(7) "success"
["response"]=>
string(255) "{"id":"api.user.create","ver":"v1","ts":"2017-07-31 04:44:08:598+0000","params":{"resmsgid":null,"msgid":null,"err":null,"status":"success","errmsg":null},"responseCode":"OK","result":{"response":"SUCCESS","userId":"44291d87-e964-40d7-84a1-8498f92f0ec2"}}"
}
*/

0 comments on commit a639452

Please sign in to comment.