Skip to content

Commit 5b34368

Browse files
committed
Refactored HttpClient to send a raw request body instead of only JSON encoded parameters.
This is needed to support Asset creation where the request body must be the content of the asset. This involves the following changes - Updated HttpClient methods to accept an untyped body instead of an array of parameters - Moved JSON encoding to AbstractApi and updated documentation accordingly - Updated test cases
1 parent 782b02d commit 5b34368

File tree

8 files changed

+132
-66
lines changed

8 files changed

+132
-66
lines changed

lib/Github/Api/AbstractApi.php

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public function setPerPage($perPage)
5757
}
5858

5959
/**
60-
* {@inheritDoc}
60+
* Send a GET request with query parameters.
61+
*
62+
* @param string $path Request path.
63+
* @param array $parameters GET parameters.
64+
* @param array $requestHeaders Request Headers.
65+
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
6166
*/
6267
protected function get($path, array $parameters = array(), $requestHeaders = array())
6368
{
@@ -70,42 +75,104 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
7075
}
7176

7277
/**
73-
* {@inheritDoc}
78+
* Send a POST request with JSON-encoded parameters.
79+
*
80+
* @param string $path Request path.
81+
* @param array $parameters POST parameters to be JSON encoded.
82+
* @param array $requestHeaders Request headers.
7483
*/
7584
protected function post($path, array $parameters = array(), $requestHeaders = array())
7685
{
77-
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
86+
return $this->postRaw(
87+
$path,
88+
$this->createJsonBody($parameters),
89+
$requestHeaders
90+
);
91+
}
92+
93+
/**
94+
* Send a POST request with raw data.
95+
*
96+
* @param string $path Request path.
97+
* @param $body Request body.
98+
* @param array $requestHeaders Request headers.
99+
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
100+
*/
101+
protected function postRaw($path, $body, $requestHeaders = array())
102+
{
103+
$response = $this->client->getHttpClient()->post(
104+
$path,
105+
$body,
106+
$requestHeaders
107+
);
78108

79109
return ResponseMediator::getContent($response);
80110
}
81111

112+
82113
/**
83-
* {@inheritDoc}
114+
* Send a PATCH request with JSON-encoded parameters.
115+
*
116+
* @param string $path Request path.
117+
* @param array $parameters POST parameters to be JSON encoded.
118+
* @param array $requestHeaders Request headers.
84119
*/
85120
protected function patch($path, array $parameters = array(), $requestHeaders = array())
86121
{
87-
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
122+
$response = $this->client->getHttpClient()->patch(
123+
$path,
124+
$this->createJsonBody($parameters),
125+
$requestHeaders
126+
);
88127

89128
return ResponseMediator::getContent($response);
90129
}
91130

131+
92132
/**
93-
* {@inheritDoc}
133+
* Send a PUT request with JSON-encoded parameters.
134+
*
135+
* @param string $path Request path.
136+
* @param array $parameters POST parameters to be JSON encoded.
137+
* @param array $requestHeaders Request headers.
94138
*/
95139
protected function put($path, array $parameters = array(), $requestHeaders = array())
96140
{
97-
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
141+
$response = $this->client->getHttpClient()->put(
142+
$path,
143+
$this->createJsonBody($parameters),
144+
$requestHeaders
145+
);
98146

99147
return ResponseMediator::getContent($response);
100148
}
101149

150+
102151
/**
103-
* {@inheritDoc}
152+
* Send a DELETE request with JSON-encoded parameters.
153+
*
154+
* @param string $path Request path.
155+
* @param array $parameters POST parameters to be JSON encoded.
156+
* @param array $requestHeaders Request headers.
104157
*/
105158
protected function delete($path, array $parameters = array(), $requestHeaders = array())
106159
{
107-
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
160+
$response = $this->client->getHttpClient()->delete(
161+
$path,
162+
$this->createJsonBody($parameters),
163+
$requestHeaders
164+
);
108165

109166
return ResponseMediator::getContent($response);
110167
}
168+
169+
/**
170+
* Create a JSON encoded version of an array of parameters.
171+
*
172+
* @param $parameters Request parameters
173+
* @return null|string
174+
*/
175+
protected function createJsonBody(array $parameters) {
176+
return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0);
177+
}
111178
}

lib/Github/HttpClient/CachedHttpClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function setCache(CacheInterface $cache)
4242
/**
4343
* {@inheritdoc}
4444
*/
45-
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
45+
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
4646
{
47-
$response = parent::request($path, $parameters, $httpMethod, $headers);
47+
$response = parent::request($path, $body, $httpMethod, $headers, $options);
4848

4949
$key = trim($this->options['base_url'].$path, '/');
5050
if (304 == $response->getStatusCode()) {
@@ -61,9 +61,9 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET',
6161
*
6262
* {@inheritdoc}
6363
*/
64-
protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
64+
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
6565
{
66-
$request = parent::createRequest($httpMethod, $path, $parameters, $headers = array());
66+
$request = parent::createRequest($httpMethod, $path, $body, $headers = array(), $options);
6767

6868
if ($modifiedAt = $this->getCache()->getModifiedSince($path)) {
6969
$modifiedAt = new \DateTime('@'.$modifiedAt);

lib/Github/HttpClient/HttpClient.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,47 +87,47 @@ public function addListener($eventName, $listener)
8787
*/
8888
public function get($path, array $parameters = array(), array $headers = array())
8989
{
90-
return $this->request($path, $parameters, 'GET', $headers);
90+
return $this->request($path, null, 'GET', $headers, array('query' => $parameters));
9191
}
9292

9393
/**
9494
* {@inheritDoc}
9595
*/
96-
public function post($path, array $parameters = array(), array $headers = array())
96+
public function post($path, $body = null, array $headers = array())
9797
{
98-
return $this->request($path, $parameters, 'POST', $headers);
98+
return $this->request($path, $body, 'POST', $headers);
9999
}
100100

101101
/**
102102
* {@inheritDoc}
103103
*/
104-
public function patch($path, array $parameters = array(), array $headers = array())
104+
public function patch($path, $body = null, array $headers = array())
105105
{
106-
return $this->request($path, $parameters, 'PATCH', $headers);
106+
return $this->request($path, $body, 'PATCH', $headers);
107107
}
108108

109109
/**
110110
* {@inheritDoc}
111111
*/
112-
public function delete($path, array $parameters = array(), array $headers = array())
112+
public function delete($path, $body = null, array $headers = array())
113113
{
114-
return $this->request($path, $parameters, 'DELETE', $headers);
114+
return $this->request($path, $body, 'DELETE', $headers);
115115
}
116116

117117
/**
118118
* {@inheritDoc}
119119
*/
120-
public function put($path, array $parameters = array(), array $headers = array())
120+
public function put($path, $body, array $headers = array())
121121
{
122-
return $this->request($path, $parameters, 'PUT', $headers);
122+
return $this->request($path, $body, 'PUT', $headers);
123123
}
124124

125125
/**
126126
* {@inheritDoc}
127127
*/
128-
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
128+
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array())
129129
{
130-
$request = $this->createRequest($httpMethod, $path, $parameters, $headers);
130+
$request = $this->createRequest($httpMethod, $path, $body, $headers, $options);
131131
$request->addHeaders($headers);
132132

133133
try {
@@ -170,23 +170,13 @@ public function getLastResponse()
170170
return $this->lastResponse;
171171
}
172172

173-
protected function createRequest($httpMethod, $path, array $parameters = array(), array $headers = array())
173+
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
174174
{
175-
if ('GET' !== $httpMethod) {
176-
$requestBody = count($parameters) === 0
177-
? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0)
178-
;
179-
$options = array();
180-
} else {
181-
$requestBody = null;
182-
$options = array('query' => $parameters);
183-
}
184-
185175
return $this->client->createRequest(
186176
$httpMethod,
187177
$path,
188178
array_merge($this->headers, $headers),
189-
$requestBody,
179+
$body,
190180
$options
191181
);
192182
}

lib/Github/HttpClient/HttpClientInterface.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,59 @@ public function get($path, array $parameters = array(), array $headers = array()
2626
* Send a POST request
2727
*
2828
* @param string $path Request path
29-
* @param array $parameters POST Parameters
29+
* @param mixed $body Request body
3030
* @param array $headers Reconfigure the request headers for this call only
3131
*
3232
* @return array Data
3333
*/
34-
public function post($path, array $parameters = array(), array $headers = array());
34+
public function post($path, $body = null, array $headers = array());
3535

3636
/**
3737
* Send a PATCH request
3838
*
3939
* @param string $path Request path
40-
* @param array $parameters PATCH Parameters
40+
* @param mixed $body Reuqest body
4141
* @param array $headers Reconfigure the request headers for this call only
4242
*
43+
* @internal param array $parameters Request body
4344
* @return array Data
4445
*/
45-
public function patch($path, array $parameters = array(), array $headers = array());
46+
public function patch($path, $body = null, array $headers = array());
4647

4748
/**
4849
* Send a PUT request
4950
*
5051
* @param string $path Request path
51-
* @param array $parameters PUT Parameters
52+
* @param mixed $body Request body
5253
* @param array $headers Reconfigure the request headers for this call only
5354
*
5455
* @return array Data
5556
*/
56-
public function put($path, array $parameters = array(), array $headers = array());
57+
public function put($path, $body, array $headers = array());
5758

5859
/**
5960
* Send a DELETE request
6061
*
6162
* @param string $path Request path
62-
* @param array $parameters DELETE Parameters
63+
* @param mixed $body Request body
6364
* @param array $headers Reconfigure the request headers for this call only
6465
*
6566
* @return array Data
6667
*/
67-
public function delete($path, array $parameters = array(), array $headers = array());
68+
public function delete($path, $body = null, array $headers = array());
6869

6970
/**
7071
* Send a request to the server, receive a response,
7172
* decode the response and returns an associative array
7273
*
73-
* @param string $path Request API path
74-
* @param array $parameters Parameters
74+
* @param string $path Request path
75+
* @param mixed $body Request body
7576
* @param string $httpMethod HTTP method to use
7677
* @param array $headers Request headers
7778
*
7879
* @return array Data
7980
*/
80-
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array());
81+
public function request($path, $body, $httpMethod = 'GET', array $headers = array());
8182

8283
/**
8384
* Change an option value.

test/Github/Tests/Api/AbstractApiTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ public function post($path, array $parameters = array(), $requestHeaders = array
161161
return $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
162162
}
163163

164+
/**
165+
* {@inheritDoc}
166+
*/
167+
public function postRaw($path, $body, $requestHeaders = array())
168+
{
169+
return $this->client->getHttpClient()->post($path, $body, $requestHeaders);
170+
}
171+
164172
/**
165173
* {@inheritDoc}
166174
*/

test/Github/Tests/Api/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function getApiMock()
1919
$client->setHttpClient($mock);
2020

2121
return $this->getMockBuilder($this->getApiClass())
22-
->setMethods(array('get', 'post', 'patch', 'delete', 'put'))
22+
->setMethods(array('get', 'post', 'postRaw', 'patch', 'delete', 'put'))
2323
->setConstructorArgs(array($client))
2424
->getMock();
2525
}

0 commit comments

Comments
 (0)