tkf / emacs-request

Request.el -- Easy HTTP request for Emacs Lisp
http://tkf.github.com/emacs-request/
GNU General Public License v3.0
629 stars 93 forks source link

Strange behaviour of request return value #109

Closed azzamsa closed 5 years ago

azzamsa commented 5 years ago

Spend hour debugging my code, turn out that emacs-request had 'strange' behavior. Or maybe I am missing something.

1) request-response-data works using setq

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (setq foo (request "https://time.siswadi.com/pray/Malang" :parser 'json-read))
#s(request-response nil nil nil nil nil "https://time.siswadi.com/pray/Malang" nil
(:parser json-read :error
#[128 "\302\300\303\301\"\"\207"
[request-default-error-callback
("https://time.siswadi.com/pray/Malang")
apply append]
6 "\n\n(fn &rest ARGS2)"]
:url "https://time.siswadi.com/pray/Malang" :response #0)
#<buffer  *request curl*> nil nil curl nil)

ELISP> (request-response-data foo)
((data
  (Fajr . "04:00")
  (Sunrise . "05:22")
  (Dhuhr . "11:38")
  (Asr . "15:03")
  (Sunset . "17:55")
  (Maghrib . "17:55")
  (Isha . "19:08")
  (SepertigaMalam . "21:44")
  (TengahMalam . "23:38")
  (DuapertigaMalam . "01:33")
  (method .
          ["Egypt" "Egyptian General Authority of Survey" "Fajr: 19.5° - Isha'a: 17.5°"]))
 (time
  (date . "2019-01-14")
  (time . "17:56:34")
  (timezone . "Asia/Jakarta")
  (offset . 7))
 (location
  (latitude . "-7.9666204000")
  (longitude . "112.6326321000")
  (address . "Malang, Malang City, East Java, Indonesia"))
 (debug
  (sunrise . "05:20")
  (sunset . "17:57"))
 (status . "OK"))

2) request-response-data didn't work if I use it directly

ELISP> (request-response-data (request "https://time.siswadi.com/pray/Malang" :parser 'json-read))
nil

3) request-response-data didn't work using let

ELISP> (let ((ohmy (request "https://time.siswadi.com/pray/Malang" :parser 'json-read)))
     (request-response-data ohmy))
nil

Using example in readme also didn't work

ELISP> (let*
    ((thisrequest
      (request "https://time.siswadi.com/pray/Malang"
               :parser 'json-read
               :success (cl-function
                         (lambda (&key data &allow-other-keys)
                           (message "I sent: %S" (nth 0 data))))))
     (data (request-response-data thisrequest))
     (err (request-response-error-thrown thisrequest))
     (status (request-response-status-code thisrequest)))
  (format "Data %s Status %s" data status)
  )
"Data nil Status nil"

But :success worked

I sent: (data (Fajr . "04:00") (Sunrise . "05:22") (Dhuhr . "11:38") (Asr . "15:03") (Sunset . "17:55") (Maghrib . "17:55") (Isha . "19:08") (SepertigaMalam . "21:44") (TengahMalam . "23:38") (DuapertigaMalam . "01:33") (method . ["Egypt" "Egyptian General Authority of Survey" "Fajr: 19.5° - Isha'a: 17.5°"]))

Thanks

azzamsa commented 5 years ago

After reading patiently through issue to see more usage example. I tried :sync t

The docs said:

Synchronous request is functional, but please don’t use it other than testing or debugging. Emacs users have better things to do rather than waiting for HTTP request. If you want a better way to write callback chains, use request-deferred.

Waiting synchronous is boring, But for now I have no other option. Using Emacs-deferred just add more complexity, IMHO.

Works using :sync t

ELISP> (let* ((thisola (request "https://time.siswadi.com/pray/Malang"
                                :parser 'json-read
                                :sync t)))
     (request-response-data thisola))
((data
  (Fajr . "04:00")
  (Sunrise . "05:22")
  (Dhuhr . "11:38")
  (Asr . "15:03")
  (Sunset . "17:55")
  (Maghrib . "17:55")
  (Isha . "19:08")
  (SepertigaMalam . "21:44")
  (TengahMalam . "23:38")
  (DuapertigaMalam . "01:33")
  (method .
          ["Egypt" "Egyptian General Authority of Survey" "Fajr: 19.5° - Isha'a: 17.5°"]))
 (time
  (date . "2019-01-14")
  (time . "21:35:43")
  (timezone . "Asia/Jakarta")
  (offset . 7))
 (location
  (latitude . "-7.9666204000")
  (longitude . "112.6326321000")
  (address . "Malang, Malang City, East Java, Indonesia"))
 (debug
  (sunrise . "05:20")
  (sunset . "17:57"))
 (status . "OK"))

Failed without :sync t

ELISP> (let* ((thisola (request "https://time.siswadi.com/pray/Malang"
                                :parser 'json-read)))
     (request-response-data thisola))
nil

I agree that we need more usage examples https://github.com/tkf/emacs-request/issues/71. I don't find sync is mentioned in readme and in tkf example in his SO answers about emacs-request.

Related : https://github.com/tkf/emacs-request/issues/92

titaniumbones commented 5 years ago

Hi @azzamsa I'll try to take another look at this but I have a little bit of a hard time reading your code, I think because of the way ielm represents data.

I'm also just filling in temporarily for the maintainer and am very slow debugging code. If you can do any more work tracking down the source of the problem that would be fantastic. perhaps start by pasting both your working code and your failing code to a pastebin so I can easily run it?

Also, there are several improvements in the development branch: https://github.com/tkf/emacs-request/tree/development . Can you see if your problem still exists there?

Thank you!

azzamsa commented 5 years ago

@titaniumbones We replied to main issue at the same seconds :)