add function for doing jmap API requests dav-2 tip

Thu, 09 Jul 2026 22:07:33 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 09 Jul 2026 22:07:33 +0200
branch
dav-2
changeset 926
8470d5aac30e
parent 925
61a231a8d8f5

add function for doing jmap API requests

libidav/Makefile file | annotate | diff | comparison | revisions
libidav/jmap_methods.c file | annotate | diff | comparison | revisions
libidav/jmap_methods.h file | annotate | diff | comparison | revisions
--- a/libidav/Makefile	Wed Jul 08 15:34:41 2026 +0200
+++ b/libidav/Makefile	Thu Jul 09 22:07:33 2026 +0200
@@ -43,6 +43,7 @@
 SRC += pwdstore.c
 SRC += jmap.c
 SRC += jmapsession.c
+SRC += jmap_methods.c
 
 OBJ = $(SRC:%.c=../build/libidav/%$(OBJ_EXT))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libidav/jmap_methods.c	Thu Jul 09 22:07:33 2026 +0200
@@ -0,0 +1,77 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "jmap_methods.h"
+
+CxBuffer* jm_mailbox_get_request(cxstring account_id) {
+    CxBuffer *buf = cxBufferCreate(NULL, NULL, 512, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS);
+    
+    cxBufferPutString(buf,
+            "{\"using\":["
+                "\"urn:ietf:params:jmap:core\","
+                "\"urn:ietf:params:jmap:mail\""
+            "],\"methodCalls\":["
+                "[\"Mailbox/get\","
+                    "{\"accountId\":\"");
+    cxBufferPutString(buf, account_id);
+    cxBufferPutString(buf,
+            "\",\"ids\":null},"
+                    "\"0\""
+                "]"
+            "]}");
+    
+    return buf;
+}
+
+CxJsonValue* jm_do_api_request(JMAPSession *sn, CxBuffer *request) {
+    curl_easy_setopt(sn->handle, CURLOPT_URL, sn->apiUrl);
+    
+    curl_easy_setopt(sn->handle, CURLOPT_POST, 1L);
+    curl_easy_setopt(sn->handle, CURLOPT_READFUNCTION, cxBufferRead);
+    curl_easy_setopt(sn->handle, CURLOPT_SEEKFUNCTION, cxBufferSeek);
+    curl_easy_setopt(sn->handle, CURLOPT_READDATA, request);
+    curl_easy_setopt(sn->handle, CURLOPT_SEEKDATA, request);
+    curl_easy_setopt(sn->handle, CURLOPT_INFILESIZE, request->size);
+    
+    CxBuffer *response = cxBufferCreate(NULL, NULL, 4096, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS);
+    curl_easy_setopt(sn->handle, CURLOPT_WRITEFUNCTION, cxBufferWrite);
+    curl_easy_setopt(sn->handle, CURLOPT_WRITEDATA, response);
+    
+    CURLcode ret = curl_easy_perform(sn->handle);
+    long http_status;
+    curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
+    CxJsonValue *json = NULL;
+    if(ret == CURLE_OK) {
+        if(http_status >= 200 && http_status <= 299) {
+            (void)cx_json_from_string(sn->mp->allocator, cx_strn(response->space, response->size), &json);
+        }
+    }
+    cxBufferFree(response);
+    
+    return json;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libidav/jmap_methods.h	Thu Jul 09 22:07:33 2026 +0200
@@ -0,0 +1,50 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef JMAP_METHODS_H
+#define JMAP_METHODS_H
+
+#include "jmap.h"
+
+#include <cx/buffer.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+CxBuffer* jm_mailbox_get_request(cxstring account_id);
+
+CxJsonValue* jm_do_api_request(JMAPSession *sn, CxBuffer *request);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* JMAP_METHODS_H */
+

mercurial