Skip to content

Curl step: Expose fields from response to use in next steps. #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: MOODLE_405_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions classes/local/step/curl_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static function form_define_fields(): array {
'rawpostdata' => ['type' => PARAM_RAW],
'sideeffects' => ['type' => PARAM_RAW],
'timeout' => ['type' => PARAM_INT],
'counterfield' => ['type' => PARAM_TEXT],
];
}

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

$mform->addElement('text', 'config_timeout', get_string('connector_curl:timeout', 'tool_dataflows'));
$mform->addHelpButton('config_timeout', 'connector_curl:timeout', 'tool_dataflows');

// Counter field.
$mform->addElement('text', 'config_counterfield', get_string('connector_curl:counterfield', 'tool_dataflows'));
$mform->addElement('static', 'config_counterfield_help', '',
get_string('connector_curl:counterfield_help', 'tool_dataflows'));
}

/**
Expand Down Expand Up @@ -303,6 +309,23 @@ public function execute($input = null) {
fclose($file);
}

if (!empty($config->counterfield)) {
$counterfield = $config->counterfield;
$varresult = json_decode($result);
foreach ($varresult as $key => $value) {
$name = "{$counterfield}.{$key}";
if ($variables->evaluate($name)) {
$variables->set($name, $value);
$this->stepdef->set_config_by_name($name, $value);
if (!$this->is_dry_run()) {
$this->stepdef->save();
}
} else {
debugging($name . ': variable could not be evaluated', DEBUG_DEVELOPER);
}
}
}

$info = $curl->get_info();
// Stores response to be reusable by other steps.
// TODO : Once set_var api is refactored add response.
Expand All @@ -312,11 +335,9 @@ public function execute($input = null) {
$errno = $curl->get_errno();

if (($httpcode >= self::HTTP_ERROR || $errno == CURLE_OPERATION_TIMEDOUT)) {
throw new \moodle_exception($httpcode . ':' . $result);
debugging($httpcode . ':' . $result);
}

// TODO: It would be good to define and list any fixed but exposed
// fields which the user can use and map to on the edit page.
$variables->set('response', (object) [
'result' => $result,
'info' => (object) $info,
Expand Down
3 changes: 3 additions & 0 deletions lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@
$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.';
$string['connector_curl:header_format'] = '<header>:<value>';
$string['connector_curl:headers_invalid'] = 'Curl connector headers are invalid.';
$string['connector_curl:counterfield'] = 'Counter field';
$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
Example: access_token: ${{ record.access_token }} ';

// GPG.
$string['gpg:userid'] = 'Key owner (user ID)';
Expand Down
Loading