Adjusted conversion of JSON response
- IPFS sometimes returns the JSON response as a string, confusing the existing code. Switched to a check of :content-type in the response header. - While we are at it, detect errors by testing the response code.
This commit is contained in:
parent
db8682b513
commit
d5e641c3ea
60
main.lisp
60
main.lisp
|
@ -30,28 +30,29 @@
|
||||||
If the JSON is 'error JSON', I.E., it signals that an error has been
|
If the JSON is 'error JSON', I.E., it signals that an error has been
|
||||||
recieved, two values are returned: NIL and the string-error-message."
|
recieved, two values are returned: NIL and the string-error-message."
|
||||||
(let ((result
|
(let ((result
|
||||||
(drakma:http-request
|
(multiple-value-list
|
||||||
|
(drakma:http-request
|
||||||
(make-call-url call arguments)
|
(make-call-url call arguments)
|
||||||
:method method
|
:method method
|
||||||
:url-encoder #'ipfs::url-encode
|
:url-encoder #'ipfs::url-encode
|
||||||
:parameters parameters
|
:parameters parameters
|
||||||
:want-stream want-stream)))
|
:want-stream want-stream))))
|
||||||
(if want-stream
|
(if want-stream
|
||||||
result
|
(car result)
|
||||||
(process-result result))))
|
(apply #'process-result result))))
|
||||||
|
|
||||||
(defun process-result (result)
|
(defun process-result (body status-code headers uri http-stream must-close status-text)
|
||||||
(cond
|
(declare (ignore uri http-stream must-close status-text))
|
||||||
((stringp result) (values nil result))
|
(let* ((result (cond ((stringp body) body)
|
||||||
((vectorp result)
|
((vectorp body) (flexi-streams:octets-to-string body))))
|
||||||
(let* ((string (flexi-streams:octets-to-string result))
|
(result (if (search "application/json" (cdr (assoc :content-type headers)))
|
||||||
(alist
|
(simplify (yason:parse result :object-as :alist))
|
||||||
(with-input-from-string (stream string)
|
result)))
|
||||||
(loop while (peek-char t stream nil)
|
(if (eql 200 status-code)
|
||||||
collect (yason:parse stream :object-as :alist)))))
|
result
|
||||||
(if (ignore-errors (equal (cdr (s-assoc "Type" (simplify alist))) "error"))
|
(values nil (if (stringp result)
|
||||||
(values NIL (cdr (s-assoc "Message" (simplify alist))))
|
result
|
||||||
(simplify alist))))))
|
(ignore-errors (cdr (s-assoc "Message" result))))))))
|
||||||
|
|
||||||
;; STRING LIST &key STRING STRING → STRING
|
;; STRING LIST &key STRING STRING → STRING
|
||||||
(defun make-call-url (call arguments &key (host *api-host*) (root *api-root*))
|
(defun make-call-url (call arguments &key (host *api-host*) (root *api-root*))
|
||||||
|
@ -509,19 +510,20 @@
|
||||||
a local file.
|
a local file.
|
||||||
/ipns/docs.ipfs.io/reference/api/http/#api-v0-files-rm"
|
/ipns/docs.ipfs.io/reference/api/http/#api-v0-files-rm"
|
||||||
(let ((result
|
(let ((result
|
||||||
(drakma:http-request
|
(multiple-value-list
|
||||||
(make-call-url
|
(drakma:http-request
|
||||||
"files/write"
|
(make-call-url
|
||||||
`(("arg" ,dest-path) ("create", create) ("parents" ,parents)
|
"files/write"
|
||||||
("truncate" ,truncate) ("raw-leaves" ,raw-leaves)
|
`(("arg" ,dest-path) ("create", create) ("parents" ,parents)
|
||||||
,@(when offset (list "offset" offset))
|
("truncate" ,truncate) ("raw-leaves" ,raw-leaves)
|
||||||
,@(when count (list "count" count))
|
,@(when offset (list "offset" offset))
|
||||||
,@(when cid-version `("cid-version" ,cid-version))
|
,@(when count (list "count" count))
|
||||||
,@(when hash (list "hash" hash))))
|
,@(when cid-version `("cid-version" ,cid-version))
|
||||||
:method :post
|
,@(when hash (list "hash" hash))))
|
||||||
:parameters `(("data" . ,path-or-string))
|
:method :post
|
||||||
:form-data t)))
|
:parameters `(("data" . ,path-or-string))
|
||||||
(process-result result)))
|
:form-data t))))
|
||||||
|
(apply #'process-result result)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Ŝarĝante…
Reference in New Issue