Wrapping the Server in an API
Explore the process of creating an API layer around GenServer servers to isolate internal state and implementations in Elixir. Learn to build data validations to ensure consistent inputs, prevent errors, and maintain clean server code. Understand how to implement reusable validation functions that accumulate errors, enhancing the reliability of your Elixir applications.
The API layer has the following tasks to fulfill:
-
Insulate the server layer from inconsistent data and stitch together independent concepts from the individual
GenServerimplementations. -
Hide internal implementations from the user, such as the
Quizstruct we make available from ourQuizManagerserver.
Any implementation details from server layers or the functional cores will be off-limits.
Similarities with OOP
Though our internal details may be radically different, the API-wrapped server will share many characteristics of an OOP object:
-
It will hide implementation details, including state, behind an API of functions.
-
It will allow complex interactions between components with message passing and will allow convenient state tracking.
Before we dive into the API, we’ll need some validations ...