diff options
author | turret <turret@duck.com> | 2024-03-30 16:04:45 -0500 |
---|---|---|
committer | turret <turret@duck.com> | 2024-03-30 16:04:45 -0500 |
commit | 80a67b7d20393a29aa5d2cb92197f3381be7fd96 (patch) | |
tree | f0c313da5ef509e79e5067972edd91976513ce0e /net/api.c | |
parent | f01745a2ee84f11b8cc54e37c5f7f596184ab785 (diff) | |
download | discord-bot-skeleton-80a67b7d20393a29aa5d2cb92197f3381be7fd96.tar.gz discord-bot-skeleton-80a67b7d20393a29aa5d2cb92197f3381be7fd96.tar.bz2 discord-bot-skeleton-80a67b7d20393a29aa5d2cb92197f3381be7fd96.zip |
*: directory changes
since this project is a skeleton and not meant to clutter up the code
that will actually consume the bot, i've opted to consolidate the
majority of files under a single directory and minimise extra files
*: move code to util/
*: move include files to include/dbs/
net: consolidate net functions into single file
config: remove config
Diffstat (limited to 'net/api.c')
-rw-r--r-- | net/api.c | 87 |
1 files changed, 0 insertions, 87 deletions
diff --git a/net/api.c b/net/api.c deleted file mode 100644 index be36866..0000000 --- a/net/api.c +++ /dev/null @@ -1,87 +0,0 @@ -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include <curl/curl.h> - -#define __API_INTERNAL -#include <api.h> -#include <log.h> - -extern char *token; - -int http_request(HTTPMethod method, char *url, - struct curl_slist *_Nullable headers, char *writebuf, size_t bufsiz) -{ - int inputpipe[2]; - int outputpipe[2]; - - if(pipe(inputpipe) < 0) - return -(errno << 8); - if(pipe(outputpipe) < 0) - return -(errno << 8); - - if(writebuf && bufsiz > 0) - write(inputpipe[1], writebuf, bufsiz); - close(inputpipe[1]); - - FILE *input_read = fdopen(inputpipe[0], "r"); - FILE *output_write = fdopen(outputpipe[1], "w"); - - int ret = outputpipe[0]; - - CURL *job = curl_easy_init(); - if(job == NULL) - panic("api: curl_easy_init failed"); - - curl_easy_setopt(job, CURLOPT_URL, url); - curl_easy_setopt(job, CURLOPT_READDATA, input_read); - curl_easy_setopt(job, CURLOPT_WRITEDATA, output_write); - char *requestmethod = "GET"; - switch(method) { - case HTTP_PATCH: - requestmethod = "PATCH"; - break; - case HTTP_DELETE: - requestmethod = "DELETE"; - break; - case HTTP_PUT: - requestmethod = "PUT"; - break; - case HTTP_POST: - requestmethod = "POST"; - break; - case HTTP_GET: /* fallthrough */ - default: - break; - } - curl_easy_setopt(job, CURLOPT_CUSTOMREQUEST, requestmethod); - if(headers) - curl_easy_setopt(job, CURLOPT_HTTPHEADER, headers); - CURLcode res = curl_easy_perform(job); - - if(res > 0) { - close(outputpipe[0]); - ret = -res; - } - - curl_easy_cleanup(job); - fclose(input_read); - fclose(output_write); - return ret; -} - -int api_request(HTTPMethod method, char *url, - struct curl_slist *_Nullable headers, char *writebuf, size_t bufsiz) -{ - char *new_url = calloc((strlen("https://discord.com/api") + strlen(url) + 1), - sizeof(char)); - strcpy(new_url, "https://discord.com/api"); - strcat(new_url, url); - struct curl_slist *headers_auth = curl_slist_append(headers, token); - int ret = http_request(method, new_url, headers_auth, writebuf, bufsiz); - free(new_url); - curl_slist_free_all(headers_auth); - return ret; -} |