Advertisement
sheredega

Untitled

Dec 13th, 2023
871
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1.     function asyncHttpRequest(method, url, args, resolve, reject)
  2.         assert(type(method) == 'string', '"method" expected string')
  3.         assert(type(url) == 'string', '"url" expected string')
  4.         assert(type(args) == 'table', '"args" expected table')
  5.         local thread = requestRunner()(method, url, effil.table(args))
  6.         if not resolve then resolve = function() end end
  7.         if not reject then reject = function() end end
  8.        
  9.         return {
  10.             effilRequestThread = thread;
  11.             luaHttpHandleThread = lua_thread.create(handleAsyncHttpRequestThread, thread, resolve, reject);
  12.         }
  13.     end
  14.  
  15.     function handleAsyncHttpRequestThread(runner, resolve, reject)
  16.         local status, err
  17.         repeat
  18.             status, err = runner:status()
  19.             wait(0)
  20.         until status ~= 'running'
  21.         if not err then
  22.             if status == 'completed' then
  23.                 local result, response = runner:get()
  24.                 if result then
  25.                     resolve(response)
  26.                 else
  27.                     reject(response)
  28.                 end
  29.             return
  30.             elseif status == 'canceled' then
  31.                 return reject(status)
  32.             end
  33.         else
  34.             return reject(err)
  35.         end
  36.     end
  37.  
  38.     function requestRunner()
  39.         return effil.thread(function(method, url, args)
  40.             local requests = require 'requests'
  41.             local _args = {}
  42.             local function table_assign(target, def, deep)
  43.                 for k, v in pairs(def) do
  44.                     if target[k] == nil then
  45.                         if type(v) == 'table' or type(v) == 'userdata' then
  46.                             target[k] = {}
  47.                             table_assign(target[k], v)
  48.                         else
  49.                             target[k] = v
  50.                         end
  51.                     elseif deep and (type(v) == 'table' or type(v) == 'userdata') and (type(target[k]) == 'table' or type(target[k]) == 'userdata') then
  52.                         table_assign(target[k], v, deep)
  53.                     end
  54.                 end
  55.                 return target
  56.             end
  57.             table_assign(_args, args, true)
  58.             local result, response = pcall(requests.request, method, url, _args)
  59.             if result then
  60.                 response.json, response.xml = nil, nil
  61.                 return true, response
  62.             else
  63.                 return false, response
  64.             end
  65.         end)
  66.     end
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement