Debugged ipfs-files-write.

- Adjusted some supporting code for flexibility.
- Ipfs-files-write will take a pathname or a string as input.
This commit is contained in:
bnmcgn@gmail.com 2021-05-15 17:36:23 -07:00
parent 9e2d1d636d
commit db8682b513

View File

@ -31,26 +31,30 @@
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 (drakma:http-request
(make-call-url *api-host* *api-root* 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
result
(process-result result))))
(cond (want-stream result) (defun process-result (result)
((stringp result) (values nil result)) (cond
((vectorp result) ((stringp result) (values nil result))
(let* ((string (flexi-streams:octets-to-string result)) ((vectorp result)
(alist (let* ((string (flexi-streams:octets-to-string result))
(with-input-from-string (stream string) (alist
(loop while (peek-char t stream nil) (with-input-from-string (stream string)
collect (yason:parse stream :object-as :alist))))) (loop while (peek-char t stream nil)
(if (ignore-errors (equal (cdr (s-assoc "Type" (simplify alist))) "error")) collect (yason:parse stream :object-as :alist)))))
(values NIL (cdr (s-assoc "Message" (simplify alist)))) (if (ignore-errors (equal (cdr (s-assoc "Type" (simplify alist))) "error"))
(simplify alist))))))) (values NIL (cdr (s-assoc "Message" (simplify alist))))
(simplify alist))))))
;; STRING STRING LIST → STRING ;; STRING LIST &key STRING STRING → STRING
(defun make-call-url (host root call arguments) (defun make-call-url (call arguments &key (host *api-host*) (root *api-root*))
"Create the URL of an API call, as per the given arguments. "Create the URL of an API call, as per the given arguments.
Symbols are assumed to be something like 'T (so boolean), nil likewise. Symbols are assumed to be something like 'T (so boolean), nil likewise.
Arguments should look like this: Arguments should look like this:
@ -497,20 +501,27 @@
;; PATHNAME STRING [:NUMBER :BOOLEAN :BOOLEAN :BOOLEAN :NUMBER :BOOLEAN ;; PATHNAME STRING [:NUMBER :BOOLEAN :BOOLEAN :BOOLEAN :NUMBER :BOOLEAN
;; :NUMBER :STRING] ;; :NUMBER :STRING]
;; → NIL || (NIL STRING) ;; → NIL || (NIL STRING)
(defun files-write (pathname dest-path (defun files-write (path-or-string dest-path
&key (offset nil) (create nil) (parents nil) &key (offset nil) (create nil) (parents nil)
(truncate nil) (count nil) (raw-leaves nil) (truncate nil) (count nil) (raw-leaves nil)
(cid-version nil) (hash nil)) (cid-version nil) (hash nil))
"Write to a given file. "Write to a given file. First parameter can be a string or a path to
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"
(ipfs-call "files/write" (let ((result
`(("arg" ,dest-path) ("create" ,create) ("parents" ,parents) (drakma:http-request
("truncate" ,truncate) ("raw-leaves" ,raw-leaves) (make-call-url
,(if offset (list "offset" offset)) "files/write"
,(if count (list "count" count)) `(("arg" ,dest-path) ("create", create) ("parents" ,parents)
,(if cid-version `("cid-version" ,cid-version)) ("truncate" ,truncate) ("raw-leaves" ,raw-leaves)
,(if hash (list "hash" hash))) ,@(when offset (list "offset" offset))
:parameters `(("file" . ,pathname)))) ,@(when count (list "count" count))
,@(when cid-version `("cid-version" ,cid-version))
,@(when hash (list "hash" hash))))
:method :post
:parameters `(("data" . ,path-or-string))
:form-data t)))
(process-result result)))