forked from ekstep/EkStep-Developer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue ekstep#18 feat: NTP integration class in PHP
- Loading branch information
Showing
3 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}" | ||
} | ||
*/ |