1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
<?php
declare(strict_types=1);
namespace Cepis;
/**
* Client for the public CEP API at https://cep.is.
*
* Reuse one instance: the configuration is immutable and no method keeps state
* between calls.
*
* ```php
* $client = new Cepis\Client(timeout: 5.0, totalTimeout: 15.0);
* echo $client->lookup('01001-000')->localidade; // São Paulo
* ```
*
* For a script, {@see Client::default()} is the shared, default-configured
* instance:
*
* ```php
* echo Cepis\Client::default()->lookup('01001-000')->uf; // SP
* ```
*
* Retries cover 429, the transient 5xx in {@see Client::RETRIABLE_STATUS} and
* network failures, with exponential backoff and full jitter. 400 and 404 are
* deliberately absent from that set: both are definitive answers about the
* request, so retrying returns the same thing more slowly while burning
* someone else's quota.
*/
final class Client
{
public const VERSION = '0.1.0';
/**
* Where the API lives, unless `baseUrl` says otherwise.
*/
public const DEFAULT_URL = 'https://cep.is';
/**
* Most CEPs the API accepts in a single batch request.
*/
public const MAX_BATCH = 500;
/**
* Statuses worth trying again.
*/
public const RETRIABLE_STATUS = [429, 500, 502, 503, 504];
/**
* Where the API lives. A trailing slash is stripped, so it cannot produce
* doubled separators that some proxies normalise and others 404.
*/
public readonly string $baseUrl;
/**
* Identifies the library and its version to the server.
*/
public readonly string $userAgent;
/**
* How the GET is actually performed. {@see CurlTransport} unless the
* caller supplied one.
*/
public readonly Transport $transport;
private static ?self $default = null;
/**
* @param string $baseUrl Where the API lives. Point it at a local instance
* (the API repo brings one up on `http://localhost:8080` with
* `just up`) or at a test server. It is the only setting that usually
* changes between environments.
* @param float $timeout Seconds to allow for **a single attempt** —
* connection, response and body read together. It is not a cap on the
* whole call; that cap is `$totalTimeout`.
* @param int $maxAttempts How many attempts in total, counting the first.
* `1` disables retrying. The default of `3` covers the common case
* (one transient failure) without turning a real outage into a long
* wait.
* @param float $backoffBase Base of the exponential wait, in seconds. The
* actual wait is drawn from `[0, base * 2 ** (n - 1)]` — *full
* jitter*. The randomness is not decoration: without it, N clients
* that fail together come back together and reproduce the spike that
* took the service down.
* @param float $backoffMax Cap on each individual wait, in seconds. It
* applies to a server-sent `Retry-After` too, or a
* `Retry-After: 3600` from a misconfigured proxy would park the
* process for an hour.
* @param float|null $totalTimeout Cap on the whole call, attempts and
* waits included. `null` leaves `$maxAttempts` as the only bound. Set
* it in code serving a web request: without it the worst case is
* `$maxAttempts * $timeout` plus the sum of the backoffs.
* @param string|null $userAgent Overrides the `User-Agent`. The default
* identifies the library and its version, which is what lets the
* server diagnose an old client misbehaving.
* @param Transport|null $transport Overrides how the GET is performed.
* Supply one to route requests through the host application's HTTP
* layer — a WordPress plugin passes an implementation over
* `wp_remote_get`. `null` uses curl directly.
*
* @throws \InvalidArgumentException a setting is out of range.
*/
public function __construct(
string $baseUrl = self::DEFAULT_URL,
public readonly float $timeout = 10.0,
public readonly int $maxAttempts = 3,
public readonly float $backoffBase = 0.5,
public readonly float $backoffMax = 8.0,
public readonly ?float $totalTimeout = null,
?string $userAgent = null,
?Transport $transport = null,
) {
if ($maxAttempts < 1) {
throw new \InvalidArgumentException('maxAttempts must be >= 1');
}
if ($timeout <= 0) {
throw new \InvalidArgumentException('timeout must be > 0');
}
if ($backoffBase < 0 || $backoffMax < 0) {
throw new \InvalidArgumentException('backoffBase and backoffMax must be >= 0');
}
$this->baseUrl = rtrim($baseUrl, '/');
$this->userAgent = $userAgent ?? sprintf('cepis-php/%s (+https://cep.is)', self::VERSION);
$this->transport = $transport ?? new CurlTransport();
}
/**
* The shared, default-configured client.
*
* Built once because {@see Client} keeps no state between calls. It
* applies process-wide, third-party libraries that also use this package
* included. Anyone needing different settings should build their own with
* `new Client(...)`.
*/
public static function default(): self
{
return self::$default ??= new self();
}
/**
* Return the CEP's 8 digits, accepting the mask.
*
* `"01001-000"`, `"01001000"` and `" 01001-000 "` all become
* `"01001000"`.
*
* This runs **before** any request: a 7-digit CEP is the caller's bug, and
* spending a round trip for the server to say so wastes both sides' time.
*
* @throws InvalidCepException unless exactly 8 digits remain.
*/
public static function normalizeCep(string $value): string
{
$cleaned = str_replace(['-', '.', ' '], '', trim($value));
if (preg_match('/^\d{8}$/', $cleaned) !== 1) {
throw new InvalidCepException(sprintf('CEP must be 8 digits, got "%s"', $value));
}
return $cleaned;
}
/**
* Look up a single CEP, masked or unmasked.
*
* ```php
* $client->lookup('01001-000')->localidade; // "São Paulo"
* ```
*
* A 400 from the API is surfaced as {@see InvalidCepException} carrying
* the API's own message: the format already passed local validation, so
* the server knows something this client does not.
*
* @throws InvalidCepException the CEP is not 8 digits (no request is made).
* @throws CepNotFoundException well-formed, but not in the database.
* @throws RateLimitedException 429 after every attempt.
* @throws ServerErrorException 5xx after every attempt.
* @throws NetworkException the request never completed.
* @throws InvalidResponseException a success status with an unreadable body.
*/
public function lookup(string $cep): Address
{
$number = self::normalizeCep($cep);
[$status, $body] = $this->request('/' . $number);
if ($status === 404) {
throw new CepNotFoundException(
sprintf('CEP %s does not exist in the DNE database', $number),
);
}
if ($status === 400) {
throw new InvalidCepException(
self::errorMessage($body, sprintf('CEP %s rejected', $number)),
);
}
return Address::fromArray($this->decodeJsonObject($body));
}
/**
* Look up many CEPs in one batch.
*
* Returns an array of **the same length and in the same order** as the
* input, re-indexed from zero. A CEP that does not exist becomes `null` at
* its position — it does not vanish from the array, and it does not throw.
* That is the right difference from {@see Client::lookup()}: in a batch of
* a thousand, one bad CEP must not cost the other nine hundred and
* ninety-nine.
*
* A malformed CEP also becomes `null`, for the same reason, rather than
* throwing {@see InvalidCepException}. To know *which* one was malformed,
* validate first with {@see Client::normalizeCep()}.
*
* Input is chunked into blocks of {@see Client::MAX_BATCH} (the API's
* limit) automatically. Do not loop over {@see Client::lookup()}: a loop of
* 500 calls pays 500 TLS handshakes, the batch pays one.
*
* **Batches carry no coordinate.** The endpoint behind them is CSV, and
* the CSV has no `geo` column. Every {@see Address} returned here has a
* `null` `geo`, including for CEPs that do have one. Need the coordinate?
* Use {@see Client::lookup()}, one at a time.
*
* @param list<string> $ceps
*
* @return list<Address|null>
*/
public function lookupMany(array $ceps): array
{
if ($ceps === []) {
return [];
}
$result = [];
foreach (array_chunk(array_values($ceps), self::MAX_BATCH) as $chunk) {
foreach ($this->lookupChunk($chunk) as $address) {
$result[] = $address;
}
}
return $result;
}
/**
* Query `GET /health`: does the API answer, and with which database.
*
* Does not throw for an API reporting itself degraded — that arrives in
* {@see Health::$status}, and checking health is exactly when you want the
* diagnosis rather than an exception. Network failures still throw.
*/
public function health(): Health
{
[, $body] = $this->request('/health', [200, 503]);
return Health::fromArray($this->decodeJsonObject($body));
}
/**
* One chunk of at most {@see Client::MAX_BATCH} CEPs, in a single request.
*
* A blank CSV row means "not found": the API fills only the `cep` column
* and leaves the rest empty, precisely so it does not misalign whoever is
* consuming the batch in a spreadsheet.
*
* The API guarantees one row per requested CEP. Should that ever fail to
* hold — a proxy truncating, a cut response — the result is padded to the
* input length rather than returned short, because a shifted array
* silently pairs the wrong address with the wrong CEP.
*
* @param list<string> $ceps
*
* @return list<Address|null>
*/
private function lookupChunk(array $ceps): array
{
$query = http_build_query(['ceps' => implode(',', $ceps)]);
[, $body] = $this->request('/csv?' . $query);
$rows = [];
foreach ($this->parseCsv($body) as $row) {
$rows[] = trim($row['localidade'] ?? '') === '' ? null : Address::fromArray($row);
}
$wanted = count($ceps);
if (count($rows) !== $wanted) {
$rows = array_slice(array_pad($rows, $wanted, null), 0, $wanted);
}
return $rows;
}
/**
* Parse the CSV batch body into one associative row per record.
*
* `fgetcsv` over an in-memory stream is PHP's own RFC 4180 parser: it
* handles the quoted fields the API emits for values containing a comma,
* the `""` escape inside them, and a quoted field spanning a newline —
* which splitting on `\n` and calling `str_getcsv` per line would not.
*
* The empty `$escape` argument is required for RFC 4180: PHP's historical
* default treats a backslash as an escape character, which would swallow
* the closing quote of a field ending in one.
*
* @return list<array<string, string>>
*/
private function parseCsv(string $body): array
{
$text = $body;
if (str_starts_with($text, "\xEF\xBB\xBF")) {
$text = substr($text, 3);
}
$stream = fopen('php://temp', 'r+');
if ($stream === false) {
throw new InvalidResponseException('could not open a stream to parse the CSV');
}
try {
fwrite($stream, $text);
rewind($stream);
$header = fgetcsv($stream, null, ',', '"', '');
if (!is_array($header) || $header === [null]) {
throw new InvalidResponseException(
'the batch response has no CSV header row: '
. var_export(substr($body, 0, 120), true),
);
}
$header = array_map(static fn ($name): string => trim((string) $name), $header);
$width = count($header);
$rows = [];
while (($record = fgetcsv($stream, null, ',', '"', '')) !== false) {
if (!is_array($record) || $record === [null]) {
continue;
}
$record = array_slice(array_pad($record, $width, ''), 0, $width);
$rows[] = array_combine(
$header,
array_map(static fn ($value): string => (string) ($value ?? ''), $record),
);
}
return $rows;
} finally {
fclose($stream);
}
}
/**
* Decode a JSON object body, or throw {@see InvalidResponseException}.
*
* @return array<string, mixed>
*/
private function decodeJsonObject(string $body): array
{
try {
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new InvalidResponseException(
'response is not valid JSON — something between you and the API is '
. 'answering instead of it: ' . var_export(substr($body, 0, 120), true),
0,
$exception,
);
}
if (!is_array($data) || (array_is_list($data) && $data !== [])) {
throw new InvalidResponseException(
'expected a JSON object, got ' . get_debug_type($data),
);
}
return $data;
}
/**
* Pull `mensagem` out of an error body, falling back to a default.
*/
private static function errorMessage(string $body, string $fallback): string
{
$data = json_decode($body, true);
if (is_array($data) && isset($data['mensagem']) && is_scalar($data['mensagem'])) {
$message = (string) $data['mensagem'];
if ($message !== '') {
return $message;
}
}
return $fallback;
}
/**
* Parse the `Retry-After` header in both RFC 9110 forms.
*
* It may arrive as a number of seconds (`"120"`) or as an HTTP date
* (`"Wed, 21 Oct 2026 07:28:00 GMT"`). Handling only the first is the
* common bug, and it makes a client ignore exactly the wait request that
* matters most.
*/
private static function retryAfterSeconds(?string $value): ?float
{
if ($value === null) {
return null;
}
$value = trim($value);
if ($value === '') {
return null;
}
if (is_numeric($value)) {
return max(0.0, (float) $value);
}
$target = strtotime($value);
if ($target === false) {
return null;
}
return max(0.0, (float) $target - microtime(true));
}
/**
* How long to wait before the next attempt.
*
* The server's `Retry-After` wins over the local calculation when present:
* it knows when it will be back, we are guessing. It still goes through
* the `backoffMax` cap. Otherwise the wait is drawn from `[0, ceiling]`
* rather than being the ceiling — see `$backoffBase` on why that matters.
*/
private function backoff(int $attempt, ?float $retryAfter): float
{
if ($retryAfter !== null) {
return min($retryAfter, $this->backoffMax);
}
$ceiling = min($this->backoffBase * (2 ** ($attempt - 1)), $this->backoffMax);
return (mt_rand() / mt_getrandmax()) * $ceiling;
}
/**
* Perform the GET, with retries. Returns `[status, body]`.
*
* *Definitive* error statuses (400, 404) are returned normally, for the
* caller to turn into the right exception — only transient failures become
* exceptions here, and only once the attempts are exhausted.
*
* @param list<int> $acceptedStatus
*
* @return array{0: int, 1: string}
*/
private function request(string $path, array $acceptedStatus = [200]): array
{
$url = $this->baseUrl . $path;
$headers = [
'User-Agent' => $this->userAgent,
'Accept' => 'application/json, text/csv;q=0.9, */*;q=0.1',
'Accept-Encoding' => 'identity',
];
$deadline = $this->totalTimeout === null ? null : self::monotonic() + $this->totalTimeout;
$lastStatus = null;
$lastRetryAfter = null;
$lastNetworkError = null;
for ($attempt = 1; $attempt <= $this->maxAttempts; $attempt++) {
if ($deadline !== null && self::monotonic() >= $deadline) {
break;
}
try {
$response = $this->transport->get($url, $headers, $this->timeout);
if (
in_array($response->status, $acceptedStatus, true)
|| !in_array($response->status, self::RETRIABLE_STATUS, true)
) {
return [$response->status, $response->body];
}
$lastStatus = $response->status;
$lastRetryAfter = self::retryAfterSeconds($response->header('Retry-After'));
$lastNetworkError = null;
} catch (NetworkException $exception) {
$lastNetworkError = $exception;
$lastStatus = null;
$lastRetryAfter = null;
}
if ($attempt === $this->maxAttempts) {
break;
}
$wait = $this->backoff($attempt, $lastRetryAfter);
if ($deadline !== null) {
$remaining = $deadline - self::monotonic();
if ($remaining <= 0) {
break;
}
$wait = min($wait, $remaining);
}
if ($wait > 0) {
usleep((int) round($wait * 1_000_000));
}
}
$this->fail($url, $lastStatus, $lastRetryAfter, $lastNetworkError);
}
/**
* Turn the last failure into the exception that says what to do about it.
*/
private function fail(
string $url,
?int $status,
?float $retryAfter,
?NetworkException $networkError,
): never {
$attempts = sprintf('(after %d attempt(s))', $this->maxAttempts);
if ($status === 429) {
throw new RateLimitedException(
sprintf('rate limit reached at %s %s', $url, $attempts),
$retryAfter,
);
}
if ($status !== null) {
throw new ServerErrorException(
sprintf('the API answered %d at %s %s', $status, $url, $attempts),
$status,
);
}
throw new NetworkException(
sprintf(
'could not reach %s %s: %s',
$url,
$attempts,
$networkError?->getMessage() ?? 'the call ran out of time before any attempt',
),
0,
$networkError,
);
}
private static function monotonic(): float
{
return hrtime(true) / 1_000_000_000;
}
}
|