all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to use http-get properly in code
@ 2006-02-05 18:41 Mathias Dahl
  2006-02-06 22:59 ` Thien-Thi Nguyen
  0 siblings, 1 reply; 3+ messages in thread
From: Mathias Dahl @ 2006-02-05 18:41 UTC (permalink / 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?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to use http-get properly in code
  2006-02-05 18:41 How to use http-get properly in code Mathias Dahl
@ 2006-02-06 22:59 ` Thien-Thi Nguyen
  2006-02-06 23:23   ` Thien-Thi Nguyen
  0 siblings, 1 reply; 3+ messages in thread
From: Thien-Thi Nguyen @ 2006-02-06 22:59 UTC (permalink / raw)


Mathias Dahl <brakjoller@gmail.com> writes:

> ;; 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)))))

you are confusing a sentinel and a process output filter.
the former is called on changes to process state (e.g., hangup).
this function (above) is an example of the latter.

> 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.

that approach makes the code behave synchronously, which is what
you want.  (it's ok to want "ugly" yet useful things, sometimes.)
see `gnugo-synchronous-send/return' in gnugo.el, somewhere under:

  http://www.glug.org/people/ttn/software/personal-elisp/

for an example.  there are lots of synchronous examples, but fewer
asynchronous ones, because doing asynchronous right is more hairy.
see the page beginning with "GNU Go vs GNU Go" in gnugo-extra.el
(also under the aforementioned directory) for an example of
classic(ish) asynchronous design.

thi

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to use http-get properly in code
  2006-02-06 22:59 ` Thien-Thi Nguyen
@ 2006-02-06 23:23   ` Thien-Thi Nguyen
  0 siblings, 0 replies; 3+ messages in thread
From: Thien-Thi Nguyen @ 2006-02-06 23:23 UTC (permalink / raw)


Thien-Thi Nguyen <ttn@glug.org> writes:

> Mathias Dahl <brakjoller@gmail.com> writes:
>
>> ;; 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)))))
>
> you are confusing a sentinel and a process output filter.
> the former is called on changes to process state (e.g., hangup).
> this function (above) is an example of the latter.

oops, i was mistaken.  if you are indeed confusing these concepts, that
is not apparent (or at least it is not as apparant as my own confusion
about how to recognize the confusion of others, if there be any).  sorry
about that!  (all the other gnugo plugs are still valid, however. ;-)

a better answer to your overall question would be to suggest checking
MESSAGE to fine-tune the actions of `ef-http-get-sentinel'.  you may
find that `ef-http-get-sentinel' is being called many times.  for those
times when the process is not yet finished (implying that its output is
not yet well-formed), just do nothing.

thi

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-02-06 23:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-05 18:41 How to use http-get properly in code Mathias Dahl
2006-02-06 22:59 ` Thien-Thi Nguyen
2006-02-06 23:23   ` Thien-Thi Nguyen

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.