qlic

Zoho Cliq but not really
git clone git@getsh.org:qlic.git
Log | Files | Refs | LICENSE

qlic_common.c (2904B)


      1 #include <qlic_common.h>
      2 #include <curl/curl.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <qlic_response_handler.h>
      6 
      7 void qlic_error(const char* error_message) {
      8 	fprintf(stderr, "%s\n", error_message);
      9 }
     10 
     11 
     12 
     13 static struct curl_slist* __qlic_set_request_headers(QlicContext* context, QlicString* access_token) {
     14 	CURL* curl = (CURL*)context->context;
     15 	if(access_token == NULL) {
     16 		return NULL;
     17 	}
     18 	struct curl_slist* list = NULL;
     19 #define __QLIC_AUTHORIZATION_HEADER "Authorization: Zoho-oauthtoken "
     20 	// TODO free up authorization_header if curl doesn't handle
     21 	size_t authorization_header_len = sizeof(__QLIC_AUTHORIZATION_HEADER) + access_token->len;
     22 	char* authorization_header = (char*)malloc(authorization_header_len);
     23 	strncpy(authorization_header, __QLIC_AUTHORIZATION_HEADER, sizeof(__QLIC_AUTHORIZATION_HEADER));
     24 	strncat(authorization_header, access_token->string, access_token->len);
     25 	list = curl_slist_append(list, authorization_header);
     26 	list = curl_slist_append(list, "Content-Type: application/json");
     27 	list = curl_slist_append(list, "contentType: application/json");
     28 	return list;
     29 }
     30 
     31 QlicString* init_qlic_string() {
     32 	QlicString* qlic_string = (QlicString*)malloc(sizeof(QlicString));
     33 	qlic_string->string = NULL;
     34 	qlic_string->len = -1;
     35 	return qlic_string;
     36 }
     37 
     38 QlicContext* qlic_context_access_init(QlicString* access_token) {
     39 	QlicContext* qlic_context = qlic_init();
     40 	if (qlic_context) {
     41 		CURL* curl = (CURL*)qlic_context->context;
     42 		struct curl_slist* list = __qlic_set_request_headers(qlic_context, access_token);
     43 		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
     44 		return qlic_context;
     45 	} else {
     46 		return NULL;
     47 	}
     48 }
     49 
     50 void destroy_qlic_string(QlicString* qlic_string) {
     51 	if(qlic_string->string != NULL) {
     52 		free(qlic_string->string);
     53 	}
     54 	free(qlic_string);
     55 }
     56 
     57 QlicContext* qlic_init() {
     58 	CURL *curl = curl_easy_init();
     59 	if (curl) {
     60 		QlicContext* qlic_context = (QlicContext*)calloc(1, sizeof(QlicContext));
     61 		qlic_context->context = curl;
     62 		return qlic_context;
     63 	} else {
     64 		return NULL;
     65 	}
     66 }
     67 
     68 void qlic_request(QlicContext* context, qlic_response_callback callback, bool is_post_request) {
     69 	if(context) {
     70 		CURL* curl = (CURL*)context->context;
     71 		CURLcode res;
     72 		curl_easy_setopt(curl, CURLOPT_URL, context->request_url->string);
     73 		/* curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); */
     74 		if (is_post_request) {
     75 			curl_easy_setopt(curl, CURLOPT_POST, 1);
     76 			curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"text\": \"Hi\"}");
     77 		}
     78 		/* curl_easy_setopt(curl, CURLOPT_READFUNCTION, qlic_handle_read_chat); */
     79 		/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); */
     80 		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
     81 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
     82 		res = curl_easy_perform(curl);
     83 		if(res != CURLE_OK) {
     84 			fprintf(stderr, "curl_easy_perform() failed: %s\n",
     85 								  curl_easy_strerror(res));
     86 		}
     87 		curl_easy_cleanup(curl);
     88 	}
     89 }
     90