2015-01-31 10:13:49 -06:00
;;; ein-contents-api.el --- Interface to Jupyter's Contents API
;; Copyright (C) 2015 - John Miller
;; Authors: Takafumi Arakaki <aka.tkf at gmail.com>
;; John M. Miller <millejoh at mac.com>
;; This file is NOT part of GNU Emacs.
;; ein-contents-api.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; ein-contents-api.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with ein-notebooklist.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;;
;;; An interface to the Jupyter Contents API as described in
;;; https://github.com/ipython/ipython/wiki/IPEP-27%3A-Contents-Service.
;;;
;;
;;; Code:
2015-02-16 13:06:35 -06:00
( require 'cl )
2015-01-31 10:13:49 -06:00
( require 'ein-core )
2017-07-12 14:38:04 -05:00
( require 'ein-classes )
2015-02-09 13:27:20 -06:00
( require 'ein-utils )
2016-12-05 09:14:41 -05:00
( require 'ein-log )
( require 'ein-query )
( provide 'ein-contents-api ) ; must provide before requiring ein-notebook:
( require 'ein-notebook ) ; circular: depends on this file!
2015-01-31 10:13:49 -06:00
2015-02-10 14:53:08 -06:00
( defcustom ein:content-query-timeout ( * 60 1000 ) ; 1 min
" Query timeout for getting content from Jupyter/IPython notebook.
If you cannot open large notebooks because of a timeout error try
increasing this value. Setting this value to ` nil ' means to use
global setting. For global setting and more information, see
` ein:query-timeout '. "
:type ' ( choice ( integer :tag " Timeout [ms] " 5000 )
( const :tag " Use global setting " nil ) )
:group 'ein )
2016-10-05 11:39:54 -05:00
( defcustom ein:force-sync nil
" If T, force ein to communicate with the IPython/Jupyter contents API synchronously. If not, use asynchronous communication. If you are seeing odd errors while using ein try setting this to T, though note that Emacs will likely be less responsive as it blocks while waiting for the IPython/Jupyter notebook server to respond "
:type 'boolean
:group 'ein )
2015-02-10 14:53:08 -06:00
2016-10-19 11:57:34 -05:00
( defun ein:content-url ( content &rest params )
2015-02-16 13:06:35 -06:00
( let ( ( url-or-port ( ein:$content-url-or-port content ) )
( path ( ein:$content-path content ) ) )
2016-10-19 11:57:34 -05:00
( if params
( url-encode-url ( apply #' ein:url
url-or-port
" api/contents "
path
params ) )
( url-encode-url ( ein:url url-or-port " api/contents " path ) ) ) ) )
( defun ein:content-url-legacy ( content &rest params )
2015-02-09 13:27:20 -06:00
" Generate content url's for IPython Notebook version 2.x "
2015-02-16 13:06:35 -06:00
( let ( ( url-or-port ( ein:$content-url-or-port content ) )
2015-12-04 14:15:41 -06:00
( path ( ein:$content-path content ) ) )
2016-10-19 11:57:34 -05:00
( if params
( url-encode-url ( apply #' ein:url
url-or-port
" api/notebooks "
path
params ) )
( url-encode-url ( ein:url url-or-port " api/notebooks " path ) ) ) ) )
2015-02-09 13:27:20 -06:00
2017-02-17 15:06:59 -06:00
( defun ein:content-query-contents ( path &optional url-or-port force-sync callback retry-p )
2015-01-31 10:31:20 -06:00
" Return the contents of the object at the specified path from the Jupyter server. "
2016-12-22 12:19:52 -06:00
( condition-case err
( let* ( ( url-or-port ( or url-or-port ( ein:default-url-or-port ) ) )
( new-content ( make-ein:$content
:url-or-port url-or-port
:ipython-version ( ein:query-ipython-version url-or-port )
:path path ) )
( url ( ein:content-url new-content ) ) )
( if ( = 2 ( ein:$content-ipython-version new-content ) )
( setq new-content ( ein:content-query-contents-legacy path url-or-port ein:force-sync callback ) )
( ein:query-singleton-ajax
( list 'content-query-contents url-or-port path )
url
:type " GET "
:timeout ein:content-query-timeout
:parser #' ein:json-read
:sync ein:force-sync
:success ( apply-partially #' ein:new-content new-content callback )
2017-02-17 15:06:59 -06:00
:error ( apply-partially #' ein:content-query-contents-error url retry-p
( list path url-or-port force-sync callback t ) ) ) )
2016-12-22 12:19:52 -06:00
new-content )
2017-02-01 09:25:47 -06:00
( error ( progn ( message " Error %s on query contents, try calling ` ein:notebooklist-login ` first... " err )
( if ( >= ein:log-level ( ein:log-level-name-to-int 'debug ) )
( throw 'error err ) ) ) ) ) )
2015-02-09 13:27:20 -06:00
2015-02-10 14:53:08 -06:00
( defun ein:content-query-contents-legacy ( path &optional url-or-port force-sync callback )
2015-02-12 12:10:56 -06:00
" Return contents of object at specified path for IPython Notebook versions 2.x "
2015-02-09 13:27:20 -06:00
( let* ( ( url-or-port ( or url-or-port ( ein:default-url-or-port ) ) )
2015-02-10 14:53:08 -06:00
( new-content ( make-ein:$content :url-or-port url-or-port
2015-02-16 13:06:35 -06:00
:ipython-version ( ein:query-ipython-version url-or-port )
:path path ) )
( url ( ein:content-url-legacy new-content ) ) )
2015-01-31 10:13:49 -06:00
( ein:query-singleton-ajax
2015-02-09 13:27:20 -06:00
( list 'content-query-contents-legacy url-or-port path )
2015-01-31 10:13:49 -06:00
url
:type " GET "
2015-02-10 14:53:08 -06:00
:timeout ein:content-query-timeout
2015-01-31 10:13:49 -06:00
:parser #' ein:json-read
2016-10-05 11:39:54 -05:00
:sync ein:force-sync
2015-02-12 08:45:29 -06:00
:success ( apply-partially #' ein:query-contents-legacy-success path new-content callback )
2015-02-15 11:34:50 -06:00
:error ( apply-partially #' ein:content-query-contents-error url ) )
2015-01-31 10:13:49 -06:00
new-content ) )
2015-02-09 13:27:20 -06:00
( defun ein:fix-legacy-content-data ( data )
( if ( listp ( car data ) )
( loop for item in data
collecting
( ein:fix-legacy-content-data item ) )
( if ( string= ( plist-get data :path ) " " )
2015-05-27 21:49:48 -05:00
( plist-put data :path ( plist-get data :name ) )
2015-02-09 13:27:20 -06:00
( plist-put data :path ( format " %s/%s " ( plist-get data :path ) ( plist-get data :name ) ) ) ) ) )
2015-02-12 08:45:29 -06:00
( defun* ein:query-contents-legacy-success ( path content callback &key data &allow-other-keys )
( if ( not ( plist-get data :type ) )
;; Content API in 2.x a bit inconsistent.
( progn
( setf ( ein:$content-name content ) ( substring path ( or ( cl-position ?/ path ) 0 ) )
2016-12-05 09:14:41 -05:00
( ein:$content-path content ) path
2015-02-12 08:45:29 -06:00
( ein:$content-type content ) " directory "
;;(ein:$content-created content) (plist-get data :created)
;;(ein:$content-last-modified content) (plist-get data :last_modified)
( ein:$content-format content ) nil
( ein:$content-writable content ) nil
( ein:$content-mimetype content ) nil
( ein:$content-raw-content content ) ( ein:fix-legacy-content-data data ) )
( when callback
( funcall callback content ) )
content )
( ein:new-content content callback :data data ) ) )
2015-02-09 13:27:20 -06:00
2017-01-11 07:42:06 -06:00
;; TODO: This is one place to check for redirects - update the url slot if so.
;; Will need to pass the response object and check either request-response-history
;; or request-response-url.
( defun* ein:new-content ( content callback &key data response &allow-other-keys )
2015-01-31 10:31:20 -06:00
( setf ( ein:$content-name content ) ( plist-get data :name )
( ein:$content-path content ) ( plist-get data :path )
( ein:$content-type content ) ( plist-get data :type )
( ein:$content-created content ) ( plist-get data :created )
( ein:$content-last-modified content ) ( plist-get data :last_modified )
( ein:$content-format content ) ( plist-get data :format )
( ein:$content-writable content ) ( plist-get data :writable )
( ein:$content-mimetype content ) ( plist-get data :mimetype )
2015-02-10 14:53:08 -06:00
( ein:$content-raw-content content ) ( plist-get data :content ) )
2017-07-21 12:36:07 -05:00
( ein:aif ( ein:get-response-redirect response )
( setf ( ein:$content-url-or-port content ) it ) )
;; (if (length (request-response-history response))
;; (let ((url (url-generic-parse-url (format "%s" (request-response-url response)))))
;; (setf (ein:$content-url-or-port content) (format "%s://%s:%s"
;; (url-type url)
;; (url-host url)
;; (url-port url)))))
2015-02-10 14:53:08 -06:00
( when callback
( funcall callback content ) )
content )
2015-01-31 10:31:20 -06:00
2015-02-12 08:45:29 -06:00
( defun ein:content-to-json ( content )
( let ( ( path ( if ( >= ( ein:$content-ipython-version content ) 3 )
( ein:$content-path content )
( substring ( ein:$content-path content )
0
2015-02-12 12:10:56 -06:00
( or ( cl-position ?/ ( ein:$content-path content ) :from-end t )
0 ) ) ) ) )
2015-02-12 08:45:29 -06:00
( json-encode ` ( ( :type . , ( ein:$content-type content ) )
2016-03-01 16:02:00 -06:00
( :name . , ( ein:$content-name content ) )
( :path . , path )
2017-04-20 07:47:05 -05:00
( :format . , ( or ( ein:$content-format content ) " json " ) )
2016-03-01 16:02:00 -06:00
( :content ,@ ( ein:$content-raw-content content ) ) ) ) ) )
2015-02-12 08:45:29 -06:00
( defun ein:content-from-notebook ( nb )
( let ( ( nb-content ( ein:notebook-to-json nb ) ) )
( make-ein:$content :name ( ein:$notebook-notebook-name nb )
:path ( ein:$notebook-notebook-path nb )
2015-02-16 13:06:35 -06:00
:url-or-port ( ein:$notebook-url-or-port nb )
2015-02-12 08:45:29 -06:00
:type " notebook "
:ipython-version ( ein:$notebook-api-version nb )
:raw-content nb-content ) ) )
2017-02-17 15:06:59 -06:00
( defun* ein:content-query-contents-error ( url retry-p packed &key symbol-status response &allow-other-keys )
( if ( and ( eql symbol-status 'parse-error )
( not retry-p ) )
2016-12-22 12:19:52 -06:00
( progn
2017-02-17 15:06:59 -06:00
( message " Content list call failed, maybe because curl hasn't updated it's cookie jar yet? Let's try one more time.... " )
( apply #' ein:content-query-contents packed ) )
2016-12-22 12:19:52 -06:00
( progn
( ein:log 'verbose
" Error thrown: %S " ( request-response-error-thrown response ) )
( ein:log 'error
" Content list call %s failed with status %s. " url symbol-status ) ) ) )
2015-01-31 10:13:49 -06:00
2016-10-19 11:57:34 -05:00
;;; Managing/listing the content hierarchy
2015-01-31 10:13:49 -06:00
( defvar *ein:content-hierarchy* ( make-hash-table ) )
2015-12-04 14:15:41 -06:00
( defun ein:get-content-hierarchy ( url-or-port )
( or ( gethash url-or-port *ein:content-hierarchy* )
( ein:refresh-content-hierarchy url-or-port ) ) )
2015-01-31 10:13:49 -06:00
( defun ein:make-content-hierarchy ( path url-or-port )
2015-02-09 13:27:20 -06:00
( let* ( ( node ( ein:content-query-contents path url-or-port t ) )
2015-05-27 21:49:48 -05:00
( active-sessions ( make-hash-table :test 'equal ) )
2015-01-31 10:13:49 -06:00
( items ( ein:$content-raw-content node ) ) )
2015-12-04 14:15:41 -06:00
( ein:content-query-sessions active-sessions url-or-port t )
2015-01-31 10:13:49 -06:00
( ein:flatten ( loop for item in items
2015-05-27 21:49:48 -05:00
for c = ( make-ein:$content :url-or-port url-or-port )
2015-02-10 14:53:08 -06:00
do ( ein:new-content c nil :data item )
2015-01-31 10:13:49 -06:00
collect
( cond ( ( string= ( ein:$content-type c ) " directory " )
( cons c
( ein:make-content-hierarchy ( ein:$content-path c ) url-or-port ) ) )
2016-12-05 09:14:41 -05:00
( t ( progn
2016-12-01 21:10:20 -06:00
( setf ( ein:$content-session-p c )
( gethash ( ein:$content-path c ) active-sessions ) )
c ) ) ) ) ) ) )
2015-01-31 10:13:49 -06:00
( defun ein:refresh-content-hierarchy ( &optional url-or-port )
( let ( ( url-or-port ( or url-or-port ( ein:default-url-or-port ) ) ) )
( setf ( gethash url-or-port *ein:content-hierarchy* )
2015-05-27 21:49:48 -05:00
( ein:make-content-hierarchy " " url-or-port ) ) ) )
2015-01-31 10:13:49 -06:00
2016-10-19 11:57:34 -05:00
2015-02-12 08:45:29 -06:00
;;; Save Content
2016-01-11 15:11:14 -07:00
( defun ein:content-save-legacy ( content &optional callback cbargs errcb errcbargs )
2015-02-16 13:06:35 -06:00
( ein:query-singleton-ajax
2015-02-16 21:14:47 -06:00
( list 'content-save ( ein:$content-url-or-port content ) ( ein:$content-path content ) )
( ein:content-url-legacy content )
:type " PUT "
:headers ' ( ( " Content-Type " . " application/json " ) )
:timeout ein:content-query-timeout
:data ( ein:content-to-json content )
:success ( apply-partially #' ein:content-save-success callback cbargs )
2016-01-11 15:11:14 -07:00
:error ( apply-partially #' ein:content-save-error ( ein:content-url-legacy content ) errcb errcbargs ) ) )
2015-02-16 21:14:47 -06:00
2016-01-11 15:11:14 -07:00
( defun ein:content-save ( content &optional callback cbargs errcb errcbargs )
2015-02-16 21:14:47 -06:00
( if ( >= ( ein:$content-ipython-version content ) 3 )
( ein:query-singleton-ajax
( list 'content-save ( ein:$content-url-or-port content ) ( ein:$content-path content ) )
( ein:content-url content )
:type " PUT "
:headers ' ( ( " Content-Type " . " application/json " ) )
:timeout ein:content-query-timeout
2016-12-01 21:12:43 -06:00
:data ( encode-coding-string ( ein:content-to-json content ) buffer-file-coding-system )
2015-02-16 21:14:47 -06:00
:success ( apply-partially #' ein:content-save-success callback cbargs )
2016-01-11 15:11:14 -07:00
:error ( apply-partially #' ein:content-save-error ( ein:content-url content ) errcb errcbargs ) )
2015-02-16 21:14:47 -06:00
( ein:content-save-legacy content callback cbargs ) ) )
2015-02-12 08:45:29 -06:00
( defun* ein:content-save-success ( callback cbargs &key status response &allow-other-keys )
2016-11-05 17:49:52 -05:00
;;(ein:log 'verbose "Saving content successful with status %s" status)
2015-02-12 08:45:29 -06:00
( when callback
( apply callback cbargs ) ) )
2016-01-11 15:11:14 -07:00
( defun* ein:content-save-error ( url errcb errcbargs &key response status-code &allow-other-keys )
2016-10-23 19:26:21 -05:00
( ein:log 'error
2015-02-12 08:45:29 -06:00
" Error thrown: %S " ( request-response-error-thrown response ) )
( ein:log 'error
2016-10-23 19:26:21 -05:00
" Content save call %s failed with status %s. " url status-code )
2016-01-11 15:11:14 -07:00
( when errcb
( apply errcb errcbargs ) ) )
2015-02-12 08:45:29 -06:00
2016-10-19 11:57:34 -05:00
2015-02-12 08:45:29 -06:00
;;; Rename Content
2015-01-31 10:31:20 -06:00
2015-02-16 13:06:35 -06:00
( defun ein:content-legacy-rename ( content new-path callback cbargs )
( let ( ( path ( substring new-path 0 ( or ( position ?/ new-path :from-end t ) 0 ) ) )
( name ( substring new-path ( or ( position ?/ new-path :from-end t ) 0 ) ) ) )
2015-01-31 10:31:20 -06:00
( ein:query-singleton-ajax
2015-02-16 13:06:35 -06:00
( list 'content-rename ( ein:$content-url-or-port content ) ( ein:$content-path content ) )
( ein:content-url-legacy content )
2015-01-31 10:31:20 -06:00
:type " PATCH "
2015-02-16 13:06:35 -06:00
:data ( json-encode ` ( ( name . , name )
( path . , path ) ) )
2015-01-31 10:32:52 -06:00
:parser #' ein:json-read
2015-02-16 21:14:47 -06:00
:success ( apply-partially #' update-content-path-legacy content callback cbargs )
2015-02-16 13:06:35 -06:00
:error ( apply-partially #' ein:content-rename-error new-path ) ) ) )
2015-01-31 10:31:20 -06:00
2015-02-16 21:14:47 -06:00
( defun* update-content-path-legacy ( content callback cbargs &key data &allow-other-keys )
( setf ( ein:$content-path content ) ( ein:trim-left ( format " %s/%s " ( plist-get data :path ) ( plist-get data :name ) )
" / " )
( ein:$content-name content ) ( plist-get data :name )
( ein:$content-last-modified content ) ( plist-get data :last_modified ) )
( when callback
( apply callback cbargs ) ) )
2015-02-16 13:06:35 -06:00
( defun ein:content-rename ( content new-path &optional callback cbargs )
( if ( >= ( ein:$content-ipython-version content ) 3 )
( ein:query-singleton-ajax
( list 'content-rename ( ein:$content-url-or-port content ) ( ein:$content-path content ) )
( ein:content-url content )
:type " PATCH "
:data ( json-encode ` ( ( path . , new-path ) ) )
:parser #' ein:json-read
:success ( apply-partially #' update-content-path content callback cbargs )
:error ( apply-partially #' ein:content-rename-error ( ein:$content-path content ) ) )
( ein:content-legacy-rename content new-path callback cbargs ) ) )
( defun* update-content-path ( content callback cbargs &key data &allow-other-keys )
2015-01-31 10:31:20 -06:00
( setf ( ein:$content-path content ) ( plist-get data :path )
( ein:$content-name content ) ( plist-get data :name )
2015-02-16 13:06:35 -06:00
( ein:$content-last-modified content ) ( plist-get data :last_modified ) )
( when callback
( apply callback cbargs ) ) )
2015-01-31 10:31:20 -06:00
( defun* ein:content-rename-error ( path &key symbol-status response &allow-other-keys )
( ein:log 'verbose
" Error thrown: %S " ( request-response-error-thrown response ) )
( ein:log 'error
" Renaming content %s failed with status %s. " path symbol-status ) )
2015-02-09 13:27:20 -06:00
2016-10-19 11:57:34 -05:00
2015-05-27 21:49:48 -05:00
;;; Sessions
( defun ein:content-query-sessions ( session-hash url-or-port &optional force-sync )
2016-10-29 09:43:40 -05:00
( unless force-sync
( setq force-sync ein:force-sync ) )
2015-05-27 21:49:48 -05:00
( ein:query-singleton-ajax
2015-12-04 14:15:41 -06:00
( list 'content-query-sessions )
2015-05-27 21:49:48 -05:00
( ein:url url-or-port " api/sessions " )
:type " GET "
:parser #' ein:json-read
2016-10-19 11:57:34 -05:00
:success ( apply-partially #' ein:content-query-sessions-success session-hash url-or-port )
2015-05-27 21:49:48 -05:00
:error #' ein:content-query-sessions-error
2016-10-29 09:43:40 -05:00
:sync force-sync ) )
2015-05-27 21:49:48 -05:00
( defun* ein:content-query-sessions-success ( session-hash url-or-port &key data &allow-other-keys )
( cl-flet ( ( read-name ( nb-json )
( if ( = ( ein:query-ipython-version url-or-port ) 2 )
( if ( string= ( plist-get nb-json :path ) " " )
( plist-get nb-json :name )
( format " %s/%s " ( plist-get nb-json :path ) ( plist-get nb-json :name ) ) )
( plist-get nb-json :path ) ) ) )
( dolist ( s data )
( setf ( gethash ( read-name ( plist-get s :notebook ) ) session-hash )
( cons ( plist-get s :id ) ( plist-get s :kernel ) ) ) )
session-hash ) )
( defun* ein:content-query-sessions-error ( &key symbol-status response &allow-other-keys )
( ein:log 'error " Session query failed with status %s (%s). " symbol-status response ) )
2016-10-19 11:57:34 -05:00
;;; Checkpoints
( defun ein:content-query-checkpoints ( content &optional callback cbargs )
( let* ( ( url ( ein:content-url content " checkpoints " ) ) )
( ein:query-singleton-ajax
( list 'content-query-checkpoints url )
url
:type " GET "
:timeout ein:content-query-timeout
:parser #' ein:json-read
:sync ein:force-sync
:success ( apply-partially #' ein:content-query-checkpoints-success content callback cbargs )
:error ( apply-partially #' ein:content-query-checkpoints-error content ) ) ) )
( defun ein:content-create-checkpoint ( content &optional callback cbargs )
( let* ( ( url ( ein:content-url content " checkpoints " ) ) )
( ein:query-singleton-ajax
( list 'content-query-checkpoints url )
url
:type " POST "
:timeout ein:content-query-timeout
:parser #' ein:json-read
:sync ein:force-sync
:success ( apply-partially #' ein:content-query-checkpoints-success content callback cbargs )
:error ( apply-partially #' ein:content-query-checkpoints-error content ) ) ) )
( defun ein:content-restore-checkpoint ( content checkpoint-id &optional callback cbargs )
( let* ( ( url ( ein:content-url content " checkpoints " checkpoint-id ) ) )
( ein:query-singleton-ajax
( list 'content-query-checkpoints url )
url
:type " POST "
:timeout ein:content-query-timeout
:parser #' ein:json-read
:sync ein:force-sync
:success ( when callback
( apply callback cbargs ) )
:error ( apply-partially #' ein:content-query-checkpoints-error content ) ) ) )
( defun ein:content-delete-checkpoint ( content checkpoint-id &optional callback cbargs )
( let* ( ( url ( ein:content-url content " checkpoints " checkpoint-id ) ) )
( ein:query-singleton-ajax
( list 'content-query-checkpoints url )
url
:type " DELETE "
:timeout ein:content-query-timeout
:parser #' ein:json-read
:sync ein:force-sync
:success ( when callback
( apply callback cbargs ) )
:error ( apply-partially #' ein:content-query-checkpoints-error content ) ) ) )
( defun* ein:content-query-checkpoints-success ( content cb cbargs &key data status response &allow-other-keys )
2016-10-19 14:43:19 -05:00
( unless ( listp ( car data ) )
( setq data ( list data ) ) )
2016-10-19 11:57:34 -05:00
( setf ( ein:$content-checkpoints content ) data )
( when cb
2016-10-19 14:43:19 -05:00
( apply cb content cbargs ) ) )
2016-10-19 11:57:34 -05:00
( defun* ein:content-query-checkpoints-error ( content &key symbol-status response &allow-other-keys )
( ein:log 'error " Content checkpoint operation failed with status %s (%s). " symbol-status response ) )
2017-04-02 14:33:56 -05:00
;;; Uploads
( defun ein:get-local-file ( path )
" If path exists, get contents and try to guess type of file (one of file, notebook, or directory)
and content format ( one of json, text, or base64 ) . "
( unless ( file-readable-p path )
( error " File %s is not accessible and cannot be uploaded. " path ) )
( let ( ( name ( file-name-nondirectory path ) )
( type ( file-name-extension path ) ) )
( with-temp-buffer
( insert-file-contents path )
( cond ( ( string= type " ipynb " )
( list name " notebook " " json " ( buffer-string ) ) )
( ( eql buffer-file-coding-system 'no-conversion )
( list name " file " " base64 " ( buffer-string ) ) )
( t ( list name " file " " text " ( buffer-string ) ) ) ) ) ) )
( defun ein:content-upload ( path uploaded-file-path &optional url-or-port )
( multiple-value-bind ( name type format contents ) ( ein:get-local-file uploaded-file-path )
( let* ( ( content ( make-ein:$content :url-or-port ( or url-or-port ( ein:default-url-or-port ) )
:name name
:path ( concat path " / " name )
:raw-content contents ) )
( data ( make-hash-table ) ) )
( setf ( gethash 'path data ) path
( gethash 'name data ) name
( gethash 'type data ) type
( gethash 'format data ) format
( gethash 'content data ) contents )
( ein:query-singleton-ajax
( list 'content-upload name )
( ein:content-url content )
:type " PUT "
:headers ' ( ( " Content-Type " . " application/json " ) )
:timeout ein:content-query-timeout
:data ( json-encode data )
:success ( lexical-let ( ( uploaded-file-path uploaded-file-path ) )
#' ( lambda ( &rest -ignore- ) ( message " File %s succesfully uploaded. " uploaded-file-path ) ) )
:error ( apply-partially #' ein:content-upload-error uploaded-file-path ) ) ) ) )
( cl-defun ein:content-upload-error ( path &key symbol-status response &allow-other-keys )
( ein:display-warning ( format " Could not upload %s. Failed with status %s " path symbol-status ) ) )