request.c (1321B)
1 #include <aur/request.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 betaur_searchby_t betaur_str_to_searchby(size_t searchby_len, const char* searchby) { 7 #define X(ENUM, STR) \ 8 if (searchby == #STR) \ 9 return ENUM; 10 11 BETAUR_SEARCHBY_MAP(X) 12 #undef X 13 return BETAUR_INVALID; 14 } 15 16 const char* betaur_searchby_to_str(betaur_searchby_t by) { 17 switch(by) { 18 #define X(ENUM, STR) \ 19 case ENUM: \ 20 return #STR; \ 21 break; 22 23 BETAUR_SEARCHBY_MAP(X) 24 #undef X 25 default: 26 return ""; 27 } 28 } 29 30 31 bool betaur_http_search(betaur_searchby_t by, size_t arg_len, char* arg) { 32 // TODO stub: implement 33 #define BETAUR_SEARCH_API_STR "/rpc/v5/search/%.*s?by=%.*s" 34 #define BETAUR_SEARCH_API_LEN (sizeof(BETAUR_SEARCH_API_STR) - 8) 35 char* final_string; 36 const char* bystr = betaur_searchby_to_str(by); 37 size_t bystr_len = strlen(bystr); 38 size_t final_string_len = BETAUR_SEARCH_API_LEN + bystr_len + arg_len; 39 final_string = (char*) malloc(final_string_len); 40 41 if( snprintf(final_string, final_string_len, BETAUR_SEARCH_API_STR, bystr_len, bystr, arg_len, arg) != final_string_len - 1) { 42 printf("FATAL: ERrro string not matching: %d\n", final_string_len); 43 return false; 44 // TODO throw error 45 } 46 final_string[final_string_len] = '\0'; 47 48 // TODO call curl 49 50 free(final_string); 51 return false; 52 } 53 54 #undef BETAUR_SEARCHBY_MAP