User Tools

Site Tools



minionlib

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
minionlib [2020/12/13 17:02] – [HTTPRequest] kaliminionlib [2021/04/30 18:13] (current) ace
Line 88: Line 88:
     *Queues and Fires the Event with 1-n arguments. Eventname and arguments need to be strings.      *Queues and Fires the Event with 1-n arguments. Eventname and arguments need to be strings. 
     *Use RegisterEventHandler("eventname", handlerfunc) , to register a lua function which will handle the fired event.     *Use RegisterEventHandler("eventname", handlerfunc) , to register a lua function which will handle the fired event.
 +    *Requires at least 1 argument, even if it's a blank string. (ex. QueueEvent("some event",""))
  
  
Line 250: Line 251:
 <code lua> <code lua>
 function SendHttpRequest() function SendHttpRequest()
- local API_Key = "XXXXXX-587C-3XXX-90B5-XXXXXXXXXXXXXXX3F-XXX-4A79-XXX-XXXXXXXXXX" +   local function success(str, header, statuscode) 
- local function success(str,header,statuscode) +      d("HTTP Request: success."
- d("HTTP Request: success."+      d("HTTP Result Header: " .. tostring(header)
- d("HTTP Result Header: "..header) +      d("HTTP Result StatusCode: " .. tostring(statuscode))
- d("HTTP Result StatusCode: "..tostring(statuscode))+
  
- local data = json.decode(str) +      local data = json.decode(str) 
- if data then +      if data then 
- d("HTTP Request: data valid. Fetched Characters:") +         d("HTTP Request: data valid. Currency Coin info:") 
- for _in pairs(data) do +         d(data) 
- d(v.name+      end 
- end +       
- return data +      local function HeadersTable(header) 
- end +         if type(header) == "string" and #header > 0 then 
- end+            header = string.match(header,".?%c(.*)") -- Removing the first entry 
 +            local tbl = {} 
 +            for w in header:gmatch("[^%c]+") do 
 +               local k,= string.match(w,"(.*): (.*)"
 +               tbl[k] = v 
 +            end 
 +            table.print(tbl) 
 +            return tbl 
 +         end 
 +      end 
 +       
 +      header = HeadersTable(header) -- if you want to convert the header string to a table 
 +   end
  
- local function failed(error,header,statuscode) +   local function failed(error, header, statuscode) 
- d("HTTP Failed Error: "..error) +      d("HTTP Failed Error: " .. error) 
- d("HTTP Failed Header: "..header) +      d("HTTP Failed Header: " .. header) 
- d("HTTP Failed StatusCode: "..tostring(statuscode)) +      d("HTTP Failed StatusCode: " .. tostring(statuscode)) 
- d(str or "Updating Characters failed, but nothing returned."+   end
- end+
  
- local params = { +   local params = { 
- host = "api.guidwars2.com", +      host = "api.guildwars2.com", 
- path = "/v2/characters?ids=all&access_token=.. API_Key+      path = "/v2/currencies?ids=1", 
- port = 443, +      port = 443, 
- method = "GET", -- "GET" or "POST" +      method = "GET", -- "GET","POST","PUT","DELETE" 
- https = true, +      https = true
- body = "", +      onsuccess = success, 
- headers = "", +      onfailure = failed, 
- onsuccess = success, +      getheaders = true--true will return the headersif you dont need it you can leave this at nil 
- onfailure = failed, +      body = "", --optional, if not required for your call can be nil or "" 
- getheaders = true -- if true, headers will be returned +      headers = {}, --optional, if not required for your call can be nil or "" 
-+   
- HttpRequest(params)+ 
 +   HttpRequest(params)
 end end
 </code> </code>
minionlib.1607878971.txt.gz · Last modified: 2020/12/13 17:02 by kali