41 lines
1.5 KiB
Common Lisp
41 lines
1.5 KiB
Common Lisp
;;;; cl-scopes/web/client - web client functionality
|
|
|
|
(defpackage :scopes/web/client
|
|
(:use :common-lisp)
|
|
(:local-nicknames (:config :scopes/config)
|
|
(:core :scopes/core)
|
|
(:message :scopes/core/message)
|
|
(:alx :alexandria))
|
|
(:export #:config #:base-url #:api-path #:doc-path
|
|
#:get-page #:send-message))
|
|
|
|
(in-package :scopes/web/client)
|
|
|
|
(defclass config (config:base)
|
|
((config:setup :initform #'core:default-setup)
|
|
(base-url :reader base-url :initarg :base-url :initform "http://localhost:8135")
|
|
(doc-path :reader doc-path :initarg :doc-path :initform "/")
|
|
(api-path :reader api-path :initarg :api-path :initform "/api/")))
|
|
|
|
;;;; client context (= service)
|
|
|
|
(defun get-page (ctx msg)
|
|
(let* ((cfg (core:config ctx))
|
|
(path (getf (message:data msg) :path))
|
|
(url (str:concat (base-url cfg) (doc-path cfg) path)))
|
|
(dex:get url)))
|
|
|
|
(defun send-message (ctx msg)
|
|
(let* ((cfg (core:config ctx))
|
|
(url (str:concat (base-url cfg) (api-path cfg) (msgpath msg))))
|
|
(dex:post url :content (data-as-alist (message:data msg)))))
|
|
|
|
(defun msgpath (msg)
|
|
(let ((lst (loop for p in (message:head-as-list msg) when p collect p)))
|
|
(str:join "/" (mapcar #'string-downcase lst))))
|
|
|
|
(defun data-as-alist (data)
|
|
(if (symbolp (car data)) ; seems to be a property list
|
|
(mapcar #'(lambda (p) (cons (string-downcase (car p)) (cdr p)))
|
|
(alx:plist-alist data))
|
|
data))
|