From ee80ab2258cf7c75613891837cf4444bffef133d Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 14 Apr 2026 00:20:10 +0100 Subject: Generalize json-post to json-request. --- kaagum/openai.scm | 26 +++++++++++++++++--------- 1 file 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") -- cgit 1.4.1