Making an API Call
Learn how to communicate with external APIs.
We'll cover the following
Understanding the dependencies
We can’t possibly store all data across users in in-memory objects till the end of time. That would never scale. We will also run into some very complex data structures if we start storing diverse spheres of information all in one place. This is where we need to rely on our system design skills to build distributed systems. Once we know how to build a server to process input, apply business logic, and return meaningful output, we must understand how to process data.
Distributed systems would imply that multiple smaller entities are all responsible for their focused scope. We would therefore then need to talk to each smaller entity whenever we need help with anything related to a resource owned by that entity—for example, a usersInfoServ
microservice might perform basic CRUD operations on a user information database, while a postsInfoServ
microservice might do the same for a posts database. Now both of these are dependent on a database. That is an external dependency.
Now, someone orchestrating across these two to be able to fetch a user’s name and their feed would have to make API calls to both these services, stitch their responses together, and then serve up the data. This would make both these services external dependencies for this orchestrator.
So, what can be an external dependency?
It can be other services responsible for specific functions.
It can be data stores, such as databases, file systems, or caches.
It can be queueing mechanisms that allow us to process requests in an orderly and controlled fashion.
Making an HTTP REST API call
For our first kind of dependency, a simple HTTP call would suffice. For our data dependency, we will use a public, free-to-use fake API called FakerAPI. This means we save time and effort and jump straight into integration and testing our code instead of worrying about data. Let’s look at how we can add that to our server.
First, let’s edit ListUsersHandler
and make it call the FakerAPI “persons” resource. We will print the response body to look at the API’s response JSON structure.
Get hands-on with 1400+ tech skills courses.