summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-20 09:16:38 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-20 09:16:38 +0800
commitf5b387f8602b30d47841696cb0165aa88218f42a (patch)
treed5bbdea82c3d4b8b96d035421e7d28dbffd0f6ed /main.c
parentfad30f64dd061fb8c98fe64f6596e1785c8babeb (diff)
Rename index to main and move it up to root dir
Also log the downloaded data.
Diffstat (limited to 'main.c')
-rw-r--r--main.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..9304259
--- /dev/null
+++ b/main.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <string.h>
+#include <emscripten/fetch.h>
+
+static void handle_success(emscripten_fetch_t *fetch)
+{
+ printf("Finished downloading %llu bytes of %s from URL %s.\n",
+ fetch->numBytes, fetch->data, fetch->url);
+ emscripten_fetch_close(fetch);
+}
+
+static void handle_error(emscripten_fetch_t *fetch)
+{
+ printf("Downloading %s failed, HTTP failure status code: %d.\n",
+ fetch->url, fetch->status);
+ emscripten_fetch_close(fetch);
+}
+
+void getproducts(const char *token, const char *app_url)
+{
+ emscripten_fetch_attr_t attr;
+ emscripten_fetch_attr_init(&attr);
+ strcpy(attr.requestMethod, "GET");
+ attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
+ attr.onsuccess = handle_success;
+ attr.onerror = handle_error;
+ static const char *schema = "Bearer ";
+ char auth[strlen(schema) + strlen(token) + 1];
+ sprintf(auth, "%s%s", schema, token);
+ attr.requestHeaders = (const char *[]){ "Authorization", auth, NULL };
+ static const char *path = "/products";
+ char url[strlen(app_url) + strlen(path) + 1];
+ sprintf(url, "%s%s", app_url, path);
+ emscripten_fetch(&attr, url);
+}
+
+int main(int argc, char *argv[])
+{
+ EM_ASM(getToken());
+ return 0;
+}