about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2026-04-14 00:20:10 +0100
committerArun Isaac2026-04-14 00:20:10 +0100
commitee80ab2258cf7c75613891837cf4444bffef133d (patch)
tree1f0411dd05e7836a72fc8eaac40152e1a3610aa9
parent40798ff23f64434136a3984e8d0f33500da9d284 (diff)
downloadkaagum-ee80ab2258cf7c75613891837cf4444bffef133d.tar.gz
kaagum-ee80ab2258cf7c75613891837cf4444bffef133d.tar.lz
kaagum-ee80ab2258cf7c75613891837cf4444bffef133d.zip
Generalize json-post to json-request.
-rw-r--r--kaagum/openai.scm26
1 files changed, 17 insertions, 9 deletions
diff --git a/kaagum/openai.scm b/kaagum/openai.scm
index 937a1de..8f13de2 100644
--- a/kaagum/openai.scm
+++ b/kaagum/openai.scm
@@ -32,16 +32,15 @@
 (define (uri-join base uri)
   (string-append base uri))
 
-(define* (json-post url #:key headers json)
-  "Send a POST request to @var{url} with @var{json} body and additional
-@var{headers}. The @samp{Content-Type} header is set to @samp{application/json}
-and need not be specified in @var{headers}. Return JSON response."
+(define* (json-request method url #:key (headers '()) body)
+  "Send a HTTP @var{method} request to @var{url} with @var{body} and
+additional @var{headers}. Return JSON response."
   (let-values (((response body)
-                (http-post url
-                           #:headers `((content-type application/json)
-                                       ,@headers)
-                           #:body (scm->json-string json)
-                           #:streaming? #t)))
+                (http-request url
+                              #:method method
+                              #:headers headers
+                              #:body body
+                              #:streaming? #t)))
     ;; Guile does not consider application/json responses as textual, and does
     ;; not automatically set the port encoding to UTF-8.
     (set-port-encoding! body "UTF-8")
@@ -61,6 +60,15 @@ and need not be specified in @var{headers}. Return JSON response."
                     (string-append "JSON API request failed with code "
                                    (number->string (response-code response))))))))))
 
+(define* (json-post url #:key (headers '()) json)
+  "Send a POST request to @var{url} with @var{json} body and additional
+@var{headers}. The @samp{Content-Type} header is set to @samp{application/json}
+and need not be specified in @var{headers}. Return JSON response."
+  (json-request 'POST url
+                #:headers `((content-type application/json)
+                            ,@headers)
+                #:body (scm->json-string json)))
+
 ;; Declare the Authorization header as opaque so that Guile doesn't try to mess
 ;; with it.
 (declare-opaque-header! "Authorization")