2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include "imv_swima_rest.h"
21 typedef struct private_imv_swima_rest_t private_imv_swima_rest_t
;
24 * Private data of an imv_swima_rest_t object.
26 struct private_imv_swima_rest_t
{
29 * Public members of imv_swima_rest_t
31 imv_swima_rest_t
public;
39 * Timeout of REST API connection
45 #define HTTP_STATUS_CODE_PRECONDITION_FAILED 412
47 METHOD(imv_swima_rest_t
, post
, status_t
,
48 private_imv_swima_rest_t
*this, char *command
, json_object
*jrequest
,
49 json_object
**jresponse
)
51 struct json_tokener
*tokener
;
52 chunk_t data
, response
= chunk_empty
;
57 if (asprintf(&uri
, "%s%s",this->uri
, command
) < 0)
61 data
= chunk_from_str((char*)json_object_to_json_string(jrequest
));
63 status
= lib
->fetcher
->fetch(lib
->fetcher
, uri
, &response
,
64 FETCH_TIMEOUT
, this->timeout
,
65 FETCH_REQUEST_DATA
, data
,
66 FETCH_REQUEST_TYPE
, "application/json; charset=utf-8",
67 FETCH_REQUEST_HEADER
, "Accept: application/json",
68 FETCH_REQUEST_HEADER
, "Expect:",
69 FETCH_RESPONSE_CODE
, &code
,
73 if (status
!= SUCCESS
)
75 if (code
!= HTTP_STATUS_CODE_PRECONDITION_FAILED
|| !response
.ptr
)
77 DBG2(DBG_IMV
, "REST http request failed with status code: %d", code
);
84 /* Parse HTTP response into a JSON object */
85 tokener
= json_tokener_new();
86 *jresponse
= json_tokener_parse_ex(tokener
, response
.ptr
,
88 json_tokener_free(tokener
);
98 METHOD(imv_swima_rest_t
, destroy
, void,
99 private_imv_swima_rest_t
*this)
106 * Described in header.
108 imv_swima_rest_t
*imv_swima_rest_create(char *uri
, u_int timeout
)
110 private_imv_swima_rest_t
*this;
121 return &this->public;