2023-06-04 12:35:42 +02:00
|
|
|
#include "giteaAPI.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/easy.h>
|
2023-06-05 17:33:04 +02:00
|
|
|
#include <json-c/debug.h>
|
|
|
|
#include <json-c/json_object.h>
|
2023-06-04 12:35:42 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <malloc.h>
|
2023-06-05 17:33:04 +02:00
|
|
|
#include <json-c/json.h>
|
|
|
|
#include <assert.h>
|
2023-06-04 12:35:42 +02:00
|
|
|
|
2023-06-05 17:33:04 +02:00
|
|
|
#define UNUSED(n) (void)(n)
|
|
|
|
|
|
|
|
struct RESPONSE response;
|
|
|
|
|
|
|
|
void teaui_gitea_printResponse(){
|
|
|
|
fwrite(response.data, sizeof(char), response.size, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int teaui_gitea_parseResponse(char* dest, enum TEAUI_GITEA_JSONPARSER type){
|
|
|
|
char* json = malloc(response.size+1);
|
|
|
|
memcpy(json, response.data, sizeof(char) * response.size);
|
|
|
|
json[response.size-1] = '\0';
|
|
|
|
json_object *parent = json_tokener_parse(json);
|
|
|
|
json_object *child;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TEAUI_GITEA_JSONPARSER_TOKEN:
|
|
|
|
if(json_object_object_get_ex(parent, "sha1", &child)){
|
|
|
|
strcpy(dest, json_object_get_string(child));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strcpy(dest, json);
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t _teaui_gitea_saveResponse(char* ptr, size_t size, size_t nmemb, void *userdata){
|
|
|
|
UNUSED(userdata);
|
|
|
|
char* data = realloc(response.data, response.size + (nmemb*size));
|
|
|
|
assert(data != NULL);
|
|
|
|
response.data = data;
|
|
|
|
memcpy(response.data + response.size, ptr, nmemb*size);
|
|
|
|
response.size += size *nmemb;
|
|
|
|
return nmemb*size;
|
|
|
|
}
|
2023-06-04 12:35:42 +02:00
|
|
|
|
|
|
|
SESSION teaui_gitea_session(const char *instance){
|
|
|
|
curl_global_init(0);
|
|
|
|
CURL *handle = curl_easy_init();
|
|
|
|
SESSION ret = {.handle=handle};
|
2023-06-05 17:33:04 +02:00
|
|
|
ret.instance = malloc(sizeof(char) * (strlen(instance)+1));
|
|
|
|
strcpy(ret.instance, instance);
|
2023-06-04 12:35:42 +02:00
|
|
|
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){
|
2023-06-05 17:33:04 +02:00
|
|
|
char header[256] = "Authorization: token ";
|
2023-06-04 12:35:42 +02:00
|
|
|
strcat(header, token);
|
|
|
|
curl_easy_setopt(s.handle , CURLOPT_HEADERDATA, header);
|
|
|
|
}
|
|
|
|
|
2023-06-05 17:33:04 +02:00
|
|
|
void teaui_gitea_auth_generateToken(SESSION s, const char * username, const char *name){
|
2023-06-04 12:35:42 +02:00
|
|
|
char endpoint[1024];
|
|
|
|
strcpy(endpoint, s.instance);
|
|
|
|
strcat(endpoint, "/api/v1/users/");
|
|
|
|
strcat(endpoint, username);
|
|
|
|
strcat(endpoint, "/tokens");
|
2023-06-05 17:33:04 +02:00
|
|
|
|
|
|
|
char post_data[512] = "name=";
|
|
|
|
strcat(post_data, name);
|
|
|
|
|
2023-06-04 12:35:42 +02:00
|
|
|
curl_easy_setopt(s.handle, CURLOPT_URL, endpoint);
|
2023-06-05 17:33:04 +02:00
|
|
|
curl_easy_setopt(s.handle, CURLOPT_POSTFIELDS, post_data);
|
|
|
|
curl_easy_setopt(s.handle, CURLOPT_WRITEFUNCTION, _teaui_gitea_saveResponse);
|
2023-06-04 12:35:42 +02:00
|
|
|
curl_easy_perform(s.handle);
|
2023-06-05 17:33:04 +02:00
|
|
|
curl_easy_getinfo(s.handle, CURLINFO_RESPONSE_CODE, response.status);
|
|
|
|
return ;
|
2023-06-04 12:35:42 +02:00
|
|
|
}
|