diff options
| author | 2025-10-13 05:36:56 +0300 | |
|---|---|---|
| committer | 2025-10-13 05:36:56 +0300 | |
| commit | b4ab1bc4d1f4979a8a7cd79bceb942769188c07a (patch) | |
| tree | e434020caf09df6c91c5bec84f097bdbf91c997b | |
| parent | Move serializing stuff from bot/impl to a new file (diff) | |
| download | ukkoclot-b4ab1bc4d1f4979a8a7cd79bceb942769188c07a.tar.gz ukkoclot-b4ab1bc4d1f4979a8a7cd79bceb942769188c07a.tar.xz ukkoclot-b4ab1bc4d1f4979a8a7cd79bceb942769188c07a.zip | |
Move do-call &co to a transport package
| -rw-r--r-- | src/bot/impl.lisp | 39 | ||||
| -rw-r--r-- | src/bot/method-macros.lisp | 4 | ||||
| -rw-r--r-- | src/transport.lisp | 44 |
3 files changed, 48 insertions, 39 deletions
diff --git a/src/bot/impl.lisp b/src/bot/impl.lisp index 93e63f5..ea1aa03 100644 --- a/src/bot/impl.lisp +++ b/src/bot/impl.lisp | |||
| @@ -2,16 +2,8 @@ | |||
| 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/bot/impl | 3 | (defpackage :ukkoclot/bot/impl |
| 4 | (:use :c2cl :iterate :ukkoclot/config) | 4 | (:use :c2cl :iterate :ukkoclot/config) |
| 5 | (:import-from :anaphora :aand :acond :it) | ||
| 6 | (:import-from :cl+ssl) | ||
| 7 | (:import-from :dex) | ||
| 8 | (:import-from :log) | ||
| 9 | (:import-from :ukkoclot/serializing :fixup-args :parse-value) | ||
| 10 | (:import-from :ukkoclot/strings :lisp->snake-case) | ||
| 11 | (:local-nicknames | ||
| 12 | (:jzon :com.inuoe.jzon)) | ||
| 13 | (:export | 5 | (:export |
| 14 | :bot :bot-p :make-bot :do-call | 6 | :bot :bot-p :make-bot |
| 15 | 7 | ||
| 16 | :bot-config :bot-db :bot-base-uri :bot-power-on :bot-username% :bot-id%)) | 8 | :bot-config :bot-db :bot-base-uri :bot-power-on :bot-username% :bot-id%)) |
| 17 | (in-package :ukkoclot/bot/impl) | 9 | (in-package :ukkoclot/bot/impl) |
| @@ -29,32 +21,3 @@ | |||
| 29 | "https://api.telegram.org/bot" | 21 | "https://api.telegram.org/bot" |
| 30 | (config-bot-token config) "/"))) | 22 | (config-bot-token config) "/"))) |
| 31 | (make-bot% :config config :db db :base-uri base-uri))) | 23 | (make-bot% :config config :db db :base-uri base-uri))) |
| 32 | |||
| 33 | (defun req (uri method content) | ||
| 34 | (let ((retrier (dex:retry-request 5 :interval 1))) | ||
| 35 | (handler-case (dex:request uri :method method :content content) | ||
| 36 | (dex:http-request-too-many-requests (e) (dex:ignore-and-continue e)) ; We deal with too many reqs manually | ||
| 37 | (dex:http-request-failed (e) (funcall retrier e)) | ||
| 38 | (cl+ssl::ssl-error (e) (funcall retrier e))))) | ||
| 39 | |||
| 40 | (defun do-call% (bot method uri type args-encoded) | ||
| 41 | (let ((body (req uri method args-encoded))) | ||
| 42 | (let ((hash (jzon:parse body))) | ||
| 43 | (acond | ||
| 44 | ((gethash "ok" hash) (parse-value type (gethash "result" hash))) | ||
| 45 | ((aand (gethash "parameters" hash) | ||
| 46 | (gethash "retry_after" it)) | ||
| 47 | (log:info "Should sleep for ~A seconds" it) | ||
| 48 | (sleep it) | ||
| 49 | (log:info "Good morning!") | ||
| 50 | (do-call% bot method uri type args-encoded)) | ||
| 51 | (t (error "TG error ~A: ~A ~:A" | ||
| 52 | (gethash "error_code" hash) | ||
| 53 | (gethash "description" hash) | ||
| 54 | (gethash "parameters" hash))))))) | ||
| 55 | |||
| 56 | (defun do-call (bot method path type args) | ||
| 57 | (let ((uri (concatenate 'string (bot-base-uri bot) path)) | ||
| 58 | (args-encoded (fixup-args args))) | ||
| 59 | (log:debug "~A .../~A ~S" method path args-encoded) | ||
| 60 | (do-call% bot method uri type args-encoded))) | ||
diff --git a/src/bot/method-macros.lisp b/src/bot/method-macros.lisp index 7b54dc9..b6f237f 100644 --- a/src/bot/method-macros.lisp +++ b/src/bot/method-macros.lisp | |||
| @@ -1,7 +1,9 @@ | |||
| 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/bot/method-macros | 3 | (defpackage :ukkoclot/bot/method-macros |
| 4 | (:use :c2cl :iterate :ukkoclot/bot/impl) | 4 | (:use :c2cl :iterate) |
| 5 | (:import-from :ukkoclot/bot/impl :bot) | ||
| 6 | (:import-from :ukkoclot/transport :do-call) | ||
| 5 | (:export :define-tg-method)) | 7 | (:export :define-tg-method)) |
| 6 | (in-package :ukkoclot/bot/method-macros) | 8 | (in-package :ukkoclot/bot/method-macros) |
| 7 | 9 | ||
diff --git a/src/transport.lisp b/src/transport.lisp new file mode 100644 index 0000000..0b5b138 --- /dev/null +++ b/src/transport.lisp | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | ;; SPDX-License-Identifier: EUPL-1.2 | ||
| 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> | ||
| 3 | (defpackage :ukkoclot/transport | ||
| 4 | (:use :c2cl) | ||
| 5 | (:import-from :anaphora :aand :acond :it) | ||
| 6 | (:import-from :cl+ssl) | ||
| 7 | (:import-from :dex) | ||
| 8 | (:import-from :log) | ||
| 9 | (:import-from :ukkoclot/bot/impl :bot-base-uri) | ||
| 10 | (:import-from :ukkoclot/serializing :fixup-args :parse-value) | ||
| 11 | (:local-nicknames | ||
| 12 | (:jzon :com.inuoe.jzon)) | ||
| 13 | (:export :do-call)) | ||
| 14 | (in-package :ukkoclot/transport) | ||
| 15 | |||
| 16 | (defun req (uri method content) | ||
| 17 | (let ((retrier (dex:retry-request 5 :interval 1))) | ||
| 18 | (handler-case (dex:request uri :method method :content content) | ||
| 19 | ;; We deal with too many requests manually | ||
| 20 | (dex:http-request-too-many-requests (e) (dex:ignore-and-continue e)) | ||
| 21 | (dex:http-request-failed (e) (funcall retrier e)) | ||
| 22 | (cl+ssl::ssl-error (e) (funcall retrier e))))) | ||
| 23 | |||
| 24 | (defun do-call% (method uri out-type args-encoded) | ||
| 25 | (let ((body (req uri method args-encoded))) | ||
| 26 | (let ((hash (jzon:parse body))) | ||
| 27 | (acond | ||
| 28 | ((gethash "ok" hash) (parse-value out-type (gethash "result" hash))) | ||
| 29 | ((aand (gethash "parameters" hash) | ||
| 30 | (gethash "retry_after" it)) | ||
| 31 | (log:info "Should sleep for ~A seconds" it) | ||
| 32 | (sleep it) | ||
| 33 | (log:info "Good morning!") | ||
| 34 | (do-call% method uri out-type args-encoded)) | ||
| 35 | (t (error "TG error ~A: ~A ~:A" | ||
| 36 | (gethash "error_code" hash) | ||
| 37 | (gethash "description" hash) | ||
| 38 | (gethash "parameters" hash))))))) | ||
| 39 | |||
| 40 | (defun do-call (bot method path out-type args) | ||
| 41 | (let ((uri (concatenate 'string (bot-base-uri bot) path)) | ||
| 42 | (args-encoded (fixup-args args))) | ||
| 43 | (log:debug "~A .../~A ~S" method path args-encoded) | ||
| 44 | (do-call% method uri out-type args-encoded))) | ||