Skip to content

Commit

Permalink
Merge pull request #4 from stevenmaguire/card-attachments
Browse files Browse the repository at this point in the history
Update Http client to handle file upload successfully
  • Loading branch information
stevenmaguire committed Oct 19, 2015
2 parents 2699426 + 1b8736b commit d39edac
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
7 changes: 7 additions & 0 deletions API-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ $result = $client->getCardAttachments($cardId);
#### Add card attachment

```php
$attributes = [
'name' => 'Cheddar Bo is delicious!',
'file' => fopen('/path/to/cheddar-bo.jpg', 'r'),
'mimeType' => 'image/jpeg',
'url' => null
];

$result = $client->addCardAttachment($cardId, $attributes);
```
#### Delete card attachment
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#Changelog
All Notable changes to `trello-php` will be documented in this file

## 0.3.5 - 2015-10-19

### Added
- Update Http client to handle file upload successfully.

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing

## 0.3.4 - 2015-10-05

### Added
Expand Down
37 changes: 36 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class Http
{
/**
* Multipart resources to include in next request.
*
* @var array
*/
protected $multipartResources = [];

/**
* Http client
*
Expand Down Expand Up @@ -54,6 +61,14 @@ protected function authenticateRequest(RequestInterface $request)
*/
protected function createRequest($verb, $path, $parameters = [])
{
if (isset($parameters['file'])) {
$this->queueResourceAs(
'file',
\GuzzleHttp\Psr7\stream_for($parameters['file'])
);
unset($parameters['file']);
}

$request = new Request(
$verb,
$this->getUrlFromPath($path),
Expand Down Expand Up @@ -169,6 +184,22 @@ public function put($path, $parameters)
return $this->sendRequest($request);
}

/**
* Adds a given resource to multipart stream collection, to be processed by next request.
*
* @param string $name
* @param resource|string|Psr\Http\Message\StreamInterface $resource
*
* @return void
*/
protected function queueResourceAs($name, $resource)
{
array_push($this->multipartResources, [
'name' => $name,
'contents' => $resource,
]);
}

/**
* Retrieves http response for a given request.
*
Expand All @@ -180,7 +211,11 @@ public function put($path, $parameters)
protected function sendRequest(RequestInterface $request)
{
try {
$response = $this->httpClient->send($request);
$response = $this->httpClient->send($request, [
'multipart' => $this->multipartResources
]);

$this->multipartResources = [];

return json_decode($response->getBody());
} catch (RequestException $e) {
Expand Down
1 change: 1 addition & 0 deletions tests/ApiTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ public function testAddCardAttachment()
{
$cardId = $this->getTestString();
$attributes = $this->getTestAttributes();
$attributes['file'] = uniqid();
$payload = $this->getSuccessPayload();
$this->prepareFor("POST", sprintf("/cards/%s/attachments", $cardId), "", $payload);

Expand Down
8 changes: 6 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ protected function prepareFor($method, $path, $query = "", $payload = [], $statu
&& strpos($uri->getQuery(), $authorizedQuery) > -1;
});

$requestOptions = m::on(function ($options) {
return is_array($options);
});

if (is_string($payload)) {
$responseBody = $payload;
} else {
Expand All @@ -110,12 +114,12 @@ protected function prepareFor($method, $path, $query = "", $payload = [], $statu

$client = m::mock(HttpClient::class);
if ($status == 200) {
$client->shouldReceive('send')->with($request)->andReturn($response);
$client->shouldReceive('send')->with($request, $requestOptions)->andReturn($response);
} else {
$badRequest = m::mock(RequestInterface::class);
$response->shouldReceive('getReasonPhrase')->andReturn("");
$exception = new BadResponseException('test exception', $badRequest, $response);
$client->shouldReceive('send')->with($request)->andThrow($exception);
$client->shouldReceive('send')->with($request, $requestOptions)->andThrow($exception);
}

$this->client->setHttpClient($client);
Expand Down

0 comments on commit d39edac

Please sign in to comment.