About Monkey 2 › Forums › Monkey 2 Programming Help › Using monkey to call an api behind https?
This topic contains 3 replies, has 2 voices, and was last updated by jondecker76 2 days, 9 hours ago.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
March 2, 2019 at 6:53 pm #16102
Does monkey support https yet ? not touched anything monkey related or code related for a long while now so very much out of the loop, but something’s come up recently that if I even bother trying to code it would require me being able to call a https uri to grab some json or xml output..
also nice to see the site is still here.
March 4, 2019 at 5:43 pm #16104ParticipantI’m currently using monkey to interface with a few different REST APIs over https with no problem
March 4, 2019 at 11:45 pm #16105Participantany chance i could see an example ?
March 5, 2019 at 4:12 pm #16107ParticipantMonkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960Method post<T>:T(action:String,api:API)Local params:=New Map<String,String>Return post<T>(action,params,api)EndMethod post<T>:T(action:String,params:Map<String,String>,api:API)Global id:=0id+=1#If __TARGET__="windows"tmp=GetEnv( "TMP" )+"mx2_wget-"+id+".txt"#Elsetmp="/tmp/mx2_wget-"+id+".txt"#endifLocal p:=parameters.Copy()' We need to add the Action, Version and TimestampDebugPrint("POST ACTION: " + action)p.Add("Action", UrlEncode(action))p.Add("Timestamp", UrlEncode(UTC()))p.Add("Version", UrlEncode(api.version))' Add in extra parametersFor Local i:=Eachin paramsp.Add(i.Key,UrlEncode(i.Value))NextLocal stringToSign:=calculateStringToSignV2(p,api)DebugPrint("StringToSign: " + stringToSign)'sign and base64 encodeLocal signature64:=HMAC256_64(stringToSign,Self.secretKey)p.Add("Signature",UrlEncode(signature64))' build wget cmdLocal cmd:Stringcmd="wget --content-on-error -q -T 10 -O ~q" + Self.tmp +"~q --method=POST --body-data=~q" + BuildParamString(p) + "~q ~q" + api.url + "~q"DebugPrint("Command String:")DebugPrint(cmd)DebugPrint("URL:")Local httpRequest:=api.url+"?"+BuildParamString(p)DebugPrint(httpRequest)Local result:=system(cmd)DebugPrint("Result: " + result)Local ldstr:=LoadString(Self.tmp)DebugPrint("LoadStr: "+ldstr)If ldstr="" Then DebugPrint("LdStr successful")Return New T(httpRequest,result,ldstr)EndMethod calculateStringToSignV2:String(params:Map<String,String>,api:API)Local ret:Stringret="POST~n"ret+=region.endpoint.Replace("https://","")+"~n"ret+="/" + api.section + "/" + api.version + "~n"ret+=Self.BuildParamString(params)Return retEndThis is straight from my current Amazon MWS API wrapper. It’s kind of specific to what I’m doing, but that part that builds a request to send is the line starting with cmd=….
You should be able to modify this for your own needs pretty easily (and get rid of the the generic return type if you just need a string)
In my case as above, the type T has a constructor which accepts:
– httpRequest (the original httpRequest, which is useful for debugging and logging)-result (the result integer returned from the wget command. Also useful for logging, debugging and filtering
– ldstr (the actual string returned by the API call. In my case it’s XML, but it could be anything
Here is the New() method of the base class for all of my <T> return types (if that helps)
Monkey1234567891011121314151617181920212223242526272829303132Method New(httpRequest:String,result:String,xmlStr:String)Self.request=httpRequestSelf.xml=xmlStrSelf.cmdResult=resultIf xml="" Then' Probably a network connection error?Local type:="Client"Local code:="Blank XML Response. cmdResult=" + cmdResultLocal message:="Probably a network connection issue"error=New MWSError(type,code,message)Else' Determine if there has been an error, and handle accordinglySelf.doc=New XMLDocument()Self.doc.Parse(Self.xml)Self.requestId=GetRequestID()Self.ns=GetNamespace()'Print "Checking For error"Local err:=GetNode("Error",doc)If err Then' an error node was found! lets process!Local type:=GetNodeValue("Type",err)Local code:=GetNodeValue("Code",err) + " cmdResult=" + cmdResultLocal message:=GetNodeValue("Message",err)error=New MWSError(type,code,message)Else'Print "No error"error=NullEndifEndifEndAn an example of a return type that extends the above…
Monkey123456789101112131415Class ListOrders Extends MWSResponseField NextToken:StringField LastUpdatedBefore:StringField CreatedBefore:StringField Orders:Order[]Method New(httpRequest:String,result:String,xmlStr:String)Super.New(httpRequest,result,xmlStr)Local node:=docNextToken=GetNodeValue("NextToken",node)LastUpdatedBefore=GetNodeValue("LastUpdatedBefore",node)CreatedBefore=GetNodeValue("CreatedBefore",node)Orders=GetNodeArray<Order>("Orders",node)EndEndHere is an example of an actual request class calling the generic post method:
Monkey1234567Class AmazonMWS ExtensionMethod ListOrderItems:ListOrderItems(orderNumber:String)DebugPrint("Calling ListOrderItems")Local p:=New Parametersp.Map("AmazonOrderId",UrlEncode(orderNumber))Return post<ListOrderItems>("ListOrderItems",p,orders)End -
AuthorPosts
You must be logged in to reply to this topic.