-
Notifications
You must be signed in to change notification settings - Fork 260
/
Http.php
137 lines (120 loc) · 4.4 KB
/
Http.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Zendesk\API;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\LazyOpenStream;
use GuzzleHttp\Psr7\Request;
use Zendesk\API\Exceptions\ApiResponseException;
use Zendesk\API\Exceptions\AuthException;
/**
* HTTP functions via curl
* @package Zendesk\API
*/
class Http
{
public static $curl;
/**
* Prepares an endpoint URL with optional side-loading
*
* @param array $sideload
* @param array $iterators
*
* @return string
*/
public static function prepareQueryParams(array $sideload = null, array $iterators = null)
{
$addParams = [];
// First look for side-loaded variables
if (is_array($sideload)) {
$addParams['include'] = implode(',', $sideload);
}
// Next look for special collection iterators
if (is_array($iterators)) {
foreach ($iterators as $k => $v) {
if (in_array($k, ['per_page', 'page', 'sort_order', 'sort_by'])) {
$addParams[$k] = $v;
}
}
}
return $addParams;
}
/**
* Use the send method to call every endpoint except for oauth/tokens
*
* @param HttpClient $client
* @param string $endPoint E.g. "/tickets.json"
* @param array $options
* Available options are listed below:
* array $queryParams Array of unencoded key-value pairs, e.g. ["ids" => "1,2,3,4"]
* array $postFields Array of unencoded key-value pairs, e.g. ["filename" => "blah.png"]
* string $method "GET", "POST", etc. Default is GET.
* string $contentType Default is "application/json"
*
* @return array The response body, parsed from JSON into an associative array
* @throws ApiResponseException
* @throws AuthException
*/
public static function send(
HttpClient $client,
$endPoint,
$options = []
) {
$options = array_merge(
[
'method' => 'GET',
'contentType' => 'application/json',
'postFields' => null,
'queryParams' => null
],
$options
);
$headers = [
'Accept' => 'application/json',
'Content-Type' => $options['contentType'],
'User-Agent' => $client->getUserAgent()
];
$request = new Request(
$options['method'],
$client->getApiUrl() . $endPoint,
$headers
);
$requestOptions = [];
if (! empty($options['multipart'])) {
$request = $request->withoutHeader('Content-Type');
$requestOptions['multipart'] = $options['multipart'];
} elseif (! empty($options['postFields'])) {
$request = $request->withBody(\GuzzleHttp\Psr7\stream_for(json_encode($options['postFields'])));
} elseif (! empty($options['file'])) {
if (is_file($options['file'])) {
$fileStream = new LazyOpenStream($options['file'], 'r');
$request = $request->withBody($fileStream);
}
}
if (! empty($options['queryParams'])) {
foreach ($options['queryParams'] as $queryKey => $queryValue) {
$uri = $request->getUri();
$uri = $uri->withQueryValue($uri, $queryKey, $queryValue);
$request = $request->withUri($uri, true);
}
}
try {
list ($request, $requestOptions) = $client->getAuth()->prepareRequest($request, $requestOptions);
$response = $client->guzzle->send($request, $requestOptions);
} catch (RequestException $e) {
throw new ApiResponseException($e);
} finally {
$client->setDebug(
$request->getHeaders(),
$request->getBody()->getContents(),
isset($response) ? $response->getStatusCode() : null,
isset($response) ? $response->getHeaders() : null,
isset($e) ? $e : null
);
$request->getBody()->rewind();
}
if (isset($file)) {
fclose($file);
}
$client->setSideload(null);
return json_decode($response->getBody()->getContents());
}
}