-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from catalyst/issue-209-MOODLE_35_STABLE
issue #209: allow different HTTP methods for http_post_action_step
- Loading branch information
Showing
4 changed files
with
89 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* HTTP Post action step class. | ||
* HTTP action step class. | ||
* | ||
* @package tool_trigger | ||
* @copyright Matt Porritt <[email protected]> | ||
|
@@ -25,7 +25,7 @@ | |
namespace tool_trigger\steps\actions; | ||
|
||
/** | ||
* HTTP Post action step class. | ||
* HTTP action step class. | ||
* | ||
* @package tool_trigger | ||
* @copyright Matt Porritt <[email protected]> | ||
|
@@ -35,9 +35,22 @@ class http_post_action_step extends base_action_step { | |
|
||
use \tool_trigger\helper\datafield_manager; | ||
|
||
/** | ||
* Supported HTTP methods. | ||
*/ | ||
const SUPPORTED_HTTP_METHODS = [ | ||
'POST' => 'POST', | ||
'GET' => 'GET', | ||
'PUT' => 'PUT', | ||
'DELETE' => 'DELETE', | ||
'PATCH' => 'PATCH', | ||
]; | ||
|
||
protected $url; | ||
protected $httpmethod; | ||
protected $headers; | ||
protected $params; | ||
private $httphandler = null; | ||
|
||
/** | ||
* The fields supplied by this step. | ||
|
@@ -52,6 +65,7 @@ class http_post_action_step extends base_action_step { | |
|
||
protected function init() { | ||
$this->url = $this->data['url']; | ||
$this->httpmethod = !empty($this->data['httpmethod']) ? $this->data['httpmethod'] : 'POST'; | ||
$this->headers = $this->data['httpheaders']; | ||
$this->params = $this->data['httpparams']; | ||
$this->jsonencode = $this->data['jsonencode']; | ||
|
@@ -81,8 +95,6 @@ public static function get_step_desc() { | |
return get_string('httppostactionstepdesc', 'tool_trigger'); | ||
} | ||
|
||
private $httphandler = null; | ||
|
||
/** | ||
* Kinda hacky... unit testing requires us to specify a different http handler for guzzle to use. | ||
* That's really the only reason we need this method! | ||
|
@@ -143,7 +155,7 @@ public function execute($step, $trigger, $event, $stepresults) { | |
$params = json_encode($output); | ||
} | ||
|
||
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, $params); | ||
$request = new \GuzzleHttp\Psr7\Request($this->httpmethod, $url, $headers, $params); | ||
$client = $this->get_http_client(); | ||
|
||
try { | ||
|
@@ -159,7 +171,9 @@ public function execute($step, $trigger, $event, $stepresults) { | |
if ($response->getStatusCode() != $this->expectedresponse) { | ||
// If we weren't expecting this response, throw an exception. | ||
// The error will be caught and rerun. | ||
throw new \invalid_response_exception("HTTP Response code expected was {$this->expectedresponse}, received {$response->getStatusCode()}"); | ||
throw new \invalid_response_exception( | ||
"HTTP Response code expected was {$this->expectedresponse}, received {$response->getStatusCode()}" | ||
); | ||
} | ||
|
||
return array(true, $stepresults); | ||
|
@@ -180,6 +194,11 @@ public function form_definition_extra($form, $mform, $customdata) { | |
$mform->addRule('url', get_string('required'), 'required'); | ||
$mform->addHelpButton('url', 'httpostactionurl', 'tool_trigger'); | ||
|
||
// HTTP method. | ||
$mform->addElement('select', 'httpmethod', get_string ('httpostmethod', 'tool_trigger'), self::SUPPORTED_HTTP_METHODS); | ||
$mform->setType('httpmethod', PARAM_TEXT); | ||
$mform->addHelpButton('httpmethod', 'httpostmethod', 'tool_trigger'); | ||
|
||
// Headers. | ||
$attributes = array('cols' => '50', 'rows' => '2'); | ||
$mform->addElement('textarea', 'httpheaders', get_string ('httpostactionheaders', 'tool_trigger'), $attributes); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters