Skip to content
Snippets Groups Projects
Commit ca4164b5 authored by João Eduardo Ramos Costa's avatar João Eduardo Ramos Costa
Browse files

Merge branch '3389901-allow-select-request' into '2.0.x'

Issue #3389901 - Provide option to select request method.

See merge request !6
parents b9c0a484 51a4dd9b
No related branches found
No related tags found
No related merge requests found
Pipeline #95614 passed
......@@ -27,6 +27,7 @@ display:
type: views_query
options:
json_file: '[site:url]modules/contrib/views_json_source/data/sample/example1.json'
request_method: 'get'
row_apath: data/nodes
show_errors: 1
exposed_form:
......@@ -196,6 +197,7 @@ display:
type: views_query
options:
json_file: '[site:url]modules/contrib/views_json_source/data/sample/example1.json'
request_method: 'get'
row_apath: data/nodes
headers: ''
show_errors: 1
......@@ -225,6 +227,7 @@ display:
type: views_query
options:
json_file: '/modules/contrib/views_json_source/data/sample/example2.json'
request_method: 'get'
row_apath: data/%/contents
headers: ''
show_errors: 1
......@@ -278,6 +281,7 @@ display:
type: views_query
options:
json_file: '/modules/contrib/views_json_source/data/sample/example3.json'
request_method: 'get'
row_apath: nid=2/related
headers: ''
show_errors: 1
......
......@@ -283,25 +283,19 @@ class ViewsJsonQuery extends QueryPluginBase {
if (!$ret && $this->options['show_errors']) {
if (version_compare(phpversion(), '5.3.0', '>=')) {
$tmp = [
JSON_ERROR_NONE =>
$this->t('No error has occurred'),
JSON_ERROR_DEPTH =>
$this->t('The maximum stack depth has been exceeded'),
JSON_ERROR_STATE_MISMATCH =>
$this->t('Invalid or malformed JSON'),
JSON_ERROR_CTRL_CHAR =>
$this->t('Control character error, possibly incorrectly encoded'),
JSON_ERROR_SYNTAX =>
$this->t('Syntax error'),
JSON_ERROR_UTF8 =>
$this->t('Malformed UTF-8 characters, possibly incorrectly encoded'),
JSON_ERROR_NONE => $this->t('No error has occurred'),
JSON_ERROR_DEPTH => $this->t('The maximum stack depth has been exceeded'),
JSON_ERROR_STATE_MISMATCH => $this->t('Invalid or malformed JSON'),
JSON_ERROR_CTRL_CHAR => $this->t('Control character error, possibly incorrectly encoded'),
JSON_ERROR_SYNTAX => $this->t('Syntax error'),
JSON_ERROR_UTF8 => $this->t('Malformed UTF-8 characters, possibly incorrectly encoded'),
];
$msg = $tmp[json_last_error()] . ' - ' . $this->options['json_file'];
$this->messenger->addMessage($msg, 'error');
}
else {
$this->messenger->addMessage($this->t(
'Views JSON Backend: Parse error') .
'Views JSON Backend: Parse error') .
' - ' . $this->options['json_file'], 'error'
);
}
......@@ -548,6 +542,8 @@ class ViewsJsonQuery extends QueryPluginBase {
$options['json_file'] = ['default' => ''];
$options['row_apath'] = ['default' => ''];
$options['headers'] = ['default' => ''];
$options['request_method'] = ['default' => 'get'];
$options['request_body'] = ['default' => ''];
$options['single_payload'] = ['default' => ''];
$options['show_errors'] = ['default' => TRUE];
......@@ -579,6 +575,29 @@ class ViewsJsonQuery extends QueryPluginBase {
'#description' => $this->t("Headers to be passed for the REST call.<br />Pass the headers as JSON string. Ex:<br /><pre>{&quot;Authorization&quot;:&quot;Basic xxxxx&quot;,&quot;Content-Type&quot;:&quot;application/json&quot;}</pre><br />.Here we are passing 2 headers for making the REST API call."),
'#required' => FALSE,
];
$form['request_method'] = [
'#type' => 'select',
'#title' => $this->t('Request method'),
'#default_value' => $this->options['request_method'],
'#options' => [
'get' => $this->t('GET'),
'post' => $this->t('POST'),
],
'#description' => $this->t('The request method to the REST call.'),
];
$form['request_body'] = [
'#type' => 'textarea',
'#title' => $this->t('Request body'),
'#default_value' => $this->options['request_body'],
'#states' => [
'visible' => [
'select[name="query[options][request_method]"]' => [
'value' => 'post',
],
],
],
'#description' => $this->t('The POST request body to the REST call.<br/>Pass the form values as JSON string. Ex: <br/><pre>[{&quot;name&quot;:&quot;item_key&quot;,&quot;contents&quot;:&quot;item value&quot;,&quot;headers&quot;:{&quot;Content-type&quot;:&quot;application/json&quot;}}]</pre> See <a href="https://docs.guzzlephp.org/en/stable/request-options.html#multipart" target="_blank">the documentation for GuzzleHttp multipart request options</a>.'),
];
$form['single_payload'] = [
'#type' => 'checkbox',
'#title' => $this->t('Response contain single node.'),
......@@ -723,7 +742,24 @@ class ViewsJsonQuery extends QueryPluginBase {
* The request response.
*/
private function getRequestResponse(string $uri, array $headers = []) {
return $this->httpClient->get($uri, ['headers' => $headers]);
$this->options['request_method'] = $this->options['request_method'] ?? 'get';
switch ($this->options['request_method']) {
case 'post':
$request_options = [];
$request_options['headers'] = $headers;
if (!empty($this->options['request_body'])) {
$request_options['multipart'] = json_decode($this->options['request_body'], TRUE);
}
$result = $this->httpClient->post($uri, $request_options);
break;
default:
$result = $this->httpClient->get($uri, ['headers' => $headers]);
break;
}
return $result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment