Skip to content

Commit d39edac

Browse files
committed
Merge pull request #4 from stevenmaguire/card-attachments
Update Http client to handle file upload successfully
2 parents 2699426 + 1b8736b commit d39edac

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

API-GUIDE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,13 @@ $result = $client->getCardAttachments($cardId);
516516
#### Add card attachment
517517

518518
```php
519+
$attributes = [
520+
'name' => 'Cheddar Bo is delicious!',
521+
'file' => fopen('/path/to/cheddar-bo.jpg', 'r'),
522+
'mimeType' => 'image/jpeg',
523+
'url' => null
524+
];
525+
519526
$result = $client->addCardAttachment($cardId, $attributes);
520527
```
521528
#### Delete card attachment

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
#Changelog
22
All Notable changes to `trello-php` will be documented in this file
33

4+
## 0.3.5 - 2015-10-19
5+
6+
### Added
7+
- Update Http client to handle file upload successfully.
8+
9+
### Deprecated
10+
- Nothing
11+
12+
### Fixed
13+
- Nothing
14+
15+
### Removed
16+
- Nothing
17+
18+
### Security
19+
- Nothing
20+
421
## 0.3.4 - 2015-10-05
522

623
### Added

src/Http.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class Http
1010
{
11+
/**
12+
* Multipart resources to include in next request.
13+
*
14+
* @var array
15+
*/
16+
protected $multipartResources = [];
17+
1118
/**
1219
* Http client
1320
*
@@ -54,6 +61,14 @@ protected function authenticateRequest(RequestInterface $request)
5461
*/
5562
protected function createRequest($verb, $path, $parameters = [])
5663
{
64+
if (isset($parameters['file'])) {
65+
$this->queueResourceAs(
66+
'file',
67+
\GuzzleHttp\Psr7\stream_for($parameters['file'])
68+
);
69+
unset($parameters['file']);
70+
}
71+
5772
$request = new Request(
5873
$verb,
5974
$this->getUrlFromPath($path),
@@ -169,6 +184,22 @@ public function put($path, $parameters)
169184
return $this->sendRequest($request);
170185
}
171186

187+
/**
188+
* Adds a given resource to multipart stream collection, to be processed by next request.
189+
*
190+
* @param string $name
191+
* @param resource|string|Psr\Http\Message\StreamInterface $resource
192+
*
193+
* @return void
194+
*/
195+
protected function queueResourceAs($name, $resource)
196+
{
197+
array_push($this->multipartResources, [
198+
'name' => $name,
199+
'contents' => $resource,
200+
]);
201+
}
202+
172203
/**
173204
* Retrieves http response for a given request.
174205
*
@@ -180,7 +211,11 @@ public function put($path, $parameters)
180211
protected function sendRequest(RequestInterface $request)
181212
{
182213
try {
183-
$response = $this->httpClient->send($request);
214+
$response = $this->httpClient->send($request, [
215+
'multipart' => $this->multipartResources
216+
]);
217+
218+
$this->multipartResources = [];
184219

185220
return json_decode($response->getBody());
186221
} catch (RequestException $e) {

tests/ApiTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ public function testAddCardAttachment()
12141214
{
12151215
$cardId = $this->getTestString();
12161216
$attributes = $this->getTestAttributes();
1217+
$attributes['file'] = uniqid();
12171218
$payload = $this->getSuccessPayload();
12181219
$this->prepareFor("POST", sprintf("/cards/%s/attachments", $cardId), "", $payload);
12191220

tests/ClientTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ protected function prepareFor($method, $path, $query = "", $payload = [], $statu
9494
&& strpos($uri->getQuery(), $authorizedQuery) > -1;
9595
});
9696

97+
$requestOptions = m::on(function ($options) {
98+
return is_array($options);
99+
});
100+
97101
if (is_string($payload)) {
98102
$responseBody = $payload;
99103
} else {
@@ -110,12 +114,12 @@ protected function prepareFor($method, $path, $query = "", $payload = [], $statu
110114

111115
$client = m::mock(HttpClient::class);
112116
if ($status == 200) {
113-
$client->shouldReceive('send')->with($request)->andReturn($response);
117+
$client->shouldReceive('send')->with($request, $requestOptions)->andReturn($response);
114118
} else {
115119
$badRequest = m::mock(RequestInterface::class);
116120
$response->shouldReceive('getReasonPhrase')->andReturn("");
117121
$exception = new BadResponseException('test exception', $badRequest, $response);
118-
$client->shouldReceive('send')->with($request)->andThrow($exception);
122+
$client->shouldReceive('send')->with($request, $requestOptions)->andThrow($exception);
119123
}
120124

121125
$this->client->setHttpClient($client);

0 commit comments

Comments
 (0)