-
-
Notifications
You must be signed in to change notification settings - Fork 33
How to add a new endpoint
Endpoints are added via controllers, which handle routes. Let's look at /fort/get_data for an example: https://github.com/SapiensAnatis/DragaliaAPI/blob/develop/DragaliaAPI/Controllers/Dragalia/FortController.cs#L21
-
The class is marked with
[Route("fort")]
. This means that any request paths starting with/fort/
will be routed here. Most API paths have two components, and it is preferred to have one controller class responsible for one primary component, with methods to handle secondary components. -
The method is marked with
[HttpPost("fort")]
. This defines an endpoint which you can contact via POST (the game uses this method for virtually all of its requests). The method signature ispublic async Task<DragaliaResult>
.-
It is async because it has to do a database query, which is an operation that is not instant.
-
It returns
DragaliaResult
. For more info on this type see the Infrastructure page.
-
-
The actual method performs an asynchronous database query via the IFortRepository which it receives via dependency injection.
- It is preferred that methods perform database queries via use of the repository layer, rather than injecting ApiContext. This makes it possible to unit test controllers via the use of mock repositories, and allows any repository methods to be tested in isolation using an in-memory database.
- For methods implementing complicated logic, this should be extracted into a service which in turn calls the database layer as necessary. This allows for code to be reused and services are often easier to test than controllers.
These should always be done asynchronously.