Using Network Calls in Separate Files

Hi

I have an app which will have a number of interface screens.  I plan for this to be seperate lua files.  Each of these interface screens will need to call a generic network http url with various requests and receive a response.

I really want to keep all of the network interface stuff away from the interface files.  So, I would like a structure where I have a number of files all referencing a general network file which I have called httpwork.

I have coded the first interface file and this generic network file.

However, I am stuck as to how to get the lua file that is calling the httpwork function to receive the request response.

For example: -

my first file is called home.lua.  It has some code like this: -

local http = require("httpwork") http.MakeNetworkCall(Param1)  

This calls a function in my httpwork file called MakeNetworkCall which is like this: -

local network = require("network") local url = require("socket.url") local resp = {} local function networkListener( event )         if ( event.isError ) then                 print( "Network error!")         else                 print ( "RESPONSE: " .. event.response )         end end local function MakeNetworkCall(param1)      network.request( "somewebsite.html?param="..param1, "GET", networkListener ) end resp.MakeNetworkCall = MakeNetworkCall return resp  

So far so good.

However, I cannot work out how to get the response back from this file to my original calling file.  I know that the response is picked up by the listener, but the listener is in this file!

I tried referencing the return of the function, but of course the network.request call if asynchronous so returns immediately with nothing!  Then the listener kicks in, but that is a little late for the return.

I am probably getting too bogged down in code in different files, but I really want to try and separate my code layers if I can and keeping all this network interface stuff hidden from the front end screen code.

Has anyone got any tips on how I can best achieve this?  Happy to start again if I have to! :slight_smile:

Why are you requiring “network” at the top of the module?
 

Rob

Cannot you pass callback function as second argument to MakeNetworkCall?

I thought I needed to require network to access the network functions?

Call back function sounds good. I think I have done this before, but could you remind me of the syntax?

I just wanted to make sure you didn’t have a “network.lua” in your project and you were trying to include that.  It would write over our network routines.

Rob

Ok Rob, any chance of some tips on how to code this? What I think I would like to do is, based on what piotrz55 said, if I can pass a call back function to MakeNetworkCall function. This somehow passes the reference to the call back function to the networkListener. When the listener gets the event it calls the call back function. The call back function is declared in the file making the original call, not my httpwork.lua file. Is this possible? Would I need to make the call back function a global or could I use a local? Any help with syntax would be great… :slight_smile:

maybe something like (untested)

local network = require("network") local url = require("socket.url") local resp = {} resp.callback = nil local function networkListener( event )         if ( event.isError ) then                 print( "Network error!")         else                 print ( "RESPONSE: " .. event.response ) if resp.callback and type(resp.callback) == "function" then resp.callback(event) end         end end local function MakeNetworkCall(param1, callback)     resp.callback = callback     network.request( "somewebsite.html?param="..param1, "GET", networkListener ) end resp.MakeNetworkCall = MakeNetworkCall return resp

Cheers Rob, will give it a go!

Rob, you are a jedi master - this code works a treat.  I am back on track!!!

Why are you requiring “network” at the top of the module?
 

Rob

Cannot you pass callback function as second argument to MakeNetworkCall?

I thought I needed to require network to access the network functions?

Call back function sounds good. I think I have done this before, but could you remind me of the syntax?

I just wanted to make sure you didn’t have a “network.lua” in your project and you were trying to include that.  It would write over our network routines.

Rob

Ok Rob, any chance of some tips on how to code this? What I think I would like to do is, based on what piotrz55 said, if I can pass a call back function to MakeNetworkCall function. This somehow passes the reference to the call back function to the networkListener. When the listener gets the event it calls the call back function. The call back function is declared in the file making the original call, not my httpwork.lua file. Is this possible? Would I need to make the call back function a global or could I use a local? Any help with syntax would be great… :slight_smile:

maybe something like (untested)

local network = require("network") local url = require("socket.url") local resp = {} resp.callback = nil local function networkListener( event )         if ( event.isError ) then                 print( "Network error!")         else                 print ( "RESPONSE: " .. event.response ) if resp.callback and type(resp.callback) == "function" then resp.callback(event) end         end end local function MakeNetworkCall(param1, callback)     resp.callback = callback     network.request( "somewebsite.html?param="..param1, "GET", networkListener ) end resp.MakeNetworkCall = MakeNetworkCall return resp

Cheers Rob, will give it a go!

Rob, you are a jedi master - this code works a treat.  I am back on track!!!