summaryrefslogtreecommitdiff
path: root/src/github/client.c
diff options
context:
space:
mode:
authorGravatar Anshul Gupta <ansg191@anshulg.com> 2025-06-12 01:43:36 -0700
committerGravatar Anshul Gupta <ansg191@anshulg.com> 2025-06-12 01:46:22 -0700
commite3b96711f9f7bb2c8ec6c3c51b7525d0f5fd9413 (patch)
tree1b2ba847333bae38424571bab964ad2ffab0fa87 /src/github/client.c
parent179679633a9fc3317585167f86c87a7fe8394945 (diff)
downloadgithub-mirror-e3b96711f9f7bb2c8ec6c3c51b7525d0f5fd9413.tar.gz
github-mirror-e3b96711f9f7bb2c8ec6c3c51b7525d0f5fd9413.tar.zst
github-mirror-e3b96711f9f7bb2c8ec6c3c51b7525d0f5fd9413.zip
Implement sourcehut remote
Diffstat (limited to 'src/github/client.c')
-rw-r--r--src/github/client.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/github/client.c b/src/github/client.c
new file mode 100644
index 0000000..0c0034e
--- /dev/null
+++ b/src/github/client.c
@@ -0,0 +1,100 @@
+//
+// Created by Anshul Gupta on 4/4/25.
+//
+
+#include "client.h"
+
+#include <stdlib.h>
+
+#include <cjson/cJSON.h>
+#include <curl/curl.h>
+
+#include "queries/github/gh_identity.h"
+#include "queries/github/gh_list_repos.h"
+
+#include "../buffer.h"
+#include "types.h"
+
+char *github_identity(const gql_client *client)
+{
+ char *login = NULL;
+ buffer_t buf = buffer_new(4096);
+
+ const CURLcode ret = gql_client_send(client, gh_identity, NULL, &buf);
+ if (ret != CURLE_OK) {
+ fprintf(stderr, "Failed to send request: %s\n",
+ curl_easy_strerror(ret));
+ goto fail;
+ }
+
+ // Parse the response
+ cJSON *root = cJSON_Parse((const char *) buf.data);
+ if (!root) {
+ const char *err = cJSON_GetErrorPtr();
+ if (err)
+ fprintf(stderr, "Error parsing response: %s\n", err);
+ goto fail;
+ }
+
+ // Check for errors
+ if (gql_handle_error(root) < 0)
+ goto fail1;
+
+ // Get login from json
+ login = identity_from_json(root);
+
+fail1:
+ cJSON_Delete(root);
+fail:
+ buffer_free(buf);
+ return login;
+}
+
+int github_list_user_repos(const gql_client *client, const char *username,
+ const char *after, struct gh_list_repos_res *res)
+{
+ int status = 0;
+ buffer_t buf = buffer_new(4096);
+
+ cJSON *args = cJSON_CreateObject();
+ if (!args) {
+ status = -1;
+ goto end;
+ }
+ cJSON_AddItemToObject(args, "username", cJSON_CreateString(username));
+ cJSON_AddItemToObject(args, "after", cJSON_CreateString(after));
+
+ const CURLcode ret = gql_client_send(client, gh_list_repos, args, &buf);
+ if (ret != CURLE_OK) {
+ fprintf(stderr, "Failed to send request: %s\n",
+ curl_easy_strerror(ret));
+ status = -1;
+ goto end;
+ }
+
+ // Parse the response
+ cJSON *root = cJSON_Parse((const char *) buf.data);
+ if (!root) {
+ const char *err = cJSON_GetErrorPtr();
+ if (err)
+ fprintf(stderr, "Error parsing response: %s\n", err);
+ status = -1;
+ goto end;
+ }
+
+ // Check for errors
+ if (gql_handle_error(root) < 0) {
+ status = -1;
+ goto end;
+ }
+
+ // Convert json to struct
+ if (gh_list_repos_from_json(root, res) < 0) {
+ fprintf(stderr, "Failed to parse response\n");
+ status = -1;
+ }
+
+end:
+ buffer_free(buf);
+ return status;
+}