Building the API Layer
Explore building a thin API layer in Elixir that leverages GenServer to isolate state, handle errors, and validate data. Learn to start and manage servers, compose functions for quiz management, and understand the role of the API layer in maintaining clear boundaries and simplifying client-server interactions.
Our API layer will name the concepts of the GenServer and smooth out some of the rough edges. We’ll build a lightweight API that uses the GenServer module to do the following:
-
starts
-
calls
-
casts
Declaring the module and aliases
The first step is to do the typical imports we need. In /lib/mastery.ex, we’ll delete the default implementation and set up the aliases we need:
If possible, we’d build a service layer where the only functions we need are in the service layer. However, we’ll have to compromise on that and use the following headings:
-
We have to manage the validations, so we’ll add those aliases as well. Validation belongs here because we want to reduce the need for dealing with the uncertainty of the ...