Skip to content

Instantly share code, notes, and snippets.

@Valkrysa
Created March 8, 2023 20:25
Show Gist options
  • Save Valkrysa/5c13c41b02dec7ec709a307fc7835116 to your computer and use it in GitHub Desktop.
Save Valkrysa/5c13c41b02dec7ec709a307fc7835116 to your computer and use it in GitHub Desktop.
https://www.hiveworkshop.com/pastebin/284c1ffedc349416c5a95b82a0f6a6d7.16057
JASS:
library WebRequest requires W3HMC
/*****************************************************************
*
* v1.0.0, by TriggerHappy
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* JASS API to execute HTTP requests from maps hosted by hostbots which
* support W3HMC.
* _________________________________________________________________________
* 1. Script Installation
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* Copy this script and all of it's requirements to your map and save it.
* _________________________________________________________________________
* 2. Function API
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* function GetTriggerWebRequest takes nothing returns WebRequest
* function GetTriggerWebResponse takes nothing returns string
* function HttpReq takes string url, string params, boolean isPost, boolexpr callback returns WebRequest
* function HttpGet takes string url, boolexpr callback returns WebRequest
* function HttpPost takes string url, string params, boolexpr callback returns WebRequest
* function DestroyWebRequestTimed takes WebRequest req, real timeout returns nothing
* _________________________________________________________________________
* 3. Struct API
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* struct WebRequest
*
* static method create takes string url returns thistype
*
* method destroy takes nothing returns nothing
* method getResponse takes nothing returns string
* method begin takes nothing returns boolean
* method setHeader takes string header, string value returns nothing
* method setParams takes string params returns nothing
* method setPost takes boolean flag returns nothing
*
* boolexpr onComplete // The function to be executed when a response is received.
*
* readonly HMCRequest hmcReq
* readonly boolean isPost
* readonly string url
* readonly string params
* readonly boolean started
* player sender
* _________________________________________________________________________
* 4. Example
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* scope WebReqExample initializer Init
*
* private function OnReady takes nothing returns boolean
* local WebRequest req = GetTriggerWebRequest()
*
* call BJDebugMsg(GetTriggerWebResponse())
*
* call req.destroy()
* return false
* endfunction
*
* private function StartRequest takes nothing returns nothing
* call HttpGet("https://www.hiveworkshop.com/attachments/file-txt.293355/", Filter(function OnReady))
* endfunction
*
* //===========================================================================
* private function Init takes nothing returns nothing
* local trigger t = CreateTrigger()
* call TriggerRegisterPlayerEvent(t, Player(1), EVENT_PLAYER_END_CINEMATIC)
* call TriggerAddAction(t, function StartRequest)
* endfunction
*
* endscope
*
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
globals
constant string W3HMC_ARG_CURL_URL = "1"
constant string W3HMC_ARG_CURL_POST = "2"
constant string W3HMC_ARG_CURL_NOREPLY = "3"
constant string W3HMC_ARG_CURL_APPENDSECRET = "4"
constant string W3HMC_ARG_CURL_PARAMATERS = "5"
constant string W3HMC_ARG_CURL_APPENDREALM = "6"
constant string W3HMC_ARG_CURL_APPENDNAME = "7"
constant string W3HMC_ARG_CURL_ADDHEADER = "8"
constant string W3HMC_ARG_CURL_FOLLOWLOC = "9"
endglobals
function GetTriggerWebRequest takes nothing returns WebRequest
return GetTriggerHMCRequest()
endfunction
function GetTriggerWebResponse takes nothing returns string
return GetTriggerHMCRequest().getResponse()
endfunction
struct WebRequest extends array
readonly HMCRequest hmcReq
readonly boolean isPost
readonly string url
readonly string params
readonly boolean started
player sender
boolean noReply
boolean secure
boolean appendPlayerData
boolean allowRedirect
method destroy takes nothing returns nothing
call this.hmcReq.destroy()
endmethod
method getResponse takes nothing returns string
return this.hmcReq.getResponse()
endmethod
static method create takes string url returns thistype
local HMCRequest req = HMCRequest.create(W3HMC_REQUEST_HTTP)
local thistype this = thistype(req)
set this.url = url
set this.hmcReq = req
set this.isPost = false
set this.started = false
set this.appendPlayerData = false
set this.secure = false
set this.allowRedirect = false
set this.sender = GetLocalPlayer()
set this.params = ""
return this
endmethod
method begin takes nothing returns boolean
if (this.started) then
return false
endif
// build and start the request
call this.hmcReq.addArg(W3HMC_ARG_CURL_URL, this.url)
if (this.params != null and this.params != "") then
call this.hmcReq.addArg(W3HMC_ARG_CURL_PARAMATERS, params)
endif
if (this.isPost) then
call this.hmcReq.addArg(W3HMC_ARG_CURL_POST, "1")
endif
if (this.noReply) then
call this.hmcReq.addArg(W3HMC_ARG_CURL_NOREPLY, "1")
endif
if (this.secure) then
call this.hmcReq.addArg(W3HMC_ARG_CURL_APPENDSECRET, "1")
endif
if (this.appendPlayerData) then
call this.hmcReq.addArg(W3HMC_ARG_CURL_APPENDREALM, "1")
call this.hmcReq.addArg(W3HMC_ARG_CURL_APPENDNAME, "1")
endif
if (this.allowRedirect) then
call this.hmcReq.addArg(W3HMC_ARG_CURL_FOLLOWLOC, "1")
endif
call this.hmcReq.sendFrom(this.sender)
set this.started = true
return true
endmethod
method setPost takes boolean flag returns nothing
set this.isPost = flag
endmethod
method setParams takes string params returns nothing
set this.params = params
endmethod
method setHeader takes string header, string value returns nothing
call this.hmcReq.addArg(W3HMC_ARG_CURL_ADDHEADER, header + ":" + value)
endmethod
method operator onComplete= takes boolexpr func returns nothing
set this.hmcReq.onComplete = func
endmethod
method operator onComplete takes nothing returns boolexpr
return this.hmcReq.onComplete
endmethod
endstruct
function HttpReq takes string url, string params, boolean isPost, boolexpr callback returns WebRequest
local WebRequest req = WebRequest.create(url)
set req.onComplete = callback
call req.setPost(isPost)
call req.setParams(params)
call req.begin()
return req
endfunction
function HttpGet takes string url, boolexpr callback returns WebRequest
return HttpReq(url, null, false, callback)
endfunction
function HttpPost takes string url, string params, boolexpr callback returns WebRequest
return HttpReq(url, params, true, callback)
endfunction
private function DestroyWebRequestTimedCallback takes nothing returns nothing
local timer t = GetExpiredTimer()
local WebRequest req = WebRequest(GetStoredInteger(HMCRequest.Cache, "TIMERDATA", I2S(GetHandleId(t))))
call req.destroy()
call DestroyTimer(t)
set t = null
endfunction
// when destroying a request instantly after creating it can cause the data not to get sent.
// use this to destroy it after any period of time.
function DestroyWebRequestTimed takes WebRequest req, real timeout returns nothing
local timer t = CreateTimer()
call StoreInteger(HMCRequest.Cache, "TIMERDATA", I2S(GetHandleId(t)), req)
call TimerStart(t, timeout, false, function DestroyWebRequestTimedCallback)
set t = null
endfunction
endlibrary
Last edited: Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment