[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
How to use http-get properly in code
From: |
Mathias Dahl |
Subject: |
How to use http-get properly in code |
Date: |
Sun, 05 Feb 2006 19:41:53 +0100 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
I am currently trying to do an Emacs interface to Flickr and decided
to use http-get.el (http://emacswiki.org/cgi-bin/wiki/HttpGet) to
communicate with the servers (to make API-calls).
I have created my "sentinel" to fetch the data I get back from the
request. Here are parts of the code:
;; Somewhere to keep the result
(defvar ef-response-xml nil)
;; The sentinel
(defun ef-http-get-sentinel (proc message)
(save-excursion
(set-buffer (process-buffer proc))
(setq ef-response-xml
(xml-parse-region (point-min) (point-max)))))
;; Make a request
(defun ef-get-rest-response (arguments)
(http-get
(ef-get-rest-url
arguments)
nil 'ef-http-get-sentinel nil "*ef*"))
;; Flickr-specific code to get a "frob" from the response xml
(defun ef-get-frob-from-xml (response-xml)
(car (xml-node-children
(car (xml-get-children
(car response-xml)
'frob)))))
;; Get the frob
(defun ef-get-frob ()
(ef-get-rest-response
(list (cons "method" "flickr.auth.getFrob")
(cons "api_key" ef-api-key)))
(ef-get-frob-from-xml ef-response-xml))
The code above kind of works. The problem is the last line in the last
function above: it runs before the sentinel has been able to fetch the
results, because the request is done asynchronously. How do I get
around this? I have been thinking about adding a loop that waits for
some flag that the sentinel sets when it has fetched the data, and
then when the flag is found to be true, continue with the code, but
that feels ugly.
Is there a Correct Way (TM) to do this?
- How to use http-get properly in code,
Mathias Dahl <=