summaryrefslogtreecommitdiff
path: root/libgs/src/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'libgs/src/client.c')
-rw-r--r--libgs/src/client.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/libgs/src/client.c b/libgs/src/client.c
new file mode 100644
index 0000000..7c0b62c
--- /dev/null
+++ b/libgs/src/client.c
@@ -0,0 +1,72 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <err.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "client.h"
+#include "http.h"
+#include "log.h"
+#include "string-util.h"
+
+GSClient *gs_client_new(const char *server, const char *client_id,
+ const char *client_secret)
+{
+ GSClient *c;
+
+ c = calloc(1, sizeof(GSClient));
+ if (!c) {
+ err(1, "gs_new_client");
+ return NULL;
+ }
+
+ c->server = strdup(server);
+ c->client_id = strdup(client_id);
+ c->client_secret = strdup(client_secret);
+
+ return c;
+}
+
+void *gs_client_do_api(GSClient *c, int method, const char *url,
+ const char *req)
+{
+ char *resp;
+
+ size_t s = strlen(c->server) + strlen(url) + 1;
+ char full_url[s];
+ strlcpy(full_url, c->server, s);
+ strlcat(full_url, url, s);
+
+ switch (method) {
+ case 1:
+ resp = gs_http_get(full_url, c->access_token);
+ break;
+ case 2:
+ resp = gs_http_post(full_url, c->access_token, req);
+ break;
+ default:
+ gs_log(GS_WARNING, "gs_client_do_api", "invalid api method");
+ return NULL;
+ }
+
+ return resp;
+}
+
+void gs_client_set_token(GSClient *c, const char *access_token)
+{
+ c->access_token = strdup(access_token);
+}
+
+void gs_client_free(GSClient *c)
+{
+ if (!c)
+ return;
+ if (c->server)
+ free(c->server);
+ if (c->client_id)
+ free(c->client_id);
+ if (c->client_secret)
+ free(c->client_secret);
+ if (c->access_token)
+ free(c->access_token);
+}