Skip to content

Clients

The last thing outside your program, and the least cooperative: a service you do not own, on a network that fails differently from a disk, returning JSON somebody else designed. This section is where errors, data and testing all have to hold at once.

These pages are stubs — outlines waiting for a runnable example. See the Errors section for what that means and how a page graduates. These are also the pages furthest from this library's promise that every claim is printed by a program CI runs: an example may not reach the network, so the finished pages will have to earn their output some other way — which is the subject of two of them.

Lesson Level What it will teach
An HTTP request 201 The smallest real GET, and the blocking-or-async decision you make before writing it
Deserializing a response 201 JSON you did not design: a struct per level, or one pointer straight to the field you wanted
Injecting the base URL 201 → 301 The constant that makes a client untestable, and the parameter that does not
Mocking a server 301 What a fake server proves, what it cannot, and the test that passes against an API that no longer exists
Units are types 201 A number whose meaning lives in a comment, versus one the compiler will not let you add to a different kind

Po polsku

Ten dział zaczyna się tam, gdzie kończy się kontrola nad własnym programem: po drugiej stronie jest cudzy serwis, sieć zawodząca inaczej niż dysk (nie „nie ma pliku”, tylko timeout, 503 i odpowiedź, która przyszła, ale w innym kształcie niż w dokumentacji) oraz JSON zaprojektowany przez kogoś innego — dlatego dopiero tutaj obsługa błędów, dane i testowanie muszą zadziałać naraz. Druga rzecz, o której warto wiedzieć od razu: te strony to na razie zalążki (stubs), dokładnie w tym sensie, co zalążek artykułu na Wikipedii, i akurat w tym dziale najtrudniej je dokończyć — obietnica tej biblioteki, że każdy wynik wypisał program uruchamiany przez CI, kłóci się z przykładem chodzącym do sieci, bo taki przykład nie jest powtarzalny. Dowód poprawności trzeba więc zdobyć inaczej i to jest temat dwóch lekcji z tabeli powyżej: wstrzyknięcia adresu bazowego oraz atrapy serwera.

Szukaj po polsku: klient HTTP w Ruscie · obsługa błędów sieciowych · mockowanie serwera w testach · rust reqwest example · rust mock http server test