Config added for the cloud comunication 50/193950/1
authorMichal Skorupinski <m.skorupinsk@samsung.com>
Wed, 21 Nov 2018 19:08:12 +0000 (20:08 +0100)
committerMichal Skorupinski <m.skorupinsk@samsung.com>
Wed, 21 Nov 2018 19:08:12 +0000 (20:08 +0100)
Change-Id: Ie4d440e30d819fb130afc99dd3a192a4fb1d81b3
Signed-off-by: Michal Skorupinski <m.skorupinsk@samsung.com>
inc/cloud/http_request.h
src/cloud/cloud_lap_request.c
src/cloud/cloud_request.c
src/cloud/http_request.c

index ddaf958..d81065c 100644 (file)
@@ -38,4 +38,6 @@ int http_request_get(const char *url, char **response, long *response_code);
  */
 int http_request_post(const char *url, const char *json, char **response, long *response_code);
 
-#endif
\ No newline at end of file
+char *http_request_get_url(char *default_api, char *api_key);
+
+#endif
index 54ca712..524c2f3 100644 (file)
 #include <glib.h>
 #include <gio/gio.h>
 #include <string.h>
+#include <stdlib.h>
 #include "log.h"
 
-#define BASE_URL "http://son.tizen.online"
 #define PATH_API_LAP "/api/lap"
+#define CONFIG_LAP_API_KEY "lap"
 
 typedef struct {
        char *ap_mac;
@@ -130,7 +131,13 @@ static void lap_api_post_task_thread_cb(GTask *task, gpointer source_object, gpo
        lap_api_post_request_response_t *response = g_new0(lap_api_post_request_response_t, 1);
 
        char *json = lap_info_serializer_serialize(context->lap);
-       int retval = http_request_post(BASE_URL""PATH_API_LAP, json, &(response->response_msg), &(response->response_code));
+
+       char *url = http_request_get_url(PATH_API_LAP, CONFIG_LAP_API_KEY);
+
+       int retval = http_request_post(url, json, &(response->response_msg), &(response->response_code));
+
+       _D("URL FROM CONFIG (LAP POST): %s", url);
+       free(url);
        g_free(json);
 
        if (retval != 0) {
@@ -197,10 +204,15 @@ static void lap_api_get_task_thread_cb(GTask *task, gpointer source_object, gpoi
 
        lap_api_get_request_response_t *response = g_new0(lap_api_get_request_response_t, 1);
        char *response_json = NULL;
+       char *url_with_api = http_request_get_url(PATH_API_LAP, CONFIG_LAP_API_KEY);
 
-       GString *url = g_string_new(BASE_URL""PATH_API_LAP"?apMac=");
+       GString *url = g_string_new(url_with_api);
+       g_string_append(url, "?apMac=");
        g_string_append(url, context->ap_mac);
 
+       free(url_with_api);
+       _D("URL FROM CONFIG (LAP GET): %s", url);
+
        int retval = http_request_get(url->str, &response_json, &(response->response_code));
        g_string_free(url, TRUE);
 
index 90777d1..243b78f 100644 (file)
 #include <glib.h>
 #include <gio/gio.h>
 #include <string.h>
+#include <stdlib.h>
 #include "log.h"
 
-#define BASE_URL "http://son.tizen.online"
 #define PATH_API_RACING "/api/racing"
+#define CONFIG_CAR_API_KEY "car"
 
 typedef struct {
     char *ap_mac;
@@ -135,9 +136,15 @@ static void car_api_post_task_thread_cb(GTask *task, gpointer source_object, gpo
     car_api_post_request_response_t *response = g_new0(car_api_post_request_response_t, 1);
 
     char *json = car_info_serializer_serialize(context->car);
-    int retval = http_request_post(BASE_URL""PATH_API_RACING, json, &(response->response_msg), &(response->response_code));
+
+    char *url = http_request_get_url(PATH_API_RACING, CONFIG_CAR_API_KEY);
+
+    int retval = http_request_post(url, json, &(response->response_msg), &(response->response_code));
     g_free(json);
 
+    _D("URL FROM CONFIG (RACING POST): %s", url);
+    free(url);
+
     if (retval != 0) {
         GError *err = g_error_new(G_ERROR_DOMAIN, retval, "http_request_post failed!");
         g_task_return_error(task, err);
@@ -203,9 +210,15 @@ static void car_api_get_task_thread_cb(GTask *task, gpointer source_object, gpoi
     car_api_get_request_response_t *response = g_new0(car_api_get_request_response_t, 1);
     char *response_json = NULL;
 
-    GString *url = g_string_new(BASE_URL""PATH_API_RACING"?apMac=");
+    char *url_with_api = http_request_get_url(PATH_API_RACING, CONFIG_CAR_API_KEY);
+
+    GString *url = g_string_new(url_with_api);
+    g_string_append(url, "?apMac=");
     g_string_append(url, context->ap_mac);
 
+    free(url_with_api);
+    _D("URL FROM CONFIG (RACING GET): %s", url);
+
     int retval = http_request_get(url->str, &response_json, &(response->response_code));
     g_string_free(url, TRUE);
 
index 1780698..7618966 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <curl/curl.h>
+#include <stdbool.h>
 #include "log.h"
+#include "config.h"
+
+#define BASE_URL "http://son.tizen.online"
+#define CONFIG_CLOUD_GROUP "cloud"
+#define CONFIG_URL "url"
 
 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data);
 
@@ -133,3 +139,21 @@ static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
 
     return real_size;
 }
+
+char *http_request_get_url(char *default_api, char *api_key)
+{
+       char *url;
+       char *api;
+       char *url_with_api = calloc(PATH_MAX, sizeof(char));
+
+       bool modified = config_get_string_or_set_default(CONFIG_CLOUD_GROUP, CONFIG_URL, BASE_URL, &url);
+       modified |= config_get_string_or_set_default(CONFIG_CLOUD_GROUP, api_key, default_api, &api);
+
+       snprintf(url_with_api, PATH_MAX, "%s%s", url, api);
+
+       if (modified) {
+               config_save();
+       }
+
+       return url_with_api;
+}