ement.el/ement-api.el

112 lines
3.9 KiB
EmacsLisp
Raw Normal View History

2020-11-28 05:33:16 -06:00
;;; ement-api.el --- Matrix API library -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Adam Porter
;; Author: Adam Porter <adam@alphapapa.net>
;; Keywords: comm
2020-11-28 05:33:16 -06:00
;; This program 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.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
2020-11-28 07:41:13 -06:00
;;;; Debugging
(eval-and-compile
(setq-local warning-minimum-log-level nil)
(setq-local warning-minimum-log-level :debug))
2020-11-28 05:33:16 -06:00
;;;; Requirements
(require 'json)
(require 'url-parse)
(require 'url-util)
(require 'plz)
(require 'ement-macros)
2020-12-01 03:24:03 -06:00
(require 'ement-structs)
2020-11-28 05:33:16 -06:00
;;;; Variables
;;;; Customization
;;;; Commands
;;;; Functions
2020-12-01 03:24:03 -06:00
(cl-defun ement-api (server token endpoint then
2020-12-01 05:16:39 -06:00
&key timeout data params
2020-12-01 03:24:03 -06:00
(content-type "application/json")
(data-type 'text)
2020-12-01 03:24:03 -06:00
(else #'ement-api-error) (method 'get)
;; FIXME: What's the right term for the URL part after "/_matrix/"?
(endpoint-category "client")
2020-12-01 03:24:03 -06:00
(json-read-fn #'json-read))
"FIXME: Docstring."
2020-11-28 05:33:16 -06:00
(declare (indent defun))
(pcase-let* (((cl-struct ement-server uri-prefix port) server)
((cl-struct url type host) (url-generic-parse-url uri-prefix))
(path (format "/_matrix/%s/r0/%s" endpoint-category endpoint))
2020-11-30 18:19:23 -06:00
(query (url-build-query-string params))
2020-11-30 15:08:12 -06:00
(filename (concat path "?" query))
(url (url-recreate-url
(url-parse-make-urlobj type nil nil host port filename nil data t)))
2020-11-28 05:33:16 -06:00
(headers (ement-alist "Content-Type" content-type
"Authorization" (concat "Bearer " token))))
2020-11-30 15:08:12 -06:00
;; Omit `then' from debugging because if it's a partially applied
;; function on the session object, which may be very large, it
;; will take a very long time to print into the warnings buffer.
2020-12-01 16:45:40 -06:00
;; (ement-debug (current-time) method url headers)
(plz method url :headers headers :body data :body-type data-type
:as json-read-fn :then then :else else
2020-12-01 05:16:39 -06:00
;; FIXME: Timeout is not necessarily the same as connect-timeout, or shouldn't be.
:connect-timeout timeout :noquery t)))
2020-11-28 07:41:13 -06:00
(define-error 'ement-api-error "Ement API error" 'error)
(defun ement-api-error (plz-error)
"Signal an Ement API error for PLZ-ERROR."
;; This feels a little messy, but it seems to be reasonable.
(pcase-let* (((cl-struct plz-error response
(message plz-message) (curl-error `(,curl-exit-code . ,curl-message)))
plz-error)
(status (when (plz-response-p response)
(plz-response-status response)))
(body (when (plz-response-p response)
(plz-response-body response)))
(json-object (when body
(ignore-errors
(json-read-from-string body))))
(error-message (format "%S: %s"
(or curl-exit-code status)
(or (when json-object
(alist-get 'error json-object))
curl-message
plz-message))))
(signal 'ement-api-error (list error-message))))
2020-11-28 05:33:16 -06:00
;;;; Footer
(provide 'ement-api)
;;; ement-api.el ends here