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 )
2018-10-11 16:53:02 -04:00
( provide 'ein-notebook ) ; see manual "Named Features" regarding recursive requires
( require 'ein-notebook )
2015-01-31 10:13:49 -06:00
2018-10-24 13:12:16 -04:00
( defcustom ein:content-query-max-depth 2
" Don't recurse the directory tree deeper than this. "
:type 'integer
:group 'ein )
( defcustom ein:content-query-max-branch 6
" Don't descend into more than this number of directories per depth. The total number of parallel queries should therefore be O({max_branch}^{max_depth}) "
:type 'integer
:group 'ein )
2019-02-17 21:10:08 -06:00
( defcustom ein:content-query-timeout nil ; (* 60 1000) ;1 min
2015-02-10 14:53:08 -06:00
" 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 )
2018-10-01 18:40:31 -04:00
( apply #' ein:content-url* ( ein:$content-url-or-port content ) ( ein:$content-path content ) params ) )
( defun ein:content-url* ( url-or-port path &rest params )
2018-12-25 10:59:18 -05:00
( let* ( ( which ( if ( < ( ein:notebook-version-numeric url-or-port ) 3 )
2018-10-01 18:40:31 -04:00
" notebooks " " contents " ) )
( api-path ( concat " api/ " which ) ) )
( url-encode-url ( apply #' ein:url
url-or-port
api-path
path
params ) ) ) )
2018-10-28 14:27:09 -04:00
( defun ein:content-query-contents ( url-or-port path callback errback &optional iteration )
" Register CALLBACK of arity 1 for the contents at PATH from the URL-OR-PORT. ERRBACK of arity 1 for the contents. "
2018-10-18 19:01:43 -04:00
( unless iteration
( setq iteration 0 ) )
2018-10-01 18:40:31 -04:00
( ein:query-singleton-ajax
( list 'content-query-contents url-or-port path )
( ein:content-url* url-or-port path )
:type " GET "
:timeout ein:content-query-timeout
:parser #' ein:json-read
:sync ein:force-sync
:complete ( apply-partially #' ein:content-query-contents--complete url-or-port path )
:success ( apply-partially #' ein:content-query-contents--success url-or-port path callback )
2019-02-14 15:28:18 -05:00
:error ( apply-partially #' ein:content-query-contents--error url-or-port path callback errback iteration ) ) )
2018-10-01 18:40:31 -04:00
( defun* ein:content-query-contents--complete ( url-or-port path
&key data symbol-status response
&allow-other-keys
&aux ( resp-string ( format " STATUS: %s DATA: %s " ( request-response-status-code response ) data ) ) )
( ein:log 'debug " ein:query-contents--complete %s " resp-string ) )
2019-02-14 15:28:18 -05:00
( defun* ein:content-query-contents--error ( url-or-port path callback errback iteration &key symbol-status response error-thrown data &allow-other-keys )
( let ( ( status-code ( request-response-status-code response ) ) )
( case status-code
( 404 ( ein:log 'error " ein:content-query-contents--error %s %s "
( request-response-status-code response ) ( plist-get data :message ) )
( when errback ( funcall errback url-or-port status-code ) ) )
( t ( if ( < iteration ( if noninteractive 6 3 ) )
( progn
( ein:log 'verbose " Retry content-query-contents #%s in response to %s " iteration status-code )
( sleep-for 0 ( * ( 1+ iteration ) 500 ) )
( ein:content-query-contents url-or-port path callback errback ( 1+ iteration ) ) )
( let ( ( notice
( format " ein:content-query-contents--error %s REQUEST-STATUS %s DATA %s "
( concat ( file-name-as-directory url-or-port ) path )
symbol-status ( cdr error-thrown ) ) ) )
( if ( and ( = status-code 403 ) noninteractive )
( progn
2019-02-20 08:24:07 -05:00
( ein:log 'info notice )
2019-02-14 15:28:18 -05:00
( when callback
( funcall callback ( ein:new-content url-or-port path data ) ) ) )
( ein:log 'error notice )
( when errback ( funcall errback url-or-port status-code ) ) ) ) ) ) ) ) )
2018-10-01 18:40:31 -04: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:content-query-contents--success ( url-or-port path callback
2018-10-12 11:33:20 -05:00
&key data symbol-status response
2018-10-01 18:40:31 -04:00
&allow-other-keys )
( let ( content )
2018-12-25 10:59:18 -05:00
( if ( < ( ein:notebook-version-numeric url-or-port ) 3 )
2018-10-01 18:40:31 -04:00
( setq content ( ein:new-content-legacy url-or-port path data ) )
( setq content ( ein:new-content url-or-port path data ) ) )
( ein:aif response
( setf ( ein:$content-url-or-port content ) ( ein:get-response-redirect it ) ) )
2018-10-24 13:12:16 -04:00
( when callback
( funcall callback content ) ) ) )
2015-01-31 10:13:49 -06:00
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:content-to-json ( content )
2018-10-18 19:01:43 -04:00
( let ( ( path ( if ( >= ( ein:$content-notebook-version content ) 3 )
2015-02-12 08:45:29 -06:00
( 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 "
2018-10-18 19:01:43 -04:00
:notebook-version ( ein:$notebook-api-version nb )
2015-02-12 08:45:29 -06:00
:raw-content nb-content ) ) )
2016-10-19 11:57:34 -05:00
;;; Managing/listing the content hierarchy
2015-01-31 10:13:49 -06:00
2018-10-01 18:40:31 -04:00
( defvar *ein:content-hierarchy* ( make-hash-table :test #' equal )
" Content tree keyed by URL-OR-PORT. " )
( defun ein:content-need-hierarchy ( url-or-port )
" Callers assume ein:content-query-hierarchy succeeded. If not, nil. "
( ein:aif ( gethash url-or-port *ein:content-hierarchy* ) it
( ein:log 'warn " No recorded content hierarchy for %s " url-or-port )
nil ) )
( defun ein:new-content-legacy ( url-or-port path data )
" Content API in 2.x a bit inconsistent. "
( if ( plist-get data :type )
( ein:new-content url-or-port path data )
( let ( ( content ( make-ein:$content
:url-or-port url-or-port
2018-12-25 10:59:18 -05:00
:notebook-version ( ein:notebook-version-numeric url-or-port )
2018-10-01 18:40:31 -04:00
:path path ) ) )
( setf ( ein:$content-name content ) ( substring path ( or ( cl-position ?/ path ) 0 ) )
( ein:$content-path content ) path
( 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 ) )
content ) ) )
( defun ein:new-content ( url-or-port path data )
;; data is like (:size 72 :content nil :writable t :path Untitled7.ipynb :name Untitled7.ipynb :type notebook)
( let ( ( content ( make-ein:$content
:url-or-port url-or-port
2018-12-25 10:59:18 -05:00
:notebook-version ( ein:notebook-version-numeric url-or-port )
2018-10-01 18:40:31 -04:00
:path path ) ) )
( 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 )
( ein:$content-raw-content content ) ( plist-get data :content ) )
content ) )
2018-10-24 13:12:16 -04:00
( defun ein:content-query-hierarchy* ( url-or-port path callback sessions depth content )
2018-12-01 18:54:58 -05:00
" Returns list (tree) of content objects. CALLBACK accepts tree. "
2018-10-07 03:10:29 -04:00
( lexical-let* ( ( url-or-port url-or-port )
( path path )
( callback callback )
( items ( ein:$content-raw-content content ) )
2018-10-24 13:12:16 -04:00
( directories ( if ( < depth ein:content-query-max-depth )
( loop for item in items
with result
until ( >= ( length result ) ein:content-query-max-branch )
if ( string= " directory " ( plist-get item :type ) )
collect ( ein:new-content url-or-port path item )
into result
end
finally return result ) ) )
2018-10-07 03:10:29 -04:00
( others ( loop for item in items
with c0
if ( not ( string= " directory " ( plist-get item :type ) ) )
2018-10-24 13:12:16 -04:00
do ( setf c0 ( ein:new-content url-or-port path item ) )
2018-12-01 18:54:58 -05:00
( setf ( ein:$content-session-p c0 )
( gethash ( ein:$content-path c0 ) sessions ) )
2018-10-24 13:12:16 -04:00
and collect c0
2018-10-07 03:10:29 -04:00
end ) ) )
2018-10-01 18:40:31 -04:00
( deferred:$
( apply #' deferred:parallel
2018-10-07 03:10:29 -04:00
( loop for c0 in directories
2015-01-31 10:13:49 -06:00
collect
2018-10-12 11:33:20 -05:00
( lexical-let ( ( c0 c0 )
( d0 ( deferred:new #' identity ) ) )
( ein:content-query-contents
url-or-port
2018-10-07 03:10:29 -04:00
( ein:$content-path c0 )
2018-10-12 11:33:20 -05:00
( apply-partially #' ein:content-query-hierarchy*
url-or-port
( ein:$content-path c0 )
( lambda ( tree )
( deferred:callback-post d0 ( cons c0 tree ) ) )
2018-10-28 14:27:09 -04:00
sessions ( 1+ depth ) )
( lambda ( &rest ignore ) ( deferred:callback-post d0 ( cons c0 nil ) ) ) )
2018-10-07 03:10:29 -04:00
d0 ) ) )
2018-10-01 18:40:31 -04:00
( deferred:nextc it
( lambda ( tree )
2018-10-07 03:10:29 -04:00
( let ( ( result ( append others tree ) ) )
2018-12-01 18:54:58 -05:00
( when ( string= path " " )
( setf ( gethash url-or-port *ein:content-hierarchy* ) ( -flatten result ) ) )
2018-10-07 03:10:29 -04:00
( funcall callback result ) ) ) ) ) ) )
2018-10-01 18:40:31 -04:00
( defun ein:content-query-hierarchy ( url-or-port callback )
" Send for content hierarchy of URL-OR-PORT with CALLBACK arity 1 for content hierarchy "
2018-10-28 12:22:38 -04:00
( ein:content-query-sessions
url-or-port
( apply-partially ( lambda ( url-or-port* callback* sessions )
( ein:content-query-contents url-or-port* " "
( apply-partially #' ein:content-query-hierarchy*
url-or-port*
" "
2018-10-28 14:27:09 -04:00
callback* sessions 0 )
( lambda ( &rest ignore )
( when callback* ( funcall callback* nil ) ) ) ) )
url-or-port callback )
callback ) )
2016-10-19 11:57:34 -05:00
2015-02-12 08:45:29 -06:00
;;; Save Content
2018-10-01 18:40:31 -04:00
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
2018-10-01 18:40:31 -04:00
( 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
:data ( ein:content-to-json content )
:success ( apply-partially #' ein:content-save-success callback cbargs )
:error ( apply-partially #' ein:content-save-error ( ein:content-url 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 )
2018-10-18 19:01:43 -04:00
( if ( >= ( ein:$content-notebook-version content ) 3 )
2015-02-16 21:14:47 -06:00
( 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 ) ) )
2019-02-14 15:28:18 -05:00
( defun* ein:content-save-error ( url errcb errcbargs &key response data &allow-other-keys )
2016-10-23 19:26:21 -05:00
( ein:log 'error
2019-02-14 15:28:18 -05:00
" Content save %s failed %s %s. "
url ( request-response-error-thrown response ) ( plist-get data :message ) )
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
2018-10-01 18:40:31 -04: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 ) )
2018-10-01 18:40:31 -04:00
( ein:content-url 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 )
2018-10-18 19:01:43 -04:00
( if ( >= ( ein:$content-notebook-version content ) 3 )
2015-02-16 13:06:35 -06:00
( 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 ) ) )
2019-05-16 15:01:45 -04:00
( defun ein:session-rename ( url-or-port session-id new-path )
( ein:query-singleton-ajax
( list 'session-rename session-id new-path )
( ein:url url-or-port " api/sessions " session-id )
:type " PATCH "
:data ( json-encode ` ( ( path . , new-path ) ) )
:complete #' ein:session-rename--complete ) )
( defun* ein:session-rename--complete ( &key data response symbol-status
&allow-other-keys
&aux ( resp-string ( format " STATUS: %s DATA: %s " ( request-response-status-code response ) data ) ) )
( ein:log 'debug " ein:session-rename--complete %s " resp-string ) )
2015-02-16 13:06:35 -06:00
( 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
2019-02-14 15:28:18 -05:00
( defun* ein:content-rename-error ( path &key response data &allow-other-keys )
2015-01-31 10:31:20 -06:00
( ein:log 'error
2019-02-14 15:28:18 -05:00
" Renaming content %s failed %s %s. "
path ( request-response-error-thrown response ) ( plist-get data :message ) ) )
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
2018-10-28 14:27:09 -04:00
( defun ein:content-query-sessions ( url-or-port callback errback &optional iteration )
2018-10-28 12:22:38 -04:00
" Register CALLBACK of arity 1 to retrieve the sessions. Call ERRBACK of arity 1 (contents) upon failure. "
2018-10-20 20:45:22 -04:00
( unless iteration
( setq iteration 0 ) )
2018-10-28 14:27:09 -04:00
( unless callback
( setq callback #' ignore ) )
( unless errback
( setq errback #' ignore ) )
2018-10-01 18:40:31 -04:00
( ein:query-singleton-ajax
( list 'content-query-sessions url-or-port )
( ein:url url-or-port " api/sessions " )
:type " GET "
:parser #' ein:json-read
:complete ( apply-partially #' ein:content-query-sessions--complete url-or-port callback )
:success ( apply-partially #' ein:content-query-sessions--success url-or-port callback )
2018-10-28 14:27:09 -04:00
:error ( apply-partially #' ein:content-query-sessions--error url-or-port callback errback iteration )
2018-10-01 18:40:31 -04:00
:sync ein:force-sync ) )
( defun* ein:content-query-sessions--success ( url-or-port callback &key data &allow-other-keys )
2015-05-27 21:49:48 -05:00
( cl-flet ( ( read-name ( nb-json )
2018-12-25 10:59:18 -05:00
( if ( < ( ein:notebook-version-numeric url-or-port ) 3 )
2015-05-27 21:49:48 -05:00
( 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 ) ) ) )
2018-10-01 18:40:31 -04:00
( let ( ( session-hash ( make-hash-table :test 'equal ) ) )
( dolist ( s data ( funcall callback session-hash ) )
( setf ( gethash ( read-name ( plist-get s :notebook ) ) session-hash )
( cons ( plist-get s :id ) ( plist-get s :kernel ) ) ) ) ) ) )
2018-10-28 14:27:09 -04:00
( defun* ein:content-query-sessions--error ( url-or-port callback errback iteration
2018-10-20 20:45:22 -04:00
&key response error-thrown
&allow-other-keys )
2018-12-02 17:23:55 -05:00
( if ( < iteration ( if noninteractive 6 3 ) )
2018-10-20 20:45:22 -04:00
( progn
2018-10-24 13:12:16 -04:00
( ein:log 'verbose " Retry sessions #%s in response to %s " iteration ( request-response-status-code response ) )
2018-10-30 14:17:29 -04:00
( sleep-for 0 ( * ( 1+ iteration ) 500 ) )
2018-10-28 14:27:09 -04:00
( ein:content-query-sessions url-or-port callback errback ( 1+ iteration ) ) )
2018-10-28 12:22:38 -04:00
( ein:log 'error " ein:content-query-sessions--error %s: ERROR %s DATA %s " url-or-port ( car error-thrown ) ( cdr error-thrown ) )
( when errback ( funcall errback nil ) ) ) )
2015-05-27 21:49:48 -05:00
2018-10-12 11:33:20 -05:00
( defun* ein:content-query-sessions--complete ( url-or-port callback
&key data response
&allow-other-keys
2018-10-01 18:40:31 -04:00
&aux ( resp-string ( format " STATUS: %s DATA: %s " ( request-response-status-code response ) data ) ) )
( ein:log 'debug " ein:query-sessions--complete %s " resp-string ) )
2015-05-27 21:49:48 -05:00
2016-10-19 11:57:34 -05:00
;;; Checkpoints
2018-10-01 18:40:31 -04:00
2016-10-19 11:57:34 -05:00
( 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
2018-10-01 18:40:31 -04:00
2017-04-02 14:33:56 -05:00
( 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 ) ) )
2018-10-11 16:53:02 -04:00
( provide 'ein-contents-api )