all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Mathias Dahl <brakjoller@gmail.com>
Subject: How to use http-get properly in code
Date: Sun, 05 Feb 2006 19:41:53 +0100	[thread overview]
Message-ID: <m24q3d3axa.fsf@gmail.com> (raw)


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?

             reply	other threads:[~2006-02-05 18:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-05 18:41 Mathias Dahl [this message]
2006-02-06 22:59 ` How to use http-get properly in code Thien-Thi Nguyen
2006-02-06 23:23   ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m24q3d3axa.fsf@gmail.com \
    --to=brakjoller@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.