Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- * SERVER CONNECTOR LOGICS
- * If a form object is passed to the query() function then
- * query params are generated by a special query parser entity
- * that knows how to build a uri and what data to include in it and
- * other params. Also it knows whether any other special methods,
- * like websockets or multipart should be called.
- * If a form object is passed to be filled by the response
- * it is parsed by the form parser and then filled by the form filler,
- * another entity that knows how to deal with forms.
- * If no form is passed as a source to build request from,
- * the request is created by its name according to the schema
- * If no form is passed to be filled, then, if the decoded response
- * object has a corresponding activity, according to the schema
- * this activity is called with all non-auxiliary fields of the response
- * as its params. Whether there is such an activity or not,
- * a user handler function passed as a param to the query will be
- * executed, thus allowing users to make queries result in custom
- * events, in addition to the ability to block activities from
- * being generated]]
- local json = require("json")
- local ResponseParser = require("src.framework.network.ResponseParser")
- local serverConnector = {}
- local instance = false
- local MAX_ATTEMPTS_COUNT = 5
- local MAX_AWAIT_TIME = 300
- local SUPPORTED_LANGS = {
- "PHP",
- "PHP/laravel",
- "RUBY",
- "RUBY/rails",
- "JAVA",
- "JAVA/spring"
- }
- local REQUEABLE_RESPONSE_CODES = {
- "404",
- }
- local DEFAULT_LANG = "PHP/laravel"
- local function validateEnum(enum, value)
- for k, v in pairs(enum) do
- if v == value then
- return true
- end
- end
- return false
- end
- serverConnector.new = function(p)
- if instance then
- return instance
- else
- local serverConnectorObject = {}
- local URL = assert(p.url and type(p.url) == "string" and p.url, "the URL parameter must be a string!")
- local isSecure = p.isSecure and type(p.isSecure) == "boolean" and p.isSecure or false
- local serverMethodParam = p.serverPage and type(p.serverPage) == "string" and p.serverPage or "query"
- local serverLang = p.serverLang and type(p.serverLang) == "string" and validateEnum(p.serverLang, SUPPORTED_LANGS) or DEFAULT_LANG
- local API = serverLang == "PHP/laravel" and "api.php/"
- local queries = {}
- local function urlMaker(name, params)
- local protocol = isSecure and "https://" or "http://"
- local paramsString = ""
- for k, v in params do
- paramsString = "&" .. paramsString .. k .. "=" .. serialize(v)
- end
- return tostring(protocol .. URL .. "/" ..API .. serverMethodParam .. query .. paramsString)
- end
- local function networkListener(event, data)
- if event.isError then
- else
- local r = event.response
- local responseCode = parseHeaders(event.response).code
- local isResponseValid = validateResponseCode(responseCode)
- if isResponseValid then
- local decoded, pos, msg = json.decode(r)
- if not decoded then
- if DEBUG_MODE then
- native.showAlert( "The response from the url " .. event.url .. " did not match json format and could not be decoded", { "ok" } )
- else
- if data.attemptCount < MAX_ATTEMPTS_COUNT then
- data.attemptCount = data.attemptCount + 1
- serverConnectorObject:query(data) --perform another attempt to reach the server and get everything
- end
- end
- else
- local decodedResponseObject = ResponseParser.validateResponse(event, decoded)--either do things according to the server schema or call the user handler
- if decodedResponseObject.hasCorrespondingActivity then
- local activity = require(decodedResponseObject.correspondingActivity).new{
- layoutName = getLayoutName(decodedResponseObject.correspondingActivity),
- params = decodedResponseObject.params
- }
- end
- end
- elseif validateEnum(REQUEABLE_RESPONSE_CODES, responseCode) then
- if data.attemptCount < MAX_ATTEMPTS_COUNT then
- data.attemptCount = data.attemptCount + 1
- serverConnectorObject:query(data)
- end
- end
- end
- end
- function serverConnector:isReachable(url, listener)
- if network.canDetectNetworkStatusChanges then
- network.setStatusListener( url, listener )
- return true
- else
- if DEBUG_MODE then
- native.showAlert( "Network status error!", "The url " .. url .. " could not be reached", { "ok" } )
- else
- return false
- end
- end
- end
- function serverConnectorObject:query(data)
- data.attemptCount = data.attemptCount and data.attemptCount or 1
- local queryObject
- if queryType == "default" then
- queryObject = network.request
- elseif queryType == "download" then
- queryObject = network.download
- elseif queryType == "upload" then
- queryObject = network.upload
- end
- local queryParams = {}
- queryParams.url = urlMaker(name, params)
- queryParams.method = method
- queryParams.listener = function(event) networkListener(event, data) end
- queryParams.queryParams = queryParamsMaker(name)
- queryParams.filename = filename
- queryParams.baseDir = baseDir
- queries[name] = queryObject( queryParams.url, queryParams.method, queryParams.listener, queryParams.queryParams, queryParams.filename, queryParams.baseDir )
- return queries[name]
- end
- function serverConnectorObject:cancelQuery(query)
- network.cancel(query)
- end
- function serverConnectorObject:cancelQueryByName(name)
- if queries[name] then
- network.cancel(queries[name])
- return true
- else
- return false
- end
- end
- return serverConnectorObject
- end
- end
- return serverConnector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement