;;(load "~/glibc/dark_c/emacs-symfony-cli/http-content.el")

;;;(global-set-key (kbd "M-m") #'(lambda () (interactive) (elisp-http-content)))

(setq http-token "")

(defun elisp-http-get-header ()
  (if-let* ((auth-header (unless (or (null http-token)
                                     (string= http-token ""))
                           (list (cons "Authorization" (concat "Bearer " http-token)))))
				(headers (append
					  '(("Host" . "localhost:8080")
						("Accept" . "*/*")
						("Content-Type" . "application/json"))
					  auth-header)))
	  headers
	))

(setq my-headers
      (or (elisp-http-get-header)
          '(("Host" . "localhost:8080")
            ("Accept" . "*/*")
            ("Content-Type" . "application/json"))))

(defun elisp-http-register-line ()
  (let* ((_path (elisp-http-prompt-api))
		 (path (format "POST /%s HTTP/1.1" _path)))
	path))

(defun elisp-http-get-line ()
  (let* ((_path (elisp-http-prompt-api))
		 (path (format "GET /%s HTTP/1.1" _path)))
	path))

;;;(elisp-http-register-line)

(defun elisp-http-make-content (method)
  (let ((request-line (elisp-http-set-request-line)))
    (concat
     request-line "\n"
     (mapconcat (lambda (x)
                  (format "%s: %s" (car x) (cdr x)))
                my-headers
                "\n"))))

;;;(elisp-http-make-content)

(defun elisp-http-register-content ()
  "Generate HTTP request content for registration with JSON payload."
  (let* ((email (elisp-http-prompt-email))
		 (password (elisp-http-prompt-password))
		 (request-line (elisp-http-register-line))
         (json-body (json-encode `(("email" . ,email) ("password" . ,password))))
         (headers (append my-headers
                          (list (cons "Content-Length"
                                      (number-to-string (string-bytes json-body))))))
         (header-block
          (mapconcat (lambda (x) (format "%s: %s" (car x) (cdr x)))
                     headers
                     "\n")))
    (concat request-line "\n"
            header-block "\n\n"
            json-body)))

;; Пример использования:
;;;(elisp-http-register-content)
