New SLIM wrapper

parent 3cc786c8
......@@ -4,6 +4,10 @@ $settings = $app->getContainer()->get('settings');
$username = isset($settings['app']['secure_endpoint_username']) ? $settings['app']['secure_endpoint_username'] : '';
$password = isset($settings['app']['secure_endpoint_password']) ? $settings['app']['secure_endpoint_password'] : '';
// $app->getContainer()->get('router')
// ->getNamedRoute('queryID')
// ->add(new Gr\Gov\Minedu\Osteam\Slim\AuthorizationGuard($username, $password));
$app->getContainer()->get('router')
->getNamedRoute('queryID')
->add(new Gr\Gov\Minedu\Osteam\Slim\AuthorizationGuard($username, $password));
$app->getContainer()->get('router')
->getNamedRoute('student')
->add(new Gr\Gov\Minedu\Osteam\Slim\AuthorizationGuard($username, $password));
......@@ -14,14 +14,13 @@ use Gr\Gov\Minedu\Osteam\Slim\Client;
*/
class App extends BaseApp
{
private $urlAcademicId = '';
private $urlAmka = '';
protected $username = '';
protected $password = '';
protected $secureEndpointUsername = '';
protected $secureEndpointPassword = '';
protected $urlAcademicId = '';
protected $urlAmka = '';
public function __construct(ContainerInterface $ci)
{
parent::__construct($ci);
......@@ -32,10 +31,9 @@ class App extends BaseApp
$this->password = (isset($settings['app']['password']) ? $settings['app']['password'] : 0);
$this->secureEndpointUsername = (isset($settings['app']['secure_endpoint_username']) ? $settings['app']['secure_endpoint_username'] : '');
$this->secureEndpointPassword = (isset($settings['app']['secure_endpoint_password']) ? $settings['app']['secure_endpoint_password'] : '');
$this->urlAcademicId = (isset($settings['app']['ws_endpoint_academic_id']) ? $settings['app']['ws_endpoint_academic_id'] : '');
$this->urlAmka = (isset($settings['app']['ws_endpoint_amka']) ? $settings['app']['ws_endpoint_amka'] : '');
}
$this->urlAcademicId = 'https://academicidapp.grnet.gr/admin/web/ws/users/inspectAcademicID';
$this->urlAmka = 'https://academicidapp.grnet.gr/admin/web/ws/users/inspectAMKA';
}
protected function generateAuth()
......@@ -54,16 +52,33 @@ class App extends BaseApp
*/
public function queryID($req, $res, $args)
{
$identity = $args['identity'];
if (preg_match('/^[0-9]{12}$/', $identity) !== 1) {
$identity = $req->getQueryParam('id', 0);
if (preg_match('/[^0-9]/', $identity) === 1) {
return $res->withJson(array_merge(BaseApp::coreResponseData(false), [
'message' => 'Error: Service call parameters error, academic id must be 12 digit number'
'message' => 'Error: Service Call Parameters Error, academic id or student amka id must be a number'
]), 400);
} elseif (preg_match('/^[0-9]{12}$/', $identity) === 1) {
$data = json_encode([
"SubmissionCode" => $identity
]);
$endpoint = $this->urlAcademicId;
} elseif (preg_match('/^[0-9]{11}$/', $identity) === 1) {
$data = json_encode([
"AMKA" => $identity
]);
$endpoint = $this->urlAmka;
} else {
return $res->withJson(array_merge(BaseApp::coreResponseData(false), [
'message' => 'Error: Service Call Parameters Error, academic id must be a 12 digit number, amka id must be a 11 digit number'
]), 400);
}
$data = json_encode([
"SubmissionCode" => $identity
]);
$username = $req->getQueryParam('username', null);
$password = $req->getQueryParam('password', null);
if ($username !== null && $password !== null) {
$this->username = $username;
$this->password = $password;
}
$this->logger->info("queryID::{$identity}");
......@@ -75,12 +90,105 @@ class App extends BaseApp
'User-Agent: Academic ID SLIM Client/v1.0 osteam'
];
$results = $this->client->post($this->urlAcademicId, $data, $headers);
$results = $this->client->post($endpoint, $data, $headers);
if ($results['success'] === false) {
return $this->withJsonReady($res, $results['response'], $results['http_status']);
}
$result = $results['response'];
return $this->withJsonReady($res, $result);
$fields_requested = $req->getQueryParam('fields', null);
$field_names = [];
if ($fields_requested) {
$field_names = explode(',', $fields_requested);
}
if (count($field_names) > 0) {
// return specific fields
$parsed_result = json_decode($results['response'], true);
if ($parsed_result === false) {
return $this->withJsonReady($res, $results['response'], $results['http_status']);
}
if ($parsed_result['response'] === 'SUCCESS'
&& $parsed_result['errorReason'] === null
&& isset($parsed_result['inspectionResult'])
&& $parsed_result['inspectionResult']['webServiceSuccess'] === true) {
$response_texts = array_map(function ($v) use ($parsed_result) {
return isset($parsed_result['inspectionResult'][$v]) ?
$parsed_result['inspectionResult'][$v] :
null;
}, $field_names);
if (in_array(null, $response_texts)) {
return $res->withJson(array_merge(BaseApp::coreResponseData(false), [
'message' => 'Error: Service Call Parameters Error, fields query parameter has unknown fields'
]), 400);
} else {
return $this->withTextReady($res, implode(',', $response_texts), 400);
}
}
return $this->withJsonReady($res, $results['response'], $results['http_status']);
} else {
// return whole response
$result = $results['response'];
return $this->withJsonReady($res, $result);
}
}
/**
*
* @param Psr\Http\Message\ServerRequestInterface $req
* @param Psr\Http\Message\ResponseInterface $res
* @param $args
* @throws \Exception
* @return Response
*/
public function queryIDnoCD($req, $res, $args)
{
$identity = $req->getQueryParam('id', 0);
if (preg_match('/[^0-9]/', $identity) === 1) {
return $res->withJson(array_merge(BaseApp::coreResponseData(false), [
'message' => 'Error: Service Call Parameters Error, academic id or student amka id must be a number'
]), 400);
} elseif (preg_match('/^[0-9]{12}$/', $identity) === 1) {
$data = json_encode([
"SubmissionCode" => $identity
]);
$endpoint = $this->urlAcademicId;
} elseif (preg_match('/^[0-9]{11}$/', $identity) === 1) {
$data = json_encode([
"AMKA" => $identity
]);
$endpoint = $this->urlAmka;
} else {
return $res->withJson(array_merge(BaseApp::coreResponseData(false), [
'message' => 'Error: Service Call Parameters Error, academic id must be a 12 digit number, amka id must be a 11 digit number'
]), 400);
}
$this->logger->info("queryIDnoCD::{$identity}");
$auth = $this->generateAuth();
$headers = [
"Authorization: {$auth}",
'Content-Type: application/json',
'Accept: */*',
'User-Agent: Academic ID SLIM Client/v1.0 osteam'
];
$results = $this->client->post($endpoint, $data, $headers);
if ($results['success'] === false) {
return $this->withJsonReady($res, $results['response'], $results['http_status']);
}
$parsed_result = json_decode($results['response'], true);
if ($parsed_result === false) {
return $this->withTextReady($res, 'Unknown', 502);
}
if ($parsed_result['response'] === 'SUCCESS' && $parsed_result['errorReason'] === null) {
if (isset($parsed_result['inspectionResult'])
&& $parsed_result['inspectionResult']['webServiceSuccess'] === true) {
return $this->withTextReady($res, 'true');
}
}
return $this->withTextReady($res, 'false');
}
/**
......@@ -96,7 +204,7 @@ class App extends BaseApp
$identity = $args['identity'];
if (preg_match('/^[0-9]{11}$/', $identity) !== 1) {
return $res->withJson(array_merge(BaseApp::coreResponseData(false), [
'message' => 'Error: Service call parameters error, AMKA must be 11 digit number'
'message' => 'Error: Service Call Parameters Error, student amka id must be 11 digit number'
]), 400);
}
......@@ -104,7 +212,7 @@ class App extends BaseApp
"AMKA" => $identity
]);
$this->logger->info("queryID::{$identity}");
$this->logger->info("student::{$identity}");
$auth = $this->generateAuth();
$headers = [
......@@ -116,13 +224,22 @@ class App extends BaseApp
$results = $this->client->post($this->urlAmka, $data, $headers);
if ($results['success'] === false) {
return $this->withJsonReady($res, $results['response'], $results['http_status']);
return $this->withJsonReady($res, $results['response'], $results['http_status']);
}
$result = $results['response'];
return $this->withJsonReady($res, $result);
}
// έλεγχος επιστρεφόμενης τιμής και αποστολή αποτελέσματος
$parsed_result = json_decode($results['response'], true);
if ($parsed_result === false) {
return $this->withTextReady($res, 'Unknown', 502);
}
if ($parsed_result['response'] === 'SUCCESS' && $parsed_result['errorReason'] === null) {
if (isset($parsed_result['inspectionResult'])
&& $parsed_result['inspectionResult']['webServiceSuccess'] === true
&& is_array($parsed_result['inspectionResult']['beneficiaryNames'])) {
return $this->withTextReady($res, $parsed_result['inspectionResult']['beneficiaryNames'][0]);
}
}
return $this->withTextReady($res, 'false');
}
// queryIDnoCD
// testServiceStatus
}
......@@ -45,8 +45,8 @@ class BaseApp
public static function coreResponseData($success)
{
return [
'success' => $success === true,
'timestamp' => date('c')
// 'success' => $success === true,
// 'timestamp' => date('c')
];
}
......
......@@ -37,7 +37,7 @@ class Client
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
if ($this->_debug === true) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}
......
<?php
$app->get('/queryID/{identity}', '\Gr\Gov\Minedu\Osteam\Slim\App:queryID')
$app->get('/queryID', '\Gr\Gov\Minedu\Osteam\Slim\App:queryID')
->setName('queryID');
$app->get('/queryIDnoCD', '\Gr\Gov\Minedu\Osteam\Slim\App:queryIDnoCD')
->setName('queryIDnoCD');
$app->get('/student/{identity}', '\Gr\Gov\Minedu\Osteam\Slim\App:student')
->setName('student');
......
......@@ -21,6 +21,8 @@ return [
'password' => 'password-for-endpoint',
'secure_endpoint_username' => 'username-for-this-wrapper',
'secure_endpoint_password' => 'password-for-this-wrapper',
'ws_endpoint_academic_id' => 'url-for-endpoint',
'ws_endpoint_amka' => 'url-for-endpoint',
]
],
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment