Poll Data Services through APIs
Learn to poll any web API with Rust.
Web APIs
One of the staples of current web programming practices is a tendency to avoid monoliths in favor of small services, putting each one in charge of performing just one type of operation.
These servers have to communicate with each other through public APIs. Moreover, they typically use JSON to exchange data.
Therefore, to talk to any service, we need to know the following information:
- The API signature, which endpoint to poll, how to poll, what are they expecting in terms of body content, headers, HTTP methods, and so on.
- How to map the response to a custom struct so that we can access the data in our program.
- Server authorization and/or keys, especially if the servers have a cap on daily or monthly requests, or if their services involve a subscription of any kind.
The following sections are all examples of accessing web services with Rust.
CRUD database API
In the following examples, we use a dummy mock server that lets us test a CRUD approach to APIs.
CRUD stands for:
- Create: Creating a new record. This is usually achieved with a
POST
HTTP method. - Read: Reading one or many records. This is usually achieved with a
GET
HTTP method. - Update: Updating data for a record. This is usually achieved with a
PUT
HTTP method, although some APIs use theUPDATE
method instead. - Delete: Deleting a record. This is usually achieved with a
DELETE