diff options
author | turret <turret@duck.com> | 2023-12-18 14:06:19 -0600 |
---|---|---|
committer | turret <turret@duck.com> | 2023-12-18 14:06:19 -0600 |
commit | 42a5bb2806ff22a29b7d8d2d85c63d69cee24bba (patch) | |
tree | ba25b576da331ef2897d527e3d4f8b1790650b60 /net/api.c | |
parent | ce9e6392f16e6f8dd55e3cca8cd34f2869650418 (diff) | |
download | discord-bot-skeleton-42a5bb2806ff22a29b7d8d2d85c63d69cee24bba.tar.gz discord-bot-skeleton-42a5bb2806ff22a29b7d8d2d85c63d69cee24bba.tar.bz2 discord-bot-skeleton-42a5bb2806ff22a29b7d8d2d85c63d69cee24bba.zip |
net: get gateway url
basic api http_get function, (which the definition of will def. change
in the future)
uses libcurl to get the api /gateway to get the wss url, and assumes a
url in case of failure so we at least try authenticating
Diffstat (limited to 'net/api.c')
-rw-r--r-- | net/api.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/net/api.c b/net/api.c new file mode 100644 index 0000000..e664c42 --- /dev/null +++ b/net/api.c @@ -0,0 +1,43 @@ +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <curl/curl.h> + +extern char *token; + +int http_get(char *url) +{ + int pipefd[2]; + if(pipe(pipefd) < 0) + return -errno; + + FILE *write_end = fdopen(pipefd[1], "w"); + + int ret = pipefd[0]; + + CURL *job = curl_easy_init(); + if(job == 0) { + close(pipefd[0]); + ret = -1; + goto close_writepipe; + } + + curl_easy_setopt(job, CURLOPT_URL, url); + curl_easy_setopt(job, CURLOPT_WRITEDATA, write_end); + curl_easy_setopt(job, CURLOPT_HTTPHEADER, curl_slist_append(NULL, token)); + CURLcode res = curl_easy_perform(job); + + if(res > 0) { + ret = -res; + close(pipefd[0]); + goto cleanup; + } + +cleanup: + curl_easy_cleanup(job); +close_writepipe: + fclose(write_end); + return ret; +} |