Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

HTTP Sync - Synchronous HTTP Requests - http.requestSync()

in Code Sharing Posts: 185

Hi all,

Working with WebRepo I became fed up with managing multiple nested callbacks for http.request() calls so I now present ‘HTTP Sync’!

This library provides an implementation of http.requestSync() entirely avoiding the need for user defined callbacks.

Short example usage here:

-- Make our http request
local ok, data, status, headers = http.requestSync("https://baconipsum.com/api/?type=meat-and-filler&paras=1&format=text")

-- Check that the http request succeeded
if ok then
    print(data)
else
    print("Something went wrong!\n" .. data)
end

Please be aware that this may not play nicely with the supportedOrientations library at the moment.

Hopefully this will be useful to someone.

Cheers,
Steppers

Note: This is also available to download on WebRepo.

Tagged:
HttpSync.zip 26.2K

Comments

  • edited June 19 Posts: 1,061

    well, that's interesting. i can't say that i understand it after ten minutes of trying. why is all that mapping and faking needed? i'm sure missing something.

  • Posts: 185
    @RonJeffries I'll admit the Global Overrides stuff is pretty confusing but in essence it allows the HTTP Sync library to intercept the Codea runtime's setup() and draw() calls to make them run inside a coroutine that we can then suspend while waiting for the http request to complete.
  • Posts: 1,061

    thanks ... i'll have to think about, esp why we'd need to suspend draw. setup, i kind of get. intuitively, i'm have thought it could be simpler.

  • Posts: 185

    Without a doubt, in most cases it makes more sense to use the usual callbacks to avoid blocking execution but in some web API cases it makes life much easier to just wait for the response (Github OAuth Authentication being one).

    As for it being simpler, I don’t think so unfortunately. As the callbacks are called directly by the Codea runtime outside of calls to setup() and draw() it needs to be able to return execution to the runtime without continuing past the http.requestSync() call. coroutines are the only way I know of that makes this possible..

  • edited June 19 Posts: 1,061

    yes i agree coroutines are the way. not sure it takes quite that much mechanism, but i sure haven't done it, so what do i know? it's cool, for sure.

Sign In or Register to comment.