diff options
Diffstat (limited to 'src/transport.lisp')
| -rw-r--r-- | src/transport.lisp | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/transport.lisp b/src/transport.lisp index 941341a..d47db4d 100644 --- a/src/transport.lisp +++ b/src/transport.lisp | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | ;; SPDX-License-Identifier: EUPL-1.2 | 1 | ;; SPDX-License-Identifier: EUPL-1.2 |
| 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> | 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> |
| 3 | (defpackage :ukkoclot/transport | 3 | (defpackage :ukkoclot/transport |
| 4 | (:documentation "Package dealing with HTTP calls.") | ||
| 4 | (:use :c2cl) | 5 | (:use :c2cl) |
| 5 | (:import-from :anaphora :aand :acond :it) | ||
| 6 | (:import-from :cl+ssl) | 6 | (:import-from :cl+ssl) |
| 7 | (:import-from :dex) | 7 | (:import-from :dex) |
| 8 | (:import-from :log) | 8 | (:import-from :log) |
| @@ -14,6 +14,7 @@ | |||
| 14 | (in-package :ukkoclot/transport) | 14 | (in-package :ukkoclot/transport) |
| 15 | 15 | ||
| 16 | (defun req (uri method content) | 16 | (defun req (uri method content) |
| 17 | "Wrapper function for making a request." | ||
| 17 | (let ((retrier (dex:retry-request 5 :interval 1)) | 18 | (let ((retrier (dex:retry-request 5 :interval 1)) |
| 18 | ;; This is needed to avoid hangs, | 19 | ;; This is needed to avoid hangs, |
| 19 | ;; see https://github.com/fukamachi/dexador/issues/91#issuecomment-1093472364 | 20 | ;; see https://github.com/fukamachi/dexador/issues/91#issuecomment-1093472364 |
| @@ -25,22 +26,29 @@ | |||
| 25 | (cl+ssl::ssl-error (e) (funcall retrier e))))) | 26 | (cl+ssl::ssl-error (e) (funcall retrier e))))) |
| 26 | 27 | ||
| 27 | (defun do-call% (method uri out-type args-encoded) | 28 | (defun do-call% (method uri out-type args-encoded) |
| 28 | (let ((body (req uri method args-encoded))) | 29 | "Internal function with the arguments already encoded. |
| 29 | (let ((hash (jzon:parse body))) | 30 | |
| 30 | (acond | 31 | See `do-call'." |
| 31 | ((gethash "ok" hash) (parse-value out-type (gethash "result" hash))) | 32 | (let* ((body (req uri method args-encoded)) |
| 32 | ((aand (gethash "parameters" hash) | 33 | (hash (jzon:parse body))) |
| 33 | (gethash "retry_after" it)) | 34 | (if (gethash "ok" hash) |
| 34 | (log:info "Should sleep for ~A seconds" it) | 35 | (parse-value out-type (gethash "result" hash)) |
| 35 | (sleep it) | 36 | (let* ((error-code (gethash "error_code" hash)) |
| 36 | (log:info "Good morning!") | 37 | (description (gethash "description" hash)) |
| 37 | (do-call% method uri out-type args-encoded)) | 38 | (parameters (gethash "parameters" hash)) |
| 38 | (t (error "TG error ~A: ~A ~:A" | 39 | (retry-after (when parameters (gethash "retry_after" parameters)))) |
| 39 | (gethash "error_code" hash) | 40 | (cond |
| 40 | (gethash "description" hash) | 41 | (retry-after |
| 41 | (gethash "parameters" hash))))))) | 42 | (log:info "Should sleep for ~A seconds" retry-after) |
| 43 | (sleep retry-after) | ||
| 44 | (log:info "Good morning!") | ||
| 45 | (do-call% method uri out-type args-encoded)) | ||
| 46 | (t | ||
| 47 | (error "TG error ~A: ~A ~:A" | ||
| 48 | error-code description parameters))))))) | ||
| 42 | 49 | ||
| 43 | (defun do-call (bot method path out-type args) | 50 | (defun do-call (bot method path out-type args) |
| 51 | "Perform a HTTP call." | ||
| 44 | (let ((uri (concatenate 'string (bot-base-uri bot) path)) | 52 | (let ((uri (concatenate 'string (bot-base-uri bot) path)) |
| 45 | (args-encoded (fixup-args args))) | 53 | (args-encoded (fixup-args args))) |
| 46 | (log:debug "~A .../~A ~S" method path args-encoded) | 54 | (log:debug "~A .../~A ~S" method path args-encoded) |