|
libcmutils 0.6.5
Multi-platform C99 utility library
|
A JSON layer over CMUTIL_HttpClient. More...
Data Fields | |
| CMUTIL_HttpClient | base |
| CMUTIL_Json *(* | Get )(CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri) |
| Perform a GET request and parse the response as JSON. | |
| CMUTIL_Json *(* | Post )(CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri, CMUTIL_Json *data) |
| Perform a POST request with a JSON body. | |
| CMBool(* | Put )(CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri, CMUTIL_Json *data) |
| Perform a PUT request with a JSON body, discarding the response. | |
| void(* | Delete )(CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri) |
| Perform a DELETE request, discarding the response. | |
| void(* | SetTimeout )(CMUTIL_RestClient *client, long timeout) |
| Set the timeout applied to every REST request. | |
| int(* | GetStatus )(const CMUTIL_RestClient *client) |
| The HTTP status of the most recent request on this client. | |
A JSON layer over CMUTIL_HttpClient.
A REST client serializes the request body from a CMUTIL_Json, parses the response body back into one, and adds the two JSON content negotiation headers when the caller did not supply them.
The first member is a complete CMUTIL_HttpClient, so a REST client can be used wherever one is expected:
CMUTIL_RestClient *rest = CMUTIL_RestClientCreate("https://api.example.com");
CMCall(&rest->base, SetVerify, CMTrue, CMTrue);
CMUTIL_Json *user = CMCall(rest, Get, NULL, "/v1/users/42");
...
CMUTIL_JsonDestroy(user);
CMCall(&rest->base, Destroy); // destroys the REST client too
All four REST methods report the HTTP status of the request they performed through GetStatus, which stays zero when the request never reached a response - a connection failure, a timeout or a malformed reply. The status belongs to the most recent request made through this client, so one client performs one request at a time.
| CMUTIL_HttpClient CMUTIL_RestClient::base |
The underlying HTTP client. Use it for TLS settings, keep-alive, requests that are not JSON, and to destroy this object.
| CMUTIL_Json *(* CMUTIL_RestClient::Get) (CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri) |
Perform a GET request and parse the response as JSON.
| client | This REST client object. |
| headers | Extra request headers, or NULL. The map is only read. |
| uri | Request URI, relative to the prefix given at creation. |
CMUTIL_JsonDestroy. NULL when the request failed, the response carried no body, or the body was not JSON - check GetStatus to tell those apart. | CMUTIL_Json *(* CMUTIL_RestClient::Post) (CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri, CMUTIL_Json *data) |
Perform a POST request with a JSON body.
| client | This REST client object. |
| headers | Extra request headers, or NULL. The map is only read. |
| uri | Request URI, relative to the prefix given at creation. |
| data | The request body. Ownership stays with the caller. |
CMUTIL_JsonDestroy, or NULL. See Get. | CMBool(* CMUTIL_RestClient::Put) (CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri, CMUTIL_Json *data) |
Perform a PUT request with a JSON body, discarding the response.
| client | This REST client object. |
| headers | Extra request headers, or NULL. The map is only read. |
| uri | Request URI, relative to the prefix given at creation. |
| data | The request body. Ownership stays with the caller. |
| void(* CMUTIL_RestClient::Delete) (CMUTIL_RestClient *client, CMUTIL_Map *headers, const char *uri) |
Perform a DELETE request, discarding the response.
| client | This REST client object. |
| headers | Extra request headers, or NULL. The map is only read. |
| uri | Request URI, relative to the prefix given at creation. |
| void(* CMUTIL_RestClient::SetTimeout) (CMUTIL_RestClient *client, long timeout) |
Set the timeout applied to every REST request.
The REST methods take no timeout of their own; this is it. A new client starts at CMUTIL_REST_DEFAULT_TIMEOUT.
| client | This REST client object. |
| timeout | Timeout in milliseconds. |
| int(* CMUTIL_RestClient::GetStatus) (const CMUTIL_RestClient *client) |
The HTTP status of the most recent request on this client.
| client | This REST client object. |