Clean Flutter Network Architecture Using Dio (2022) | Part 4 — Api Endpoint
Missed out on previous part? Check out Part 3 — Interceptors
Api Endpoint
Create a file called api_endpoint.dart
This file contains:
- Enums for different endpoint collections. For e.g. for
StudentEndpoint
- If we want to make a request to
/students
we will use BASE - If we want to query specific resource like
/students/id
we will use BY_ID - If we want a sub collection like
/students/requests
we will use REQUESTS
2. ApiEndpoint
class which contains:
- The
baseUrl
field, which should be passed the value of the base url of API e.g. www.some-api.com/v1 - Static methods for each REST API collection.
Static methods return the path based on the enum and parameters passed. For example:
- For getting a specific student i.e.
/students/17855
we call
ApiEndpoint.students(StudentEndpoint.BY_ERP, erp: 17855)
- For getting a student’s activities i.e.
/students/17855/saved-activities
we call
ApiEndpoint.students(
StudentEndpoint.SAVED_ACTIVITIES_BASE,
erp: 17855,
)
- For
students/17855/saved-activities/3
we call
ApiEndpoint.students(
StudentEndpoint.SAVED_ACTIVITIES_BY_ID,
erp: 17855,
extendedResourceId: 3,
)
Summary
We have setup a flexible and scalable solution for managing our endpoints without involving Strings. We will now move on to the final part of this guide that glues together all of this layer using an Api Service.
I encourage you guys to share your views regarding this article and help me improve on any lacking areas. If you liked this article, then please clap on this and share it with your network.
Read on Part 5: Api Service