summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-19 09:02:02 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2022-09-19 09:02:02 +0800
commitf6032cd02992dc35dcf4bc67a6fbe83eb78ad39e (patch)
treee581454e37cf9b257c590f94a592776ba7beebd2
parenta7b4ce7e5a00927bf833efb84cca45e0bb94a7cc (diff)
HTML is to be filled with app URL, API key & host
JS then calls WASM, which in this example, tries to get products.
-rw-r--r--index.html13
-rw-r--r--pages/index.c33
2 files changed, 42 insertions, 4 deletions
diff --git a/index.html b/index.html
index 71d09f5..1aecc6d 100644
--- a/index.html
+++ b/index.html
@@ -8,7 +8,16 @@
<body>
<script src="https://unpkg.com/@shopify/app-bridge@3"></script>
<script src="https://unpkg.com/@shopify/app-bridge-utils@3"></script>
- <script async type="text/javascript" src="js/authenticatedFetch.js"></script>
- <script async type="text/javascript" src="js/index.js"></script>
+ <script async type="text/javascript" src="%s/js/index.js"></script>
+ <script async>
+ Module.cwrap('index_getproducts', '',
+ ['string', 'string'])(await
+ window['app-bridge-utils']
+ .getSessionToken(window['app-bridge']
+ .createApp({
+ apiKey: '%s',
+ host: '%s'
+ })), '%s');
+ </script>
</body>
</html>
diff --git a/pages/index.c b/pages/index.c
index 08aa2d6..91d8471 100644
--- a/pages/index.c
+++ b/pages/index.c
@@ -1,6 +1,35 @@
+#include <stdio.h>
+#include <string.h>
#include <emscripten/fetch.h>
-int main(int argc, char *argv[])
+static void handle_success(emscripten_fetch_t *fetch)
{
- EM_ASM(authenticatedFetch("/products"));
+ printf("Finished downloading %llu bytes from URL %s.\n",
+ fetch->numBytes, 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 index_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);
}