Skip to content

How to add a new endpoint

Jay Malhotra edited this page Feb 19, 2023 · 7 revisions

Overview

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

  1. 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.

  2. 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 is public async Task<DragaliaResult>.

    1. It is async because it has to do a database query, which is an operation that is not instant.

    2. It returns DragaliaResult. For more info on this type see the Infrastructure page.

  3. The actual method performs an asynchronous database query via the IFortRepository which it receives via dependency injection.

    1. 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.
    2. 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.

Database queries

These should always be done asynchronously.

Clone this wiki locally