From f6032cd02992dc35dcf4bc67a6fbe83eb78ad39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Mon, 19 Sep 2022 09:02:02 +0800 Subject: HTML is to be filled with app URL, API key & host JS then calls WASM, which in this example, tries to get products. --- pages/index.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'pages/index.c') 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 +#include #include -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); } -- cgit v1.2.3