Function: gptel-curl--parse-response

gptel-curl--parse-response is a natively compiled function defined in gptel-request.el.

Signature

(gptel-curl--parse-response PROC-INFO)

Documentation

Parse the buffer BUF with curl's response.

PROC-INFO is a plist with contextual information.

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel-request.el
(defun gptel-curl--parse-response (proc-info)
  "Parse the buffer BUF with curl's response.

PROC-INFO is a plist with contextual information."
  (let ((uuid (plist-get proc-info :uuid)))
    (goto-char (point-max))
    (if (not (search-backward uuid nil t))
        (list nil nil nil "Could not parse curl response.")
      (backward-char)
      (pcase-let* ((`(,_ . ,header-size) (read (current-buffer))))
        (goto-char (point-min))
        (if-let* ((http-msg (string-trim
                             (buffer-substring (line-beginning-position)
                                               (line-end-position))))
                  (http-status
                   (save-match-data
                     (and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
                          (match-string 1 http-msg))))
                  (response (progn (goto-char header-size)
                                   (condition-case nil
                                       (gptel--json-read)
                                     (error 'json-read-error)))))
            (cond
             ;; FIXME Handle the case where HTTP 100 is followed by HTTP (not 200) BUG #194
             ((member http-status '("200" "100"))
              (list (and-let* ((resp (gptel--parse-response
                                      (plist-get proc-info :backend) response proc-info))
                               ((not (string-blank-p resp))))
                      (string-trim resp))
                    http-status http-msg))
             ((and-let* ((error-data (gptel--parse-response-error response)))
                (list nil http-status http-msg error-data)))
             ((eq response 'json-read-error)
              (list nil http-status (concat "(" http-msg ") Malformed JSON in response.")
                    "Malformed JSON in response"))
             (t (list nil http-status (concat "(" http-msg ") Could not parse HTTP response.")
                      "Could not parse HTTP response.")))
          (list nil http-status (concat "(" http-msg ") Could not parse HTTP response.")
                "Could not parse HTTP response."))))))