HTTP Client
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.
1
/// Init HTTP client
2
/// \param provider Remote API URL, port, specific header and certificate if needed by the client
3
/// \param response_payload_cb Function callback to call when data is ready to be parsed
4
/// \return \c VTC_SUCCESS on success, otherwise error depends on implementation
5
ret_code_t
6
http_init(const provider_info_t *provider,
7
size_t (*response_payload_cb)(char *chunk,
8
size_t chunk_size));
9
​
10
/// Close/deinit the client
11
void
12
http_close(void);
Copied!
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

1
/// HTTP GET request
2
/// \param provider pointer to provider (url, port..)
3
/// \param relative_path path to append to the provider base URL
4
/// \param headers Headers string, each one must have the format "key:value\n\r"
5
/// \param response_code HTTP response code
6
/// \return error codes:
7
/// - \c VTC_SUCCESS on success
8
/// - \c VTC_ERROR_OFFLINE if offline
9
/// - \c VTC_HTTP_ERROR on HTTP error: check response_code.
10
ret_code_t
11
http_get(const provider_info_t *provider,
12
const char *relative_path,
13
const char *headers,
14
uint32_t *response_code);
15
​
16
/// HTTP POST request
17
/// \param provider pointer to provider (url, port..)
18
/// \param relative_path path to append to the provider base URL
19
/// \param headers Headers string, each one must have the format "key:value\n\r"
20
/// \param body HTTP body
21
/// \param body_size size of \c body array
22
/// \param response_code HTTP response code
23
/// \return error codes:
24
/// - \c VTC_SUCCESS on success
25
/// - \c VTC_ERROR_OFFLINE if offline
26
/// - \c VTC_HTTP_ERROR on HTTP error: check response_code.
27
ret_code_t
28
http_post(const provider_info_t *provider,
29
const char *relative_path,
30
char *headers,
31
const char *body,
32
size_t body_size,
33
uint32_t *response_code);
Copied!
Some important information to keep in mind:
    The full URL is a concatenation of provider->url and relative_path.
    Headers are key-value pairs separated by \r , \n or both. The format of the key-value pair is as follow key:value\n\r.
    If the HTTP call fails, the HTTP response code must be set in response_code and the function must respond VTC_HTTP_ERROR.
    Once the full response is ready to be parsed, you will need to call the callback stored during the initialization of the module with a pointer to the raw buffer and its size: response_payload_cb(response_data, response_length);
Now that we are able to receive and send data using a configured provider, we are going to build transactions that will soon be sent on the blockchain. To do so, we will need to handle events.
Here is the link to our (partial) implementation of the above step.
For full implementation, you can take a look at our example for Unix and the ESP32, both using different HTTP clients πŸ˜‰
Last modified 3mo ago