Skip to content

Commit 40f7134

Browse files
committed
- (Bug Fix) Fixed an issue with the HttpRequestService not sending a DELETE request, was sending a GET instead
- (Feature) Added ability to send file uploads with POST requests
1 parent ab69bcd commit 40f7134

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

core/services/HttpRequestService.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Craft\Postmaster_TransportModel;
66
use Craft\Plugins\Postmaster\Components\BaseService;
77
use Guzzle\Http\Client;
8+
use Guzzle\Http\Exception\ClientErrorResponseException;
89

910
class HttpRequestService extends BaseService {
1011

@@ -42,25 +43,50 @@ public function send(Postmaster_TransportModel $model)
4243
break;
4344

4445
case 'post':
45-
$request = $client->post($this->settings->url, $headers, $this->settings->postVars);
46+
$request = $client->post($this->settings->url, $this->settings->getHeaders(), $this->settings->getRequestVars());
4647
break;
4748

4849
case 'put':
49-
$request = $client->put($this->settings->url, $headers, $this->settings->postVars);
50+
$request = $client->put($this->settings->url, $this->settings->getHeaders(), $this->settings->getRequestVars());
5051
break;
5152

5253
case 'delete':
53-
$request = $client->get($this->settings->url, $headers, $this->settings->postVars);
54+
$request = $client->delete($this->settings->url, $this->settings->getHeaders());
5455
break;
5556
}
5657

57-
$request->send();
58+
$response = $request->send();
59+
60+
$model->addData('responseString', (string) $response->getBody());
61+
$model->addData('responseJson', json_decode($model->getData('responseString')));
5862

5963
return $this->success($model);
6064
}
65+
catch(ClientErrorResponseException $e)
66+
{
67+
$response = (string) $e->getResponse()->getBody();
68+
$json = json_decode($response);
69+
70+
if(is_object($json) && isset($json->errors))
71+
{
72+
if(!is_array($json->errors))
73+
{
74+
$json->errors = (array) $json->errors;
75+
}
76+
77+
return $this->failed($model, 400, $json->errors);
78+
}
79+
else
80+
{
81+
return $this->failed($model, 400, array($response));
82+
}
83+
84+
}
6185
catch(\Exception $e)
6286
{
63-
return $this->failed($model, 400, $e->getMessage());
87+
$error = $e->getMessage();
88+
89+
return $this->failed($model, 400, !is_array($error) ? array($error) : $error);
6490
}
6591
}
6692

models/Postmaster_HttpRequestServiceSettingsModel.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,72 @@ public function parse(Array $data = array())
99

1010
foreach($this->postVars as $row => $vars)
1111
{
12-
$vars['value'] = craft()->templates->renderString($vars['value'], $data);
12+
$vars['value'] = trim(craft()->templates->renderString($vars['value'], $data));
1313

1414
$parsedVars[] = $vars;
1515
}
1616

1717
$this->postVars = $parsedVars;
18+
19+
$parsedFiles = array();
20+
21+
foreach($this->files as $row => $vars)
22+
{
23+
$vars['value'] = trim(craft()->templates->renderString($vars['value'], $data));
24+
25+
$parsedFiles[] = $vars;
26+
}
27+
28+
$this->files = $parsedFiles;
29+
30+
parent::parse($data);
1831

1932
return $this;
2033
}
2134

35+
public function getHeaders()
36+
{
37+
$headers = array();
38+
39+
foreach($this->headers as $header)
40+
{
41+
$headers[$header['name']] = $header['value'];
42+
}
43+
44+
return $headers;
45+
}
46+
47+
public function getRequestVars()
48+
{
49+
$vars = array();
50+
51+
foreach($this->postVars as $var)
52+
{
53+
$vars[$var['name']] = trim($var['value']);
54+
}
55+
56+
foreach($this->files as $var)
57+
{
58+
$value = trim($var['value']);
59+
60+
if(!empty($value))
61+
{
62+
// $vars[$var['name']] = fopen($value, 'r');
63+
$vars[$var['name']] = '@' . $value;
64+
}
65+
}
66+
67+
return $vars;
68+
}
69+
2270
protected function defineAttributes()
2371
{
2472
return array(
2573
'url' => array(AttributeType::String),
2674
'action' => array(AttributeType::String, 'default' => 'post'),
2775
'postVars' => array(AttributeType::Mixed, 'default' => array()),
28-
'headers' => array(AttributeType::Mixed, 'default' => array())
76+
'headers' => array(AttributeType::Mixed, 'default' => array()),
77+
'files' => array(AttributeType::Mixed, 'default' => array())
2978
);
3079
}
3180
}

templates/services/http/settings.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,22 @@
6464
}
6565
},
6666
rows: settings.postVars
67+
}) }}
68+
69+
{{ forms.editableTableField({
70+
label: "File Uploads",
71+
instructions: "Enter the names and file paths to include in the request payload. POST is required for file uploads."|t,
72+
name: 'settings[serviceSettings][http][files]',
73+
id: 'http_files',
74+
cols: {
75+
name: {
76+
heading: 'Name'|t,
77+
type: 'singleline'
78+
},
79+
value: {
80+
heading: 'Path'|t,
81+
type: 'singleline'
82+
}
83+
},
84+
rows: settings.files
6785
}) }}

0 commit comments

Comments
 (0)