HomeDevCentral

Provide an API module

Description

Provide an API module

Summary:
API replies in REST API tend to be:

  • JSON representations of objects from a datasource, or a calculation
  • Failures with relevant status code (400, 403, 404, 409, 500, etc.)

This module provides traits to consume values and get such API response.

It provides trait implementation for Diesel QueryResult, so you can do
something like this:

pub fn get_player(connection: DatabaseConnection, name: String) -> ApiJsonResponse<Player> {
    players
        .filter(username.eq(&name))
        .first::<Player>(&*connection)
        .into_json_response()
}

This will produce a JSON representation when the result is found,
a 404 error when no result is found, a 500 error if there is a database issue.

Reviewers: dereckson

Reviewed By: dereckson

Differential Revision: https://devcentral.nasqueron.org/D1663

Details

Provenance
derecksonAuthored on Sep 4 2018, 14:44
derecksonPushed on Sep 4 2018, 14:58
Reviewer
dereckson
Differential Revision
D1663: Provide an API module
Parents
rLF9316fcaa4669: Provide a database module
Branches
Unknown
Tags
Unknown