@@ -41,8 +41,8 @@ as a second argument:
4141``` php
4242<?php
4343
44- use function MNC\Http\buffer;
4544use function MNC\Http\fetch;
45+ use Castor\Io\Eof;
4646
4747$response = fetch('https://some-domain.example/some-form', [
4848 'method' => 'POST',
@@ -53,8 +53,14 @@ $response = fetch('https://some-domain.example/some-form', [
5353 'body' => json_encode(['data' => 'value'])
5454]);
5555
56- // Emit the response to stdout
57- while (($chunk = $response->body()->read()) !== null) {
56+ // Emit the response to stdout in chunks
57+ while (true) {
58+ $chunk = '';
59+ try {
60+ $response->body()->read(4096, $chunk);
61+ } catch (Eof $e) {
62+ break;
63+ }
5864 echo $chunk;
5965}
6066```
@@ -86,7 +92,9 @@ echo $response->status()->reasonPhrase(); // OK
8692echo $response->headers()->has('content-type'); // true
8793echo $response->headers()->contains('content-type', 'html'); // true
8894echo $response->headers()->get('content-type'); // text/html;charset=utf-8
89- echo $response->body()->read(); // Outputs some bytes from the response body
95+ $bytes = '';
96+ echo $response->body()->read(4096, $bytes); // Allocates reader data into $bytes
97+ echo $bytes; // Outputs some bytes from the response body
9098```
9199
92100### Exception Handling
@@ -112,23 +120,23 @@ you most likely will be reacting in different ways to each one of them.
112120### Body Buffering
113121
114122When you call the ` MNC\Http\Response::body() ` method you get an instance of
115- ` MNC\Http \Io\Reader` , which is a very simple interface inspired in golang's
123+ ` Castor \Io\Reader` , which is a very simple interface inspired in golang's
116124` io.Reader ` . This interface allows you to read a chunk of bytes until you reach
117125` EOF ` in the data source.
118126
119127Often times, you don't want to read byte per byte, but get the whole contents
120- of the body as a string at once. This library provides the ` buffer ` function
128+ of the body as a string at once. This library provides the ` readAll ` function
121129as a convenience for that:
122130
123131``` php
124132<?php
125133
126- use function MNC\Http\buffer ;
134+ use function Castor\Io\readAll ;
127135use function MNC\Http\fetch;
128136
129137$response = fetch('https://mnavarro.dev');
130138
131- echo buffer ($response->body()); // Buffers all the contents in memory and emits them.
139+ echo readAll ($response->body()); // Buffers all the contents in memory and emits them.
132140````
133141
134142Buffering is a very good convenience, but it needs to be used with care, since it could
@@ -148,15 +156,15 @@ in content types like `text/plain`. However, there is big gain in user experienc
148156when we provide helpers like these in our apis.
149157
150158This library provides an approach a bit more safe. If the response headers contain the
151- `application/json` content type, the `MNC\Http\ Io\Reader` object of the body is internally
159+ `application/json` content type, the ``Castor\ Io\Reader` ` object of the body is internally
152160decorated with a `MNC\Http\Encoding\Json` object. This object implements both the
153- `Reader` and `JsonDecoder` interfaces . Checking for the former is the safest way of
154- handling json payloads:
161+ `Reader` interface . Checking for the former is the safest way of handling json
162+ payloads:
155163
156164```php
157165<?php
158166
159- use MNC\Http\Encoding\JsonDecoder ;
167+ use MNC\Http\Encoding\Json ;
160168use function MNC\Http\fetch;
161169
162170$response = fetch('https://api.github.com/users/mnavarrocarter', [
@@ -167,7 +175,7 @@ $response = fetch('https://api.github.com/users/mnavarrocarter', [
167175
168176$body = $response->body();
169177
170- if ($body instanceof JsonDecoder ) {
178+ if ($body instanceof Json ) {
171179 var_dump($body->decode()); // Dumps the json as an array
172180} else {
173181 // The response body is not json encoded
@@ -236,7 +244,7 @@ api, using the token internally.
236244``` php
237245<?php
238246
239- use MNC\Http\Encoding\JsonDecoder ;
247+ use MNC\Http\Encoding\Json ;
240248use function MNC\Http\fetch;
241249
242250$authenticate = static function (string $token) {
@@ -253,7 +261,7 @@ $authenticate = static function (string $token) {
253261 ]);
254262
255263 $body = $response->body();
256- if ($body instanceof JsonDecoder ) {
264+ if ($body instanceof Json ) {
257265 return $body->decode();
258266 }
259267 return null;
@@ -285,7 +293,7 @@ api client that I need to use.
285293``` php
286294<?php
287295
288- use MNC\Http\Encoding\JsonDecoder ;
296+ use MNC\Http\Encoding\Json ;
289297use function MNC\Http\fetch;
290298
291299// We start with an interface, a well defined contract.
@@ -325,7 +333,7 @@ final class FetchApiClient implements ApiClient
325333 ]);
326334
327335 $body = $response->body();
328- if ($body instanceof JsonDecoder ) {
336+ if ($body instanceof Json ) {
329337 return $body->decode();
330338 }
331339 return null;
0 commit comments