Skip to content

Commit 7a2353d

Browse files
author
Jason Smale
committed
Merge pull request #39 from vbuck/master
Add headers param to API calls, auto-detect JSON for encoding to raw data, prevent exceptions caused by logging non UTF-8 data
2 parents f404ea2 + f618328 commit 7a2353d

File tree

1 file changed

+28
-9
lines changed
  • src/app/code/community/Zendesk/Zendesk/Model/Api

1 file changed

+28
-9
lines changed

src/app/code/community/Zendesk/Zendesk/Model/Api/Abstract.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ protected function _getUrl($path)
2424
return $base_url . '/' . $path;
2525
}
2626

27-
protected function _call($endpoint, $params = null, $method = 'GET', $data = null)
27+
protected function _call(
28+
$endpoint,
29+
$params = null,
30+
$method = 'GET',
31+
$data = null,
32+
$headers = null
33+
)
2834
{
35+
$usingRawData = false;
36+
2937
if($params && is_array($params) && count($params) > 0) {
3038
$args = array();
3139
foreach($params as $arg => $val) {
@@ -34,33 +42,44 @@ protected function _call($endpoint, $params = null, $method = 'GET', $data = nul
3442
$endpoint .= '?' . implode('&', $args);
3543
}
3644

45+
if (empty($headers)) {
46+
$headers = array(
47+
'Accept' => 'application/json',
48+
'Content-Type' => 'application/json',
49+
);
50+
}
51+
3752
$url = $this->_getUrl($endpoint);
3853

3954
$method = strtoupper($method);
4055

4156
$client = new Zend_Http_Client($url);
4257
$client->setMethod($method);
43-
$client->setHeaders(
44-
array(
45-
'Accept' => 'application/json',
46-
'Content-Type' => 'application/json'
47-
)
48-
);
58+
$client->setHeaders($headers);
59+
4960
$client->setAuth(
5061
Mage::getStoreConfig('zendesk/general/email') . '/token',
5162
Mage::getStoreConfig('zendesk/general/password')
5263
);
5364

5465
if($method == 'POST' || $method == 'PUT') {
55-
$client->setRawData(json_encode($data), 'application/json');
66+
$contentType = $client->getHeader('Content-Type');
67+
68+
if ($contentType == 'application/json' && !preg_match('/^[\{\[]/', $contentType)) {
69+
$data = json_encode($data);
70+
}
71+
72+
$client->setRawData($data, $client->getHeader('Content-Type'));
73+
74+
$usingRawData = true;
5675
}
5776

5877
Mage::log(
5978
print_r(
6079
array(
6180
'url' => $url,
6281
'method' => $method,
63-
'data' => json_encode($data),
82+
'data' => $usingRawData ? '' : json_encode($data),
6483
),
6584
true
6685
),

0 commit comments

Comments
 (0)