Skip to content

Commit ad19d2a

Browse files
committed
Curl step: Expose fields from response to use in next steps.
1 parent 3ad5945 commit ad19d2a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

classes/local/step/curl_trait.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public static function form_define_fields(): array {
8181
'rawpostdata' => ['type' => PARAM_RAW],
8282
'sideeffects' => ['type' => PARAM_RAW],
8383
'timeout' => ['type' => PARAM_INT],
84+
'counterfield' => ['type' => PARAM_TEXT],
8485
];
8586
}
8687

@@ -156,6 +157,11 @@ public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
156157

157158
$mform->addElement('text', 'config_timeout', get_string('connector_curl:timeout', 'tool_dataflows'));
158159
$mform->addHelpButton('config_timeout', 'connector_curl:timeout', 'tool_dataflows');
160+
161+
// Counter field.
162+
$mform->addElement('text', 'config_counterfield', get_string('connector_curl:counterfield', 'tool_dataflows'));
163+
$mform->addElement('static', 'config_counterfield_help', '',
164+
get_string('connector_curl:counterfield_help', 'tool_dataflows'));
159165
}
160166

161167
/**
@@ -303,6 +309,23 @@ public function execute($input = null) {
303309
fclose($file);
304310
}
305311

312+
if (!empty($config->counterfield)) {
313+
$counterfield = $config->counterfield;
314+
$varresult = json_decode($result);
315+
foreach ($varresult as $key => $value) {
316+
$name = "{$counterfield}.{$key}";
317+
if ($variables->evaluate($name)) {
318+
$variables->set($name, $value);
319+
$this->stepdef->set_config_by_name($name, $value);
320+
if (!$this->is_dry_run()) {
321+
$this->stepdef->save();
322+
}
323+
} else {
324+
debugging($name . ': variable could not be evaluated', DEBUG_DEVELOPER);
325+
}
326+
}
327+
}
328+
306329
$info = $curl->get_info();
307330
// Stores response to be reusable by other steps.
308331
// TODO : Once set_var api is refactored add response.
@@ -312,11 +335,9 @@ public function execute($input = null) {
312335
$errno = $curl->get_errno();
313336

314337
if (($httpcode >= self::HTTP_ERROR || $errno == CURLE_OPERATION_TIMEDOUT)) {
315-
throw new \moodle_exception($httpcode . ':' . $result);
338+
debugging($httpcode . ':' . $result);
316339
}
317340

318-
// TODO: It would be good to define and list any fixed but exposed
319-
// fields which the user can use and map to on the edit page.
320341
$variables->set('response', (object) [
321342
'result' => $result,
322343
'info' => (object) $info,

lang/en/tool_dataflows.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@
492492
$string['connector_curl:output_response_result'] = 'Returns a string that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.';
493493
$string['connector_curl:header_format'] = '<header>:<value>';
494494
$string['connector_curl:headers_invalid'] = 'Curl connector headers are invalid.';
495+
$string['connector_curl:counterfield'] = 'Counter field';
496+
$string['connector_curl:counterfield_help'] = 'Field used to iterates value from a curl request, if the data is not nested otherwise using json_reader for it. \n
497+
Example: access_token: ${{ record.access_token }} ';
495498

496499
// GPG.
497500
$string['gpg:userid'] = 'Key owner (user ID)';

0 commit comments

Comments
 (0)