aboutsummaryrefslogtreecommitdiff
path: root/restapi/im.c
diff options
context:
space:
mode:
authorJulien Dessaux2019-08-04 11:03:38 +0200
committerJulien Dessaux2019-08-05 13:28:51 +0200
commit1bec937076d8d40f94ff4603479f21bca2de6c61 (patch)
treed811f0155c09149e9ade2016e224d58b2fbf6d81 /restapi/im.c
parentAdded configuration file parsing (diff)
downloadrocket-cli-client-1bec937076d8d40f94ff4603479f21bca2de6c61.tar.gz
rocket-cli-client-1bec937076d8d40f94ff4603479f21bca2de6c61.tar.bz2
rocket-cli-client-1bec937076d8d40f94ff4603479f21bca2de6c61.zip
Added http lib and basic restapi with rocket_close_im binary
Diffstat (limited to '')
-rw-r--r--restapi/im.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/restapi/im.c b/restapi/im.c
new file mode 100644
index 0000000..6b9f75c
--- /dev/null
+++ b/restapi/im.c
@@ -0,0 +1,58 @@
+#include <cjson/cJSON.h>
+#include <curl/curl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "common/http.h"
+#include "im.h"
+
+#define LOGIN_ARG_PRE "{ \"username\": \""
+#define LOGIN_ARG_POST "\" }"
+
+char // returns 0 if ok, greater than 0 otherwise
+restapi_im_close(const char* username)
+{
+ char ret = 0;
+
+ size_t pre_len = strlen(LOGIN_ARG_PRE);
+ size_t user_len = strlen(username);
+ size_t post_len = strlen(LOGIN_ARG_POST);
+ char* login_args = malloc(pre_len + user_len + post_len + 1);
+ strcpy(login_args, LOGIN_ARG_PRE);
+ strcpy(login_args + pre_len, username);
+ strcpy(login_args + pre_len + user_len, LOGIN_ARG_POST);
+ http_add_header("Content-type", "application/json");
+ const char* buffer = http_post("/api/v1/im.close", login_args);
+ free(login_args);
+
+ if (buffer == NULL) {
+ fprintf(stderr, "Error while im_close, http post didn't return any data.\n");
+ return 1;
+ }
+
+ cJSON* json = cJSON_Parse(buffer);
+ if (json == NULL) {
+ const char *error_ptr = cJSON_GetErrorPtr();
+ if (error_ptr != NULL)
+ fprintf(stderr, "Json parsing error before: %s\n", error_ptr);
+ fprintf(stderr, "Error while im_close, couldn't parse json output :\n%s\n", buffer);
+ ret = 2;
+ goto login_json_cleanup;
+ }
+
+ const cJSON* status = cJSON_GetObjectItemCaseSensitive(json, "status");
+ if (cJSON_IsString(status) && status->valuestring != NULL && strcmp(status->valuestring, "success") == 0) {
+ printf("user %s removed\n", username);
+ } else {
+ const cJSON* msg = cJSON_GetObjectItemCaseSensitive(json, "message");
+ if (cJSON_IsString(msg) && msg->valuestring != NULL)
+ fprintf(stderr, "Error while im_close: %s\n", msg->valuestring);
+ else
+ fprintf(stderr, "Error while im_close.\n%s\n", buffer);
+ ret = 3;
+ }
+login_json_cleanup:
+ cJSON_Delete(json);
+ return ret;
+}