teaui/giteaAPI.c

42 lines
1.1 KiB
C
Raw Normal View History

#include "giteaAPI.h"
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>
#include <malloc.h>
SESSION teaui_gitea_session(const char *instance){
curl_global_init(0);
CURL *handle = curl_easy_init();
SESSION ret = {.handle=handle};
memcpy(ret.instance, instance, strlen(instance));
return ret;
}
void teaui_gitea_cleanup(SESSION s){
curl_global_cleanup();
curl_easy_cleanup(s.handle);
}
void teaui_gitea_auth_basic(SESSION s, const char *user, const char *pass){
curl_easy_setopt(s.handle, CURLOPT_USERNAME, user);
curl_easy_setopt(s.handle, CURLOPT_PASSWORD, pass);
}
void teaui_gitea_auth_token(SESSION s, const char *token){
char header[256] = "Authorization: ";
strcat(header, token);
curl_easy_setopt(s.handle , CURLOPT_HEADERDATA, header);
}
const char* teaui_gitea_auth_generateToken(SESSION s, const char * username){
char endpoint[1024];
strcpy(endpoint, s.instance);
strcat(endpoint, "/api/v1/users/");
strcat(endpoint, username);
strcat(endpoint, "/tokens");
curl_easy_setopt(s.handle, CURLOPT_URL, endpoint);
curl_easy_perform(s.handle);
return "";
}