This commit is contained in:
Akira Tempaku 2025-04-19 18:28:25 +09:00
parent 7127a6fc68
commit 5448d575ad
Signed by: paku
GPG key ID: 5B4E8402BCC50607

View file

@ -28,14 +28,12 @@
(defun %request (method endpoint &key path query content) (defun %request (method endpoint &key path query content)
(or *service-domain* (error "microcms:*service-domain* is not configured.")) (or *service-domain* (error "microcms:*service-domain* is not configured."))
(or *api-key* (error "microcms:*api-key* is not configured.")) (or *api-key* (error "microcms:*api-key* is not configured."))
(let* ((url (%build-uri endpoint :path path :query query))
(req-headers `(("X-MICROCMS-API-KEY" . ,*api-key*)
("Content-Type" . "application/json"))))
(handler-case (handler-case
(multiple-value-bind (res-body status res-headers) (multiple-value-bind (res-body status res-headers)
(request url (request (%build-uri endpoint :path path :query query)
:method method :method method
:headers req-headers :headers `(("X-MICROCMS-API-KEY" . ,*api-key*)
("Content-Type" . "application/json"))
:content (and content (to-json content)) :content (and content (to-json content))
:force-binary nil) :force-binary nil)
(format t "microCMS status: ~D~%" status) (format t "microCMS status: ~D~%" status)
@ -43,15 +41,13 @@
(search "application/json" (gethash "content-type" res-headers))) (search "application/json" (gethash "content-type" res-headers)))
(parse res-body))) (parse res-body)))
(http-request-failed (e) (http-request-failed (e)
(format *error-output* "microCMS status: ~D~%" (response-status e)))))) (format *error-output* "microCMS status: ~D~%" (response-status e)))))
(defun %build-uri (endpoint &key path query) (defun %build-uri (endpoint &key path query)
(let ((uri (make-uri (render-uri (make-uri :scheme "https"
:scheme "https"
:host (format nil "~A.microcms.io" *service-domain*) :host (format nil "~A.microcms.io" *service-domain*)
:path (format nil "/api/v1/~A~@[/~A~]" endpoint path) :path (format nil "/api/v1/~A~@[/~A~]" endpoint path)
:query (%build-query query)))) :query (%build-query query))))
(render-uri uri)))
(defun %build-query (query) (defun %build-query (query)
(loop :for (key val) :on query :by #'cddr (loop :for (key val) :on query :by #'cddr