Providing a custom HTTP implementation for the Vertices SDK
Our header, your implementation
We provide functions signatures in vertices_http.h .
The SDK will call those functions when needed.
Client Init/DeInit
The http_init function is called on Vertices SDK initialization.
/// Init HTTP client
/// \param provider Remote API URL, port, specific header and certificate if needed by the client
/// \param response_payload_cb Function callback to call when data is ready to be parsed
/// \return \c VTC_SUCCESS on success, otherwise error depends on implementation
ret_code_t
http_init(const provider_info_t *provider,
size_t (*response_payload_cb)(char *chunk,
size_t chunk_size));
/// Close/deinit the client
void
http_close(void);
The init function is simple: all you have to do is to init the HTTP client using the parameters. The URL, port, specific headers, and certificates are passed with the provider parameter.
The callback function reponse_payload_cb must be kept locally as we will need to call it whenever a response is received after a GET or a POST method call.
The callback
The callback function will be called whenever an HTTP response body is ready to be parsed: a pointer to the received bytes chunk and the size chunk_size will be provided to the Vertices SDK when calling the callback. The buffer can be a pointer allocated by the underlying HTTP client.
GET/POST methods
/// HTTP GET request
/// \param provider pointer to provider (url, port..)
/// \param relative_path path to append to the provider base URL
/// \param headers Headers string, each one must have the format "key:value\n\r"
/// \param response_code HTTP response code
/// \return error codes:
/// - \c VTC_SUCCESS on success
/// - \c VTC_ERROR_OFFLINE if offline
/// - \c VTC_HTTP_ERROR on HTTP error: check response_code.
ret_code_t
http_get(const provider_info_t *provider,
const char *relative_path,
const char *headers,
uint32_t *response_code);
/// HTTP POST request
/// \param provider pointer to provider (url, port..)
/// \param relative_path path to append to the provider base URL
/// \param headers Headers string, each one must have the format "key:value\n\r"
/// \param body HTTP body
/// \param body_size size of \c body array
/// \param response_code HTTP response code
/// \return error codes:
/// - \c VTC_SUCCESS on success
/// - \c VTC_ERROR_OFFLINE if offline
/// - \c VTC_HTTP_ERROR on HTTP error: check response_code.
ret_code_t
http_post(const provider_info_t *provider,
const char *relative_path,
char *headers,
const char *body,
size_t body_size,
uint32_t *response_code);