several additions

This commit is contained in:
Debucquoy Anthony 2023-06-05 17:33:04 +02:00
parent e3508eee23
commit 93c0f6180d
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
5 changed files with 101 additions and 7 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
build/ build/
tags
main
.ycm_extra_conf.py

View File

@ -2,7 +2,9 @@
set -xe set -xe
CFLAGS=$(pkg-config --cflags ncurses libcurl) CFLAGS="-g -Wall -Wextra $(pkg-config --cflags ncurses libcurl json-c)"
LIBS=$(pkg-config --libs ncurses libcurl) LIBS=$(pkg-config --libs ncurses libcurl json-c)
gcc ${CFLAGS} -o main main.c giteaAPI.c ${LIBS} gcc ${CFLAGS} -o main main.c giteaAPI.c ${LIBS}
ctags -R

View File

@ -1,15 +1,57 @@
#include "giteaAPI.h" #include "giteaAPI.h"
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <json-c/debug.h>
#include <json-c/json_object.h>
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
#include <json-c/json.h>
#include <assert.h>
#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;
}
SESSION teaui_gitea_session(const char *instance){ SESSION teaui_gitea_session(const char *instance){
curl_global_init(0); curl_global_init(0);
CURL *handle = curl_easy_init(); CURL *handle = curl_easy_init();
SESSION ret = {.handle=handle}; SESSION ret = {.handle=handle};
memcpy(ret.instance, instance, strlen(instance)); ret.instance = malloc(sizeof(char) * (strlen(instance)+1));
strcpy(ret.instance, instance);
return ret; return ret;
} }
@ -24,18 +66,25 @@ void teaui_gitea_auth_basic(SESSION s, const char *user, const char *pass){
} }
void teaui_gitea_auth_token(SESSION s, const char *token){ void teaui_gitea_auth_token(SESSION s, const char *token){
char header[256] = "Authorization: "; char header[256] = "Authorization: token ";
strcat(header, token); strcat(header, token);
curl_easy_setopt(s.handle , CURLOPT_HEADERDATA, header); curl_easy_setopt(s.handle , CURLOPT_HEADERDATA, header);
} }
const char* teaui_gitea_auth_generateToken(SESSION s, const char * username){ void teaui_gitea_auth_generateToken(SESSION s, const char * username, const char *name){
char endpoint[1024]; char endpoint[1024];
strcpy(endpoint, s.instance); strcpy(endpoint, s.instance);
strcat(endpoint, "/api/v1/users/"); strcat(endpoint, "/api/v1/users/");
strcat(endpoint, username); strcat(endpoint, username);
strcat(endpoint, "/tokens"); strcat(endpoint, "/tokens");
char post_data[512] = "name=";
strcat(post_data, name);
curl_easy_setopt(s.handle, CURLOPT_URL, endpoint); curl_easy_setopt(s.handle, CURLOPT_URL, endpoint);
curl_easy_setopt(s.handle, CURLOPT_POSTFIELDS, post_data);
curl_easy_setopt(s.handle, CURLOPT_WRITEFUNCTION, _teaui_gitea_saveResponse);
curl_easy_perform(s.handle); curl_easy_perform(s.handle);
return ""; curl_easy_getinfo(s.handle, CURLINFO_RESPONSE_CODE, response.status);
return ;
} }

View File

@ -8,12 +8,25 @@ typedef struct {
char* instance; char* instance;
} SESSION; } SESSION;
struct RESPONSE{
char* data;
size_t size;
long status;
};
enum TEAUI_GITEA_JSONPARSER {
TEAUI_GITEA_JSONPARSER_TOKEN,
};
void teaui_gitea_printResponse();
int teaui_gitea_parseResponse(char* dest, enum TEAUI_GITEA_JSONPARSER type);
SESSION teaui_gitea_session(const char *instance); SESSION teaui_gitea_session(const char *instance);
void teaui_gitea_cleanup(SESSION s); void teaui_gitea_cleanup(SESSION s);
void teaui_gitea_auth_basic(SESSION s, const char *user, const char *pass); void teaui_gitea_auth_basic(SESSION s, const char *user, const char *pass);
void teaui_gitea_auth_token(SESSION s, const char *token); void teaui_gitea_auth_token(SESSION s, const char *token);
const char* teaui_gitea_auth_generateToken(SESSION s, const char *username); void teaui_gitea_auth_generateToken(SESSION s, const char *username, const char *name);
#endif /* ifndef GITEA_API_H_ */ #endif /* ifndef GITEA_API_H_ */

View File

@ -11,3 +11,29 @@
- libcurl - libcurl
- ncurses - ncurses
- a json library to find - a json library to find
## endpoints
### Notifications
- /notifications [GET, PUT]
### Organisations
- /orgs [GET]
- /orgs/{org} [GET]
- /orgs/{org}/repos [GET, POST]
- /usr/orgs
### Issues
pretty much everything
### repository
- repos/{owner}/{repo} [GET, DELETE, PATCH]
- repos/{owner}/{repo}/branches [GET, POST]
- repos/{owner}/{repo}/branches/{branch} [GET, DELETE]
- repos/{owner}/{repo}/pulls [POST, GET]
- ...