diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | post.c | 3 | ||||
-rw-r--r-- | post.h | 30 | ||||
-rw-r--r-- | request.h | 9 | ||||
-rw-r--r-- | ticket.c | 89 |
5 files changed, 56 insertions, 77 deletions
diff --git a/Makefile.am b/Makefile.am index 4ffc52a..e12c540 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,6 +5,8 @@ librtclient_a_SOURCES = \ rtclient/client.h \ request.h \ request.c \ + post.h \ + post.c \ user.c \ ticket.c \ client.c @@ -0,0 +1,3 @@ +#include "post.h" + +extern inline void post(const char *, const char *[], size_t); @@ -0,0 +1,30 @@ +#include "request.h" + +inline void post(const char *path, const char *pairs[], size_t n) +{ + size_t length = 0; + for (size_t i = 0; i < n; i += 2) { + const char *pair = pairs[i]; + if (pair && strcmp(pair, "")) + length += strlen(pair) + strlen(++pair) + 3; + } + + char content[length + 1]; + memset(content, 0, strlen(content)); + for (size_t i = 0; i < n; i += 2) { + const char *pair = pairs[i]; + if (pair && strcmp(pair, "")) + sprintf(content, "%s%s: %s\n", content, pairs[i + 1] + , pair); + } + + struct curl_httppost *post, *last = NULL; + curl_formadd(&post, &last + , CURLFORM_COPYNAME, "content" + , CURLFORM_PTRCONTENTS, content + , CURLFORM_END); + last = NULL; + request(path, "", NULL, NULL, post); + curl_formfree(post); + post = NULL; +} @@ -1,14 +1,11 @@ #ifndef RTCLIENT_REQUEST_H #define RTCLIENT_REQUEST_H -#ifdef DEBUG -#ifdef ANDROID +#if defined(DEBUG) && defined(ANDROID) #include <android/log.h> -#else -#include <stdio.h> -#endif // ANDROID -#endif // DEBUG +#endif #include <string.h> +#include <stdio.h> #include <curl/curl.h> extern CURL *curl; @@ -1,5 +1,5 @@ #include <stdlib.h> -#include "request.h" +#include "post.h" #include "rtclient/ticket.h" typedef struct rtclient_ticketlist rtclient_ticketlist; @@ -19,76 +19,23 @@ void rtclient_ticket_new(const char *queue , const char *due , const char *text) { - size_t length = strlen("id: ticket/new\n"); - if (queue && strcmp(queue, "")) - length += strlen("Queue: \n") + strlen(queue); - if (requestor && strcmp(requestor, "")) - length += strlen("Requestor: \n") + strlen(requestor); - if (subject && strcmp(subject, "")) - length += strlen("Subject: \n") + strlen(subject); - if (cc && strcmp(cc, "")) - length += strlen("Cc: \n") + strlen(cc); - if (admincc && strcmp(admincc, "")) - length += strlen("AdminCc: \n") + strlen(admincc); - if (owner && strcmp(owner, "")) - length += strlen("Owner: \n") + strlen(owner); - if (status && strcmp(status, "")) - length += strlen("Status: \n") + strlen(status); - if (priority && strcmp(priority, "")) - length += strlen("Priority: \n") + strlen(priority); - if (initialpriority && strcmp(initialpriority, "")) - length += strlen("InitialPriority: \n") + strlen(initialpriority); - if (finalpriority && strcmp(finalpriority, "")) - length += strlen("FinalPriority: \n") + strlen(finalpriority); - if (timeestimated && strcmp(timeestimated, "")) - length += strlen("TimeEstimated: \n") + strlen(timeestimated); - if (starts && strcmp(starts, "")) - length += strlen("Starts: \n") + strlen(starts); - if (due && strcmp(due, "")) - length += strlen("Due: \n") + strlen(due); - if (text && strcmp(text, "")) - length += strlen("Text: \n") + strlen(text); - - char content[length + 1]; - strcpy(content, "id: ticket/new\n"); - if (queue && strcmp(queue, "")) - sprintf(content, "%sQueue: %s\n", content, queue); - if (requestor && strcmp(requestor, "")) - sprintf(content, "%sRequestor: %s\n", content, requestor); - if (subject && strcmp(subject, "")) - sprintf(content, "%sSubject: %s\n", content, subject); - if (cc && strcmp(cc, "")) - sprintf(content, "%sCc: %s\n", content, cc); - if (admincc && strcmp(admincc, "")) - sprintf(content, "%sAdminCc: %s\n", content, admincc); - if (owner && strcmp(owner, "")) - sprintf(content, "%sOwner: %s\n", content, owner); - if (status && strcmp(status, "")) - sprintf(content, "%sStatus: %s\n", content, status); - if (priority && strcmp(priority, "")) - sprintf(content, "%sPriority: %s\n", content, priority); - if (initialpriority && strcmp(initialpriority, "")) - sprintf(content, "%sInitialPriority: %s\n", content, initialpriority); - if (finalpriority && strcmp(finalpriority, "")) - sprintf(content, "%sFinalPriority: %s\n", content, finalpriority); - if (timeestimated && strcmp(timeestimated, "")) - sprintf(content, "%sTimeEstimated: %s\n", content, timeestimated); - if (starts && strcmp(starts, "")) - sprintf(content, "%sStarts: %s\n", content, starts); - if (due && strcmp(due, "")) - sprintf(content, "%sDue: %s\n", content, due); - if (text && strcmp(text, "")) - sprintf(content, "%sText: %s\n", content, text); - - struct curl_httppost *post, *last = NULL; - curl_formadd(&post, &last - , CURLFORM_COPYNAME, "content" - , CURLFORM_PTRCONTENTS, content - , CURLFORM_END); - last = NULL; - request("/REST/1.0/ticket/new", "", NULL, NULL, post); - curl_formfree(post); - post = NULL; + post("/REST/1.0/ticket/new", (const char *[]){ + "ticket/new", "id" + , queue, "Queue" + , requestor, "Requestor" + , subject, "Subject" + , cc, "Cc" + , admincc, "AdminCc" + , owner, "Owner" + , status, "Status" + , priority, "Priority" + , initialpriority, "InitialPriority" + , finalpriority, "FinalPriority" + , timeestimated, "TimeEstimated" + , starts, "Starts" + , due, "Due" + , text, "Text" + }, 28); } static size_t search_callback(void *contents, size_t size, size_t nmemb |