With-files-write macro #6

Merged
BnMcGn merged 3 commits from master into master 2021-06-26 21:50:21 -05:00
Showing only changes of commit db8682b513 - Show all commits

View File

@ -31,13 +31,17 @@
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)
(cond
((stringp result) (values nil result)) ((stringp result) (values nil result))
((vectorp result) ((vectorp result)
(let* ((string (flexi-streams:octets-to-string result)) (let* ((string (flexi-streams:octets-to-string result))
@ -47,10 +51,10 @@
collect (yason:parse stream :object-as :alist))))) collect (yason:parse stream :object-as :alist)))))
(if (ignore-errors (equal (cdr (s-assoc "Type" (simplify alist))) "error")) (if (ignore-errors (equal (cdr (s-assoc "Type" (simplify alist))) "error"))
(values NIL (cdr (s-assoc "Message" (simplify alist)))) (values NIL (cdr (s-assoc "Message" (simplify alist))))
(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
(make-call-url
"files/write"
`(("arg" ,dest-path) ("create", create) ("parents" ,parents)
("truncate" ,truncate) ("raw-leaves" ,raw-leaves) ("truncate" ,truncate) ("raw-leaves" ,raw-leaves)
,(if offset (list "offset" offset)) ,@(when offset (list "offset" offset))
,(if count (list "count" count)) ,@(when count (list "count" count))
,(if cid-version `("cid-version" ,cid-version)) ,@(when cid-version `("cid-version" ,cid-version))
,(if hash (list "hash" hash))) ,@(when hash (list "hash" hash))))
:parameters `(("file" . ,pathname)))) :method :post
:parameters `(("data" . ,path-or-string))
:form-data t)))
(process-result result)))