request: Parse JSON responses.
authorRicardo Wurmus <rekado@elephly.net>
Thu, 25 Feb 2021 14:17:18 +0000 (15:17 +0100)
committerRicardo Wurmus <rekado@elephly.net>
Thu, 25 Feb 2021 14:20:43 +0000 (15:20 +0100)
* aws/request.scm (make-operation->request): Inspect content-type of
server response.

aws/request.scm
configure.ac

index d311ec2018c5c0c65627594ae97e396ab3026066..7ccb6c68a465d864406c94ef8999ad499341677d 100644 (file)
   #:use-module (gcrypt hmac)
   #:use-module (rnrs bytevectors)
   #:use-module (web client)
+  #:use-module ((web response) #:select (response-content-type))
   #:use-module ((web http) #:select (header-writer declare-header!))
   #:use-module (sxml simple)
+  #:use-module (json)
   #:export (make-operation->request serialize-aws-value))
 
 ;;; Commentary:
                           (_ ""))
                         #:headers new-headers))
       (lambda (response body)
-        (xml->sxml (match body
-                     ((? bytevector? bv)
-                      (utf8->string bv))
-                     ((? string? s) s)))))))
+        (let ((server-text (match body
+                             ((? bytevector? bv)
+                              (utf8->string bv))
+                             ((? string? s) s))))
+          (match (response-content-type response)
+            (('application/x-amz-json-1.1 . rest)
+             (json-string->scm server-text))
+            (('text/xml . rest)
+             (xml->sxml server-text))
+            (_ server-text)))))))
index 0b8683426a0191a0604e99c184ddf502bfc1ab1b..24ac639d9145be65fe31fd47fd13ca5cf23f0c7a 100644 (file)
@@ -11,5 +11,6 @@ AC_CONFIG_FILES([pre-inst-env], [chmod +x pre-inst-env])
 
 GUILE_PKG([3.0 2.2])
 GUILE_PROGS
+GUILE_MODULE_REQUIRED(json)
 
 AC_OUTPUT