Commit 89d52b8f authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

messages: Provide a simpler 'header' procedure and export it.

* jupyter/messages.scm (<header>): Rename contructor to '%header'.
(%protocol-version): New variable.
(current-date-string, header, make-header): New procedures.
(reply): Adjust to new 'header' API.
(string->header): Likewise.
parent 16553955
......@@ -38,6 +38,8 @@
message-parts
parts->message
header
make-header
header?
header-id
header-user
......@@ -57,7 +59,7 @@
;; Jupyter message header as defined at
;; <https://jupyter-client.readthedocs.io/en/stable/messaging.html#general-message-format>.
(define-record-type <header>
(header id user session date type version sender)
(%header id user session date type version sender)
header?
(id header-id) ;bytevector (UUID)
(user header-user) ;string
......@@ -70,6 +72,24 @@
;; itself.
(sender header-sender)) ;bytevector (UUID) | #f
(define %protocol-version "5.0")
(define (current-date-string)
(date->string (time-utc->date (current-time time-utc))
"~5.~N"))
(define* (header type user session
#:key
(id (make-id))
(date (current-date-string))
(version %protocol-version)
sender)
"Return a new Jupyter message header."
(%header id user session date type version sender))
(define make-header header)
;; Jupyter message.
(define-record-type <message>
(%message header parent-header metadata content buffers)
......@@ -122,14 +142,8 @@ This is a low-level procedure for internal use."
(define (reply message type content)
"Return a Jupyter message that is a reply to MESSAGE."
(let ((parent (message-header message)))
(make-message (header (make-id) (header-user parent)
(header-session parent)
(date->string (time-utc->date
(current-time time-utc))
"~5.~N")
type
(header-version parent)
#f)
(make-message (header type (header-user parent)
(header-session parent))
content
#:parent-header parent)))
......@@ -147,13 +161,13 @@ record. Return #f if STR is the empty dictionary."
(let ((table (json-string->scm str)))
(if (zero? (hash-count (const #t) table))
#f
(header (hash-ref table "msg_id")
(header (hash-ref table "msg_type")
(hash-ref table "username")
(hash-ref table "session")
(hash-ref table "date")
(hash-ref table "msg_type")
(hash-ref table "version")
sender))))
#:id (hash-ref table "msg_id")
#:date (hash-ref table "date")
#:version (hash-ref table "version")
#:sender sender))))
(define (header->string header)
"Return HEADER as a JSON string."
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment