Skip to content

Commit e8b1a68

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

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

classes/local/step/curl_trait.php

Lines changed: 22 additions & 2 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,10 @@ 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', '', get_string('connector_curl:counterfield_help', 'tool_dataflows'));
159164
}
160165

161166
/**
@@ -302,6 +307,23 @@ public function execute($input = null) {
302307
if (!empty($file)) {
303308
fclose($file);
304309
}
310+
311+
if (!empty($config->counterfield)) {
312+
$counterfield = $config->counterfield;
313+
$varresult = json_decode($result);
314+
foreach ($varresult as $key => $value) {
315+
$name = "{$counterfield}.{$key}";
316+
if ($variables->evaluate($name)) {
317+
$variables->set($name, $value);
318+
$this->stepdef->set_config_by_name($name, $value);
319+
if (!$this->is_dry_run()) {
320+
$this->stepdef->save();
321+
}
322+
} else {
323+
debugging($name . ': variable could not be evaluated', DEBUG_DEVELOPER);
324+
}
325+
}
326+
}
305327

306328
$info = $curl->get_info();
307329
// Stores response to be reusable by other steps.
@@ -315,8 +337,6 @@ public function execute($input = null) {
315337
throw new \moodle_exception($httpcode . ':' . $result);
316338
}
317339

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.
320340
$variables->set('response', (object) [
321341
'result' => $result,
322342
'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)