Browse Source

updated relais api calls to use guzzle instead of drupal_http_request

9.x-1.0
ppound 3 years ago
parent
commit
bf5b913a12
  1. 71
      includes/relais.inc
  2. 60
      src/Form/RoblibIllLoanForm.php

71
includes/relais.inc

@ -28,29 +28,38 @@ function upei_roblib_ill_get_pub_type($genre) {
function upei_roblib_ill_add_request($form_state, $aid) { function upei_roblib_ill_add_request($form_state, $aid) {
module_load_include('inc', 'upei_roblib_ill', 'includes/db'); module_load_include('inc', 'upei_roblib_ill', 'includes/db');
module_load_include('inc', 'upei_roblib_ill', 'includes/utilities'); module_load_include('inc', 'upei_roblib_ill', 'includes/utilities');
$url = variable_get('upei_roblib_ill_add_url', 'https://caul-cbua.relais-host.com/portal-service/request/add'); $config = \Drupal::config('upei_roblib_ill.settings');
$url = $config->get('ill_add_url');
$relais_arr = upei_roblib_ill_build_relais_arr($form_state); $relais_arr = upei_roblib_ill_build_relais_arr($form_state);
$relais_json = json_encode($relais_arr); $relais_json = json_encode($relais_arr);
$options = [ $options = [
'method' => 'POST', 'body' => $relais_json,
'data' => $relais_json,
'timeout' => 15, 'timeout' => 15,
'headers' => ['Content-Type' => 'application/json'], 'headers' => ['Content-Type' => 'application/json'],
]; ];
if (!isset($aid) || is_array($aid)) { if (!isset($aid) || is_array($aid)) {
upei_roblib_ill_log_request($relais_arr, $aid); //upei_roblib_ill_log_request($relais_arr, $aid);
drupal_set_message(t('Error retrieving authentication token, @message', ['@message' => $aid['Problem']['Message']]), 'error'); drupal_set_message(t('Error retrieving authentication token, @message', ['@message' => $aid['Problem']['Message']]), 'error');
return ['ConfirmMessage' => t('There was an error processing your request, @msg', ['@msg' => $aid['Problem']['Message']])]; return ['ConfirmMessage' => t('There was an error processing your request, @msg', ['@msg' => $aid['Problem']['Message']])];
} }
$result = drupal_http_request($url . '?aid=' . $aid, $options); //$result = drupal_http_request($url . '?aid=' . $aid, $options);
try {
$response = \Drupal::httpClient()->post($url . '?aid=' . $aid, $options);
$data = (string) $response->getBody();
}
catch (Exception $e) {
return ['ConfirmMessage' => 'Error Communicating with Relais, ' . $e->getMessage()];
}
$response_arr = json_decode($response->getBody(),TRUE);
$status_code = $response->getStatusCode();
module_load_include('inc', 'upei_roblib_ill', 'includes/db'); module_load_include('inc', 'upei_roblib_ill', 'includes/db');
if (!isset($result->data)) { if (!isset($response_date)) {
return ['ConfirmMessage' => 'Error Communicating with Relais, no data returned.']; return ['ConfirmMessage' => 'Error Communicating with Relais, no data returned.'];
} }
$response_json = $result->data; $response_json = $result->data;
$response_arr = json_decode($response_json, TRUE); //$response_arr = json_decode($response_json, TRUE);
$response_arr['ConfirmMessage'] = isset($response_arr['Problem']['ErrorMessage']) ? $response_arr['Problem']['ErrorMessage'] : $response_arr['ConfirmMessage']; $response_arr['ConfirmMessage'] = isset($response_arr['Problem']['ErrorMessage']) ? $response_arr['Problem']['ErrorMessage'] : $response_arr['ConfirmMessage'];
upei_roblib_ill_log_request($relais_arr, $response_arr); //upei_roblib_ill_log_request($relais_arr, $response_arr);
return $response_arr; return $response_arr;
} }
@ -61,21 +70,25 @@ function upei_roblib_ill_add_request($form_state, $aid) {
*/ */
function upei_roblib_ill_build_relais_arr($form_state) { function upei_roblib_ill_build_relais_arr($form_state) {
module_load_include('inc', 'upei_roblib_ill', 'includes/utilities'); module_load_include('inc', 'upei_roblib_ill', 'includes/utilities');
$storage = $form_state->getStorage();
$ill_request = $storage['request'];
$values = $form_state->getValues();
$config = \Drupal::config('upei_roblib_ill.settings');
$relais_arr = [ $relais_arr = [
"SupplyingLibrarySymbol" => variable_get('upei_roblib_ill_library_symbol', 'PCU'), "SupplyingLibrarySymbol" => $config->get('ill_library_symbol'),
]; ];
$pub_type = upei_roblib_ill_get_pub_type($form_state['storage']['upei_roblib_ill_request_form']['Genre']); $pub_type = upei_roblib_ill_get_pub_type($ill_request['Genre']);
$relais_arr['BibliographicInfo'] = upei_roblib_ill_clean_array($form_state['storage']['upei_roblib_ill_request_form']); $relais_arr['BibliographicInfo'] = upei_roblib_ill_clean_array($ill_request);
$relais_arr['DeliveryAddress'] = upei_roblib_ill_clean_array($form_state['storage']['upei_roblib_ill_auth_form']); $relais_arr['DeliveryAddress'] = upei_roblib_ill_clean_array($values);
if ($pub_type != 'B') { if ($pub_type != 'B') {
// this is not a book but is a journal or chapter we will send electronically and as copy // this is not a book but is a journal or chapter we will send electronically and as copy
$relais_arr['ElectronicDelivery']['DeliveryAddress'] = $relais_arr['ElectronicDelivery']['MessagingAddress'] = $relais_arr['ElectronicDelivery']['DeliveryAddress'] = $relais_arr['ElectronicDelivery']['MessagingAddress'] =
$relais_arr['DeliveryAddress']['DeliveryAddress']; $relais_arr['DeliveryAddress']['DeliveryAddress'];
} }
$request_info = upei_roblib_ill_request_info_array($relais_arr['BibliographicInfo'], $form_state['values']['notes']); $request_info = upei_roblib_ill_request_info_array($relais_arr['BibliographicInfo'], $ill_request['notes']);
$relais_arr['RequestInfo'] = $request_info; $relais_arr['RequestInfo'] = $request_info;
$relais_arr['PublisherInfo']['PublicationDate'] = isset($form_state['storage']['upei_roblib_ill_request_form']['Date']) ? $relais_arr['PublisherInfo']['PublicationDate'] = isset($ill_request['Date']) ?
$form_state['storage']['upei_roblib_ill_request_form']['Date'] : ''; $ill_request['Date'] : '';
$relais_arr['PublisherInfo']['PublicationType'] = $pub_type; $relais_arr['PublisherInfo']['PublicationType'] = $pub_type;
return $relais_arr; return $relais_arr;
} }
@ -92,26 +105,36 @@ function upei_roblib_ill_build_relais_arr($form_state) {
* authentication fails * authentication fails
*/ */
function upei_roblib_ill_authenticate($barcode, $surname) { function upei_roblib_ill_authenticate($barcode, $surname) {
$url = variable_get('upei_roblib_ill_auth_url', 'https://caul-cbua.relais-host.com/portal-service/user/authentication'); //$url = variable_get('upei_roblib_ill_auth_url', 'https://caul-cbua.relais-host.com/portal-service/user/authentication');
$config = \Drupal::config('upei_roblib_ill.settings');
$url = $config->get('ill_auth_url');
$json_arr = []; $json_arr = [];
$json_arr['ApiKey'] = variable_get('upei_roblib_ill_relais_key'); $json_arr['ApiKey'] = $config->get('ill_relais_key');
$json_arr['UserGroup'] = 'patron'; $json_arr['UserGroup'] = 'patron';
$json_arr['LibrarySymbol'] = variable_get('upei_roblib_ill_library_symbol'); $json_arr['LibrarySymbol'] = $config->get('ill_library_symbol');
$json_arr['PatronId'] = $barcode; $json_arr['PatronId'] = $barcode;
$request_json = json_encode($json_arr); $request_json = json_encode($json_arr);
$options = [ $options = [
'method' => 'POST', 'body' => $request_json,
'data' => $request_json,
'timeout' => 15, 'timeout' => 15,
'headers' => ['Content-Type' => 'application/json'], 'headers' => ['Content-Type' => 'application/json'],
]; ];
$result = drupal_http_request($url, $options); try {
$response = \Drupal::httpClient()->post($url, $options);
}
catch (Exception $e) {
$response_data['Problem']['Message'] = $e->getMessage();
return $response_data;
}
/*$result = drupal_http_request($url, $options);
if ($result->code != '200') { if ($result->code != '200') {
$response_data['Problem']['Message'] = $result->error; $response_data['Problem']['Message'] = $result->error;
return $response_data; return $response_data;
} }*/
$response_data = json_decode($result->data, TRUE); $response_data = json_decode($response->getBody(),TRUE);
if ($result->code == '200') { $status_code = $response->getStatusCode();
if ($status_code == '200') {
if (isset($response_data['Problem']) || strtolower($response_data['LastName']) !== strtolower($surname)) { if (isset($response_data['Problem']) || strtolower($response_data['LastName']) !== strtolower($surname)) {
$err_message = isset($response_data['Problem']['Message']) ? $response_data['Problem']['Message'] : ''; $err_message = isset($response_data['Problem']['Message']) ? $response_data['Problem']['Message'] : '';
$response_data['Problem']['Message'] = upei_roblib_ill_build_err_msg($err_message); $response_data['Problem']['Message'] = upei_roblib_ill_build_err_msg($err_message);

60
src/Form/RoblibIllLoanForm.php

@ -45,9 +45,50 @@ class RoblibIllLoanForm extends FormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateForm(array &$form, FormStateInterface $form_state) { public function validateForm(array &$form, FormStateInterface $form_state) {
module_load_include('inc', 'upei_roblib_ill', 'includes/relais');
$values = $form_state->getValues();
if ($form_state->get('step') == 2) {
$is_valid_email = \Drupal::service('email.validator')->isValid($values['DeliveryAddress']);
if (!$is_valid_email) {
$form_state->setErrorByName('DeliveryAddress', t('Your email address appears to be invalid.'));
}
if (!$values['certify']) {
$form_state->setErrorByName('certify', t('Please certify that this is item is being sought for a fair dealing purpose'));
}
$campus_id = $this->ill_form_message_id($values['campus_id']);
$aid = upei_roblib_ill_authenticate($campus_id, $values['Surname']);
if (is_array($aid) && isset($aid['Problem']['Message'])) {
$form_state->setErrorByName('Surname', $aid['Problem']['Message']);
$form_state->setErrorByName('campus_id', '');
// Log invalid requests, we need to build the full array here to log it, even though we won't send it yet as auth has failed.
//$form_state->set('upei_roblib_ill_auth_form_storage') = $form_state->getValues();
module_load_include('inc', 'upei_roblib_ill', 'includes/db');
$arr = upei_roblib_ill_build_relais_arr($form_state);
upei_roblib_ill_log_request($arr, $aid);
}
else {
$form_state->set('aid', $aid);
}
}
//if (empty($form_state->get('step')) && $form_state['triggering_element']['#value'] !== 'Lookup DOI' && empty($form_state->get('Title'))) {
// form_set_error('Title', t('Journal/Book Title is required.'));
//}
} }
/**
* @param string $id
* The users campus id
*
* @return mixed
* the campus id with leading zeros and leading/trailing whitespace removed.
*/
function ill_form_message_id($id) {
$id = ltrim($id, '0');
return trim($id);
}
/** /**
* @param array $form * @param array $form
* An associative array containing the structure of the form. * An associative array containing the structure of the form.
@ -57,6 +98,7 @@ class RoblibIllLoanForm extends FormBase {
public function submitStepOne(array &$form, FormStateInterface $form_state) { public function submitStepOne(array &$form, FormStateInterface $form_state) {
$form_state $form_state
->set('step', 2) ->set('step', 2)
->set('request', $form_state->getValues())
->setRebuild(TRUE); ->setRebuild(TRUE);
} }
@ -65,19 +107,19 @@ class RoblibIllLoanForm extends FormBase {
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
//process the form //process the form
//$response = upei_roblib_ill_add_request($form_state, $form_state['storage']['aid']); $response = upei_roblib_ill_add_request($form_state, $form_state->get('aid'));
//$error = isset($response['RequestNumber']) ? 'FALSE' : 'TRUE'; $error = isset($response['RequestNumber']) ? 'FALSE' : 'TRUE';
$form_state->setRedirect('roblib_ill.loan_form_finished'); $form_state->setRedirect('roblib_ill.loan_form_finished');
/*$form_state->set['redirect'] = [ /* $form_state->set['redirect'] = [
'/upei/roblib/ill/finished', '/upei/roblib/ill/finished',
[ [
'query' => [ 'query' => [
'message' => 'test message',//$response['ConfirmMessage'], 'message' => 'test message',//$response['ConfirmMessage'],
'email' => 'test email',//$form_state['values']['DeliveryAddress'], 'email' => 'test email',//$form_state['values']['DeliveryAddress'],
'error' => 'no errors',//$error, 'error' => 'no errors',//$error,
], ],
], ],
];*/ ]; */
} }
/** /**

Loading…
Cancel
Save