Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

feat: Added Stream Functionality #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,21 @@ 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.
*
Expand Down Expand Up @@ -106,4 +122,13 @@ private function handleResponse(ResponseInterface $response): ResponseInterface
throw new ClientException($response->getBody()->getContents(), $response->getStatusCode());
}
}

/**
* @throws \Http\Client\Exception
* @throws ClientException
*/
private function transformResponse(GotenbergRequestInterface $request): ResponseInterface
{
return $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
}
}
42 changes: 42 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -263,4 +264,45 @@ 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): void
{
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());
}
}