New HTTP Request/Response Help

I’m just messing with the new HTTP::Request/Response Classes for the first time, and what I’m finding is that the callback block for the start method will not run asynchronously, making the following example impossible because the loop will run forever, preventing the variable ‘status’ from being set:

status = nil
request = Sketchup::Http::Request.new(url)
request.start{|req,res|
	status = res.status_code
}
loop{break if !status.nil?}
if status==200
	return true
else
	return false
end

maybe I’m just going about this the wrong way?

Yea, you cannot run a loop like this in event driven embedded Ruby.

(1) The docs for this class’ constructor, clearly state:

Keeping a reference to the request is necessary in order to ensure the use of the response.

Keeping a reference” means using the correct type of reference (also loosely called a variable,) that will not go out of scope and get garbage collected by Ruby’s housekeeping.

Local variables are temporary references and go out of scope quickly, such as when the method wrapping around them, finishes executing. When that happens, (the method ends) the Sketchup::Http::Request instance object is no longer referenced, and will get GC’d.

If you have the code above in a script, at the top level, Ruby destroys local references when the script is finished loading. This safeguard is built-into the load() and require() methods, to prevent local variables from propagating into every object, (like everyone else’s classes and modules,) because the TOPLEVEL_BINDING, is Object.

So you need to use a persistent reference, such as a instance variable (ie, @request,) or a class/module variable (ie, @@request.)


(2) You do not need to implement a looping construct, in order to watch the object for a responce.

It is already built-into the class. The start method registers a block of callback code, to be executed when a HTTP Response has been received.

There needs to be a way better example, rather than having to stitch together all the method examples.

Logged an issue to add tutorial and example:

… and referenced the similar standard library examples at:

(1) @tt_su, 3 years ago, when the Ruby 2.0 Standard Library was distributed with SketchUp 2014, you had posted a warning that downloading large files with Net::HTTP blocked SketchUp (ie, could cause it to go into “Not Responding” mode.)

The docs are not clear, but I assume that this special API Sketchup::Http classes solve this main blocking issue ?

Ie, both classes overview sections are terse, and the Sketchup::Http module’s overview is the only place that “asynchronous” is mentioned, and that this module is “an alternative” to using Net::HTTP. But we will find often readers will not visit the page for a “namespace” module, and go right to the nested class documentation.


(2) At that same time Bugra had mentioned that connecting to https sites (with Net::HTTP) crashed SketchUp due to issues with OpenSSL.

Again, documentation doesn’t mention whether these new API classes solve this OpenSSL issue.

1 Like