diff --git a/API-GUIDE.md b/API-GUIDE.md index 91ddcde..2727b07 100644 --- a/API-GUIDE.md +++ b/API-GUIDE.md @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 4302ea3..9b23dd0 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Http.php b/src/Http.php index 25178f2..504414c 100644 --- a/src/Http.php +++ b/src/Http.php @@ -8,6 +8,13 @@ class Http { + /** + * Multipart resources to include in next request. + * + * @var array + */ + protected $multipartResources = []; + /** * Http client * @@ -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), @@ -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. * @@ -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) { diff --git a/tests/ApiTestTrait.php b/tests/ApiTestTrait.php index 2412f81..27d9528 100644 --- a/tests/ApiTestTrait.php +++ b/tests/ApiTestTrait.php @@ -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); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 4f5f9a1..6c2a723 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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 { @@ -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);