Skip to content

Commit

Permalink
Figure out if we need a multi-part request in the request function
Browse files Browse the repository at this point in the history
  • Loading branch information
rtmpdavid committed Feb 24, 2024
1 parent 2c462bf commit 416c89e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
1 change: 0 additions & 1 deletion src/message.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ https://core.telegram.org/bots/api#sendphoto"
(apply #'make-request bot "sendPhoto"
:|chat_id| (get-chat-id chat)
:|photo| photo
:pathname-p t
options))

(defmethod send-audio (bot chat (audio string)
Expand Down
37 changes: 19 additions & 18 deletions src/network.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,34 @@
(:report (lambda (condition stream)
(format stream "Request error: ~A" (what condition)))))

(defun process-options (&rest options &key (pathname-p nil) &allow-other-keys)
(loop for (key value)
on (alexandria:remove-from-plist options :timeout :streamp :pathname-p)
by #'cddr
when value
if pathname-p
collect (cons (kebab:to-snake-case key) value)
else
collect (kebab:to-snake-case key)
and
collect value))


(defun make-request (bot name &rest options &key (streamp nil) (timeout 3) (pathname-p nil) &allow-other-keys)
(defun process-options (options return-alist-p)
"Process request options, returning alist if required"
(let* ((options (alexandria:remove-from-plist options :timeout :streamp))
(sanitized-options (loop for (key value) on options by #'cddr
when value
collect (kebab:to-snake-case key)
and collect value)))
(if return-alist-p
(alexandria:plist-alist sanitized-options)
sanitized-options)))

(defun make-request (bot name &rest options &key (streamp nil) (timeout 3) &allow-other-keys)
"Perform HTTP request to 'name API method with 'options JSON-encoded object or multi-part form-data."
(declare (ignore streamp))

(let ((url (concatenate 'string (get-endpoint bot) name)))
(log:debug "Posting data to"
(obfuscate url)
options)
(let* ((max-timeout (* timeout 10))
(processed-options (apply #'process-options options))
(let* ((multipart-required (find-if #'pathnamep options))
(max-timeout (* timeout 10))
(processed-options (process-options options multipart-required))
(response
(dexador:post url
:headers (unless pathname-p '(("Content-Type" . "application/json")))
:content (if pathname-p processed-options (jonathan:to-json processed-options))
:headers (unless multipart-required '(("Content-Type" . "application/json")))
:content (if multipart-required
processed-options
(jonathan:to-json processed-options))
:read-timeout max-timeout
:connect-timeout max-timeout
:proxy (if *proxy* *proxy* dex:*default-proxy*)))
Expand Down

0 comments on commit 416c89e

Please sign in to comment.