Skip to content

Commit 8978349

Browse files
committed
- (Feature) Added the new HttpRequest Service which allows users to send GET, POST, PUT, and DELETE requests to a specific URL, along with the headers and payload data
1 parent 2a61ec7 commit 8978349

File tree

5 files changed

+178
-2
lines changed

5 files changed

+178
-2
lines changed

PostmasterPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function getName()
1010

1111
public function getVersion()
1212
{
13-
return '0.1.2';
13+
return '0.1.2.4';
1414
}
1515

1616
public function getDeveloper()

bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
'Craft\Plugins\Postmaster\Services\MandrillService',
77
'Craft\Plugins\Postmaster\Services\MailchimpService',
88
'Craft\Plugins\Postmaster\Services\TwilioService',
9+
'Craft\Plugins\Postmaster\Services\HttpRequestService',
910
'Craft\Plugins\Postmaster\Services\TestService',
10-
'Craft\Plugins\Postmaster\Services\PingService',
1111
));
1212

1313
craft()->postmaster->registerParcelTypes(array(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace Craft\Plugins\Postmaster\Services;
3+
4+
use Craft\Postmaster_TransportModel;
5+
use Craft\Plugins\Postmaster\Components\BaseService;
6+
use Craft\Plugins\Postmaster\Responses\TransportResponse;
7+
use \Guzzle\Http\Client;
8+
9+
class HttpRequestService extends BaseService {
10+
11+
public $name = 'Http Request';
12+
13+
public $id = 'http';
14+
15+
public function send(Postmaster_TransportModel $model)
16+
{
17+
$response = new TransportResponse($model);
18+
$headers = $this->settings->headers;
19+
20+
if(!empty($this->settings->url))
21+
{
22+
try
23+
{
24+
$client = new Client();
25+
26+
switch($this->settings->action)
27+
{
28+
case 'get':
29+
$request = $client->get($this->settings->url, $headers);
30+
$query = $request->getQuery();
31+
32+
foreach($this->settings->postVars as $var)
33+
{
34+
$query->add($var['name'], $var['value']);
35+
}
36+
37+
break;
38+
39+
case 'post':
40+
$request = $client->post($this->settings->url, $headers, $this->settings->postVars);
41+
break;
42+
43+
case 'put':
44+
$request = $client->put($this->settings->url, $headers, $this->settings->postVars);
45+
break;
46+
47+
case 'delete':
48+
$request = $client->get($this->settings->url, $headers, $this->settings->postVars);
49+
break;
50+
}
51+
52+
$request->send();
53+
}
54+
catch(\Exception $e)
55+
{
56+
$response->addError($e->getMessage());
57+
$response->setSuccess(false);
58+
}
59+
}
60+
else
61+
{
62+
$response->setSuccess(false);
63+
$response->addError(\Craft::t('The ping url is empty.'));
64+
}
65+
66+
return $response;
67+
}
68+
69+
public function getInputHtml(Array $data = array())
70+
{
71+
return $this->craft()->templates->render('postmaster/services/http/settings', $data);
72+
}
73+
74+
public function getSettingsModelClassName()
75+
{
76+
return '\Craft\Postmaster_HttpRequestServiceSettingsModel';
77+
}
78+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace Craft;
3+
4+
class Postmaster_HttpRequestServiceSettingsModel extends Postmaster_ServiceSettingsModel
5+
{
6+
public function parse(Array $data = array())
7+
{
8+
$parsedVars = array();
9+
10+
foreach($this->postVars as $row => $vars)
11+
{
12+
$vars['value'] = craft()->templates->renderString($vars['value'], $data);
13+
14+
$parsedVars[] = $vars;
15+
}
16+
17+
$this->postVars = $parsedVars;
18+
19+
return $this;
20+
}
21+
22+
protected function defineAttributes()
23+
{
24+
return array(
25+
'url' => array(AttributeType::String),
26+
'action' => array(AttributeType::String, 'default' => 'post'),
27+
'postVars' => array(AttributeType::Mixed, 'default' => array()),
28+
'headers' => array(AttributeType::Mixed, 'default' => array())
29+
);
30+
}
31+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{% import "_includes/forms" as forms %}
2+
3+
<p>{{ "The Http Request Service allows you to send HTTP requests to a specified url. You can choose which HTTP method to use along with the data that is sent with each request."|t }}</p>
4+
5+
{{ forms.textField({
6+
label: "Request Url"|t,
7+
instructions: "The url to send the request."|t,
8+
id: 'http_url',
9+
name: 'settings[serviceSettings][http][url]',
10+
value: settings.url
11+
}) }}
12+
13+
{{ forms.selectField({
14+
label: 'Action'|t,
15+
name: 'settings[serviceSettings][http][action]',
16+
value: settings.action,
17+
id: 'http_action',
18+
options: [{
19+
label: 'GET',
20+
value: 'get'
21+
},{
22+
label: 'POST',
23+
value: 'post'
24+
},{
25+
label: 'PUT',
26+
value: 'put'
27+
},{
28+
label: 'DELETE',
29+
value: 'delete'
30+
}]
31+
}) }}
32+
33+
{{ forms.editableTableField({
34+
label: "HTTP Headers",
35+
instructions: "Enter the names and values of HTTP headers to be included in the request."|t,
36+
name: 'settings[serviceSettings][http][headers]',
37+
id: 'http_headers',
38+
cols: {
39+
name: {
40+
heading: 'Name'|t,
41+
type: 'singleline'
42+
},
43+
value: {
44+
heading: 'Value'|t,
45+
type: 'singleline'
46+
}
47+
},
48+
rows: settings.headers
49+
}) }}
50+
51+
{{ forms.editableTableField({
52+
label: "Request Variables",
53+
instructions: "Enter the names and values of variables to be included in the request payload."|t,
54+
name: 'settings[serviceSettings][http][postVars]',
55+
id: 'http_post_vars',
56+
cols: {
57+
name: {
58+
heading: 'Name'|t,
59+
type: 'singleline'
60+
},
61+
value: {
62+
heading: 'Value'|t,
63+
type: 'singleline'
64+
}
65+
},
66+
rows: settings.postVars
67+
}) }}

0 commit comments

Comments
 (0)