1515// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616
1717/**
18- * HTTP Post action step class.
18+ * HTTP action step class.
1919 *
2020 * @package tool_trigger
2121 * @copyright Matt Porritt <[email protected] > 2525namespace tool_trigger \steps \actions ;
2626
2727/**
28- * HTTP Post action step class.
28+ * HTTP action step class.
2929 *
3030 * @package tool_trigger
3131 * @copyright Matt Porritt <[email protected] > @@ -35,9 +35,22 @@ class http_post_action_step extends base_action_step {
3535
3636 use \tool_trigger \helper \datafield_manager;
3737
38+ /**
39+ * Supported HTTP methods.
40+ */
41+ const SUPPORTED_HTTP_METHODS = [
42+ 'POST ' => 'POST ' ,
43+ 'GET ' => 'GET ' ,
44+ 'PUT ' => 'PUT ' ,
45+ 'DELETE ' => 'DELETE ' ,
46+ 'PATCH ' => 'PATCH ' ,
47+ ];
48+
3849 protected $ url ;
50+ protected $ httpmethod ;
3951 protected $ headers ;
4052 protected $ params ;
53+ private $ httphandler = null ;
4154
4255 /**
4356 * The fields supplied by this step.
@@ -52,6 +65,7 @@ class http_post_action_step extends base_action_step {
5265
5366 protected function init () {
5467 $ this ->url = $ this ->data ['url ' ];
68+ $ this ->httpmethod = !empty ($ this ->data ['httpmethod ' ]) ? $ this ->data ['httpmethod ' ] : 'POST ' ;
5569 $ this ->headers = $ this ->data ['httpheaders ' ];
5670 $ this ->params = $ this ->data ['httpparams ' ];
5771 $ this ->jsonencode = $ this ->data ['jsonencode ' ];
@@ -81,8 +95,6 @@ public static function get_step_desc() {
8195 return get_string ('httppostactionstepdesc ' , 'tool_trigger ' );
8296 }
8397
84- private $ httphandler = null ;
85-
8698 /**
8799 * Kinda hacky... unit testing requires us to specify a different http handler for guzzle to use.
88100 * That's really the only reason we need this method!
@@ -143,7 +155,7 @@ public function execute($step, $trigger, $event, $stepresults) {
143155 $ params = json_encode ($ output );
144156 }
145157
146- $ request = new \GuzzleHttp \Psr7 \Request (' POST ' , $ url , $ headers , $ params );
158+ $ request = new \GuzzleHttp \Psr7 \Request ($ this -> httpmethod , $ url , $ headers , $ params );
147159 $ client = $ this ->get_http_client ();
148160
149161 try {
@@ -159,7 +171,9 @@ public function execute($step, $trigger, $event, $stepresults) {
159171 if ($ response ->getStatusCode () != $ this ->expectedresponse ) {
160172 // If we weren't expecting this response, throw an exception.
161173 // The error will be caught and rerun.
162- throw new \invalid_response_exception ("HTTP Response code expected was {$ this ->expectedresponse }, received {$ response ->getStatusCode ()}" );
174+ throw new \invalid_response_exception (
175+ "HTTP Response code expected was {$ this ->expectedresponse }, received {$ response ->getStatusCode ()}"
176+ );
163177 }
164178
165179 return array (true , $ stepresults );
@@ -180,6 +194,11 @@ public function form_definition_extra($form, $mform, $customdata) {
180194 $ mform ->addRule ('url ' , get_string ('required ' ), 'required ' );
181195 $ mform ->addHelpButton ('url ' , 'httpostactionurl ' , 'tool_trigger ' );
182196
197+ // HTTP method.
198+ $ mform ->addElement ('select ' , 'httpmethod ' , get_string ('httpostmethod ' , 'tool_trigger ' ), self ::SUPPORTED_HTTP_METHODS );
199+ $ mform ->setType ('httpmethod ' , PARAM_TEXT );
200+ $ mform ->addHelpButton ('httpmethod ' , 'httpostmethod ' , 'tool_trigger ' );
201+
183202 // Headers.
184203 $ attributes = array ('cols ' => '50 ' , 'rows ' => '2 ' );
185204 $ mform ->addElement ('textarea ' , 'httpheaders ' , get_string ('httpostactionheaders ' , 'tool_trigger ' ), $ attributes );
0 commit comments