mardi 2 avril 2019

Generic API interface to avoid API explosion

I have a service in which there are many APIs. Each API takes the request parameters and just execute the SQL and responds with the values from SQL output. Each API differs with very little parameters. (e.g. API1 and API2 differs by only one condition. API3 is grouping on f1,f2 and API4 is grouping on f1,f2,f3). Also, response of each API differs by some paramters.

I am searching for some best practices to handle this kind of situations. I also went through JOOQ which can objectify the SQL.

// enum for API
API {
    GET_DETAILS_BY_ID("GET_DETAILS_BY_ID", REQUEST(ID), RESPONSE(NAME)),
    GET_DETAILS_BY_NAME("GET_DETAILS_BY_NAME", REQUEST(NAME), RESPONSE(ID));

    private String apiName;
    private APIRequest request;
    private APIResponse response;
}

If a client wants to call GetDetailsById then it would call getDetails(GET_DETAILS_BY_ID, new Request(1)). Interface in the service would map 1 to Id and execute the SQL

select name from student where id = <id>; 

and return name as API response.

I am thinking of making one generic API and parameters would have API enum and the request parameter values.

Any suggestions on this approach or pointers to best approach/practice would be helpful.

Aucun commentaire:

Enregistrer un commentaire