Api Client

ApiClient is the class which extends oatpp::web::client::ApiClient. It provides convenient Retrofit-like interface over the oatpp::web::client::RequestExecutor.

All API-Calls generated by API_CALL and API_CALL_ASYNC macros in the end call the oatpp::web::client::RequestExecutor::execute(...) and oatpp::web::client::RequestExecutor::executeAsync(...) virtual methods respectively which basically gives the possibility to substitute your own RequestExecutor and use whatever library for HTTP Client API calls.

ApiClient code generation section must begin with #include OATPP_CODEGEN_BEGIN(ApiClient) and must be closed with #include OATPP_CODEGEN_END(ApiClient).
Do not forget to close code generation section in order to avoid macro conflicts later in the code!

#include "oatpp/web/client/ApiClient.hpp"
#include "oatpp/core/macro/codegen.hpp"

#include OATPP_CODEGEN_BEGIN(ApiClient) ///< Begin code-gen section

class MyApiClient : public oatpp::web::client::ApiClient {

  API_CLIENT_INIT(MyApiClient) ///< generate constructor

  API_CALL("GET", "/resource", getResource) ///< Example of API call
  API_CALL_ASYNC("GET", "/resource", getResourceAsync) ///< Example of Async API call

};

#include OATPP_CODEGEN_END(ApiClient) ///< End code-gen section

Parameters

API_CALL, API_CALL_ASYNC macros have the following params:

API_CALL("<http-method>", "<path>", <method-name>, <optional param-mappings>)

Possible param mappings

  • Map method parameter to header:
    HEADER(<data-type>, <param-name>, "<optional header-name>")
    
  • Map method parameter to path variable:
    PATH(<data-type>, <param-name>, "<optional path-variable-name>")
    
  • Map method parameter to path's query parameter:
    QUERY(<data-type>, <param-name>, "<optional path-variable-name>")
    
  • Map method parameter to request's body. Data type must be String:
    BODY_STRING(String, <param-name>)
    
  • Map method parameter to request's body. DTO will be serialized using ObjectMapper given to the ApiClient constructor:
    BODY_DTO(<DTO-class>::ObjectWrapper, <param-name>)
    

Examples of code