From ca0cc0abe8a6ec405462d79dd5d61173cb2d7def Mon Sep 17 00:00:00 2001 From: zymawy Date: Fri, 11 Dec 2020 15:53:16 +0300 Subject: [PATCH 1/2] feat: Added Stream Functionality --- src/Client.php | 29 +++++++++++++++++++++++++++++ tests/ClientTest.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/Client.php b/src/Client.php index 69e1472..73722d4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,6 +6,7 @@ use Exception; use GuzzleHttp\Psr7\MultipartStream; +use GuzzleHttp\Psr7\Response; use Http\Client\HttpClient; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\MessageFactoryDiscovery; @@ -41,6 +42,22 @@ public function post(GotenbergRequestInterface $request): ResponseInterface return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); } + /** + * Sends the given documents to the API and returns the response diractly to the cleint. + * + * @throws ClientException + * @throws Exception + */ + public function stream(GotenbergRequestInterface $request): ResponseInterface + { + + return new Response(200, [ + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename=stream.pdf', + 'Cache-Control' => 'max-age=0' + ], $this->transformResponse($request)->getBody()); + } + /** * Sends the given documents to the API, stores the resulting PDF in the given destination. * @@ -106,4 +123,16 @@ private function handleResponse(ResponseInterface $response): ResponseInterface throw new ClientException($response->getBody()->getContents(), $response->getStatusCode()); } } + + /** + * @param \TheCodingMachine\Gotenberg\GotenbergRequestInterface $request + * @return \Psr\Http\Message\ResponseInterface + * @throws \Http\Client\Exception + * @throws \TheCodingMachine\Gotenberg\ClientException + */ + private function transformResponse(GotenbergRequestInterface $request): ResponseInterface + { + + return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); + } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 380bb06..ebadd5c 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -263,4 +263,46 @@ public function testRemoteURLHTTPHeader(): void $response = $client->post($request); $this->assertEquals(200, $response->getStatusCode()); } + + /** + * @throws RequestException + * @throws ClientException + */ + public function testStream(): void + { + $client = new Client(self::API_URL, new \Http\Adapter\Guzzle6\Client()); + // case 1: HTML. + $request = $this->createHTMLRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + + // case 2: URL. + $request = $this->createURLRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + // case 3: markdown. + $request = $this->createMarkdownRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + + // case 4: office. + $request = $this->createOfficeRequest(); + $request->setPageRanges('1-1'); + $this->assertCaseFor($request, $client); + } + + + private function assertCaseFor($request, $client) { + + if (method_exists($request, 'setWaitTimeout')) { + $request->setWaitTimeout(30.0); + } + + $response = $client->stream($request); + $this->assertEquals($response->getHeaderLine('Content-Type'), 'application/pdf'); + $this->assertEquals($response->getHeaderLine('Content-Disposition'), 'inline; filename=stream.pdf'); + $this->assertEquals($response->getHeaderLine('Cache-Control'), 'max-age=0'); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertNotEmpty($response->getBody()); + } } From 5f573ffd436f8459b71f2c817160b2c0294edc47 Mon Sep 17 00:00:00 2001 From: zymawy Date: Sat, 12 Dec 2020 01:24:54 +0300 Subject: [PATCH 2/2] run make lint --- src/Client.php | 8 ++------ tests/ClientTest.php | 6 +++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index 73722d4..99006f5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -50,11 +50,10 @@ public function post(GotenbergRequestInterface $request): ResponseInterface */ public function stream(GotenbergRequestInterface $request): ResponseInterface { - return new Response(200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename=stream.pdf', - 'Cache-Control' => 'max-age=0' + 'Cache-Control' => 'max-age=0', ], $this->transformResponse($request)->getBody()); } @@ -125,14 +124,11 @@ private function handleResponse(ResponseInterface $response): ResponseInterface } /** - * @param \TheCodingMachine\Gotenberg\GotenbergRequestInterface $request - * @return \Psr\Http\Message\ResponseInterface * @throws \Http\Client\Exception - * @throws \TheCodingMachine\Gotenberg\ClientException + * @throws ClientException */ private function transformResponse(GotenbergRequestInterface $request): ResponseInterface { - return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request))); } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index ebadd5c..39ca158 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -7,6 +7,7 @@ use HTTP\Client\Exception; use PHPUnit\Framework\TestCase; use Safe\Exceptions\FilesystemException; +use function method_exists; final class ClientTest extends TestCase { @@ -291,9 +292,8 @@ public function testStream(): void $this->assertCaseFor($request, $client); } - - private function assertCaseFor($request, $client) { - + private function assertCaseFor($request, $client): void + { if (method_exists($request, 'setWaitTimeout')) { $request->setWaitTimeout(30.0); }