-
Notifications
You must be signed in to change notification settings - Fork 53
RESTful calls from Silgy
Jurek Muszyński edited this page May 15, 2019
·
32 revisions
Silgy provides a simple RESTful calls facility. A couple of macros handle JSON objects and HTTP calls.
REST_HEADERS_RESET;
REST_HEADER_SET("apiConsumerId", "123456789");
REST_HEADER_SET("Authorization", "Bearer v2-6998a852-1521-4745-a1f3-e55341092e9f");
JSON json_req={0};
JSON json_res={0};
JSON_ADD_STR(json_req, "accountNo", "987654321");
if ( !CALL_REST(json_req, json_res, "POST", "example.com:8888/api/v1.0/accounts/getAccount") )
{
ERR("getAccount call failed");
OUT("getAccount call failed");
return ERR_REMOTE_CALL;
}
else if ( !CALL_REST_STATUS_OK )
{
WAR("getAccount status %d", CALL_REST_STATUS);
OUT("<p style=\"font-family:monospace; font-size:10pt; white-space:pre;\">%s</p>", JSON_TO_STRING_PRETTY(json_res));
return ERR_REMOTE_CALL_STATUS;
}
// OK
OUT("<p>Name: %s</p>", JSON_GET_STR(json_res, "name"));
OUT("<p>Balance: %.2f</p>", JSON_GET_FLOAT(json_res, "balance"));
CALL_REST and most of the JSON_* macros have two versions:
- By default they add ampersand to JSON arguments to make coding easier – the code may then look like in the above example.
- If JSON_NO_AUTO_AMPERSANDS compilation option is set, ampersands are not added, which may be preferred for seasoned C coders and/or there is a frequent JSON arguments passing to functions.
-
CALL_REST may block for up to RESTTimeout (in milliseconds). If not set, then CALL_REST_DEFAULT_TIMEOUT is used, which is currently 1000 ms.
-
Maximum JSON string length is APP_JSON_BUFSIZE-1, by default 64 kB.
-
Maximum JSON elements in one JSON structure is APP_JSON_MAX_ELEMS, by default 10.
-
Maximum JSON nesting levels is APP_JSON_MAX_LEVELS, by default 4.
-
There is JSON_POOL_SIZE (currently 1000) for JSON sub-records internal storage.
All the above can be in/de-creased, depending on your needs and available memory.