viewpoint-particle

Author Topic: Problem with Maps  (Read 149 times)

bottomleypotts

  • Newbie
  • *
  • Posts: 41
Problem with Maps
« on: August 31, 2021, 07:06:12 am »
Code: Winbatch
#DefineFunction UDF_www(objHTTP,oHTTP)
Ret=MapCreate()
knownHeaders=`User-Agent,Content-Type,Referer`
Err=ErrorMode(@OFF)
If MapKeyExist(oHTTP,`Proxy Enabled`) Then   If MapKeyExist(oHTTP,`Proxy`) Then   If oHTTP[`Proxy Enabled`] Then   If oHTTP[`Proxy`]!=`` Then   objHTTP.SetProxy(2,oHTTP[`Proxy`],``)
If MapKeyExist(oHTTP,`EnableRedirects`) Then   objHTTP.Option(6)=oHTTP[`EnableRedirects`]
If !MapKeyExist(oHTTP,`Async`) Then   oHTTP[`Async`]=@FALSE
objHTTP.Open(oHTTP[`Method`],oHTTP[`URL`],oHTTP[`Async`])
If MapKeyExist(oHTTP,`Username`) Then   If MapKeyExist(oHTTP,`Password`) Then   If oHTTP[`Username`]!=`` Then   objHTTP.SetCredentials(oHTTP[`Username`],oHTTP[`Password`],0)
For i=1 to ItemCount(knownHeaders,`,`)
        header=ItemExtract(i,knownHeaders,`,`)
        If MapKeyExist(oHTTP,header) Then   objHTTP.SetRequestHeader(header,oHTTP[header])
Next i
If MapKeyExist(oHTTP,`Headers`)
        For i=1 to ItemCount(oHTTP[`Headers`],@CR)
                header=ItemExtract(i,oHTTP[`Headers`],@CR)
                objHTTP.SetRequestHeader(ItemExtract(1,header,@TAB),ItemExtract(2,header,@TAB))
        Next i
EndIf
If !MapKeyExist(oHTTP,`Data`) Then   oHTTP[`Data`]=``
objHTTP.Send(oHTTP[`Data`])
If oHTTP[`Async`]
        If !MapKeyExist(oHTTP,`Timeout`) Then   oHTTP[`Timeout`]=-1
        objHTTP.WaitForResponse(oHTTP[`Timeout`])
EndIf
If MapKeyExist(oHTTP,`Filename`)
        objStream=CreateObject(`ADODB.Stream`)
        objStream.Type=1
        objStream.Open()
        objStream.Write(objHTTP.ResponseBody)
        objStream.SaveToFile(oHTTP[`Filename`],2)
        objStream.Close()
        Drop(objStream)
        Ret[`ResponseText`]=oHTTP[`Filename`]
Else
        Ret[`ResponseText`]=objHTTP.ResponseText
EndIf
Ret[`GetAllResponseHeaders`]=StrReplace(objHTTP.GetAllResponseHeaders,@CRLF,@CR)
Ret[`Status`]=objHTTP.Status
Ret[`StatusText`]=objHTTP.StatusText
ErrorMode(Err)
Return Ret
#EndFunction

zAppName=`Testing Discord`
BoxTitle(zAppName)
BoxText(``)

zNow=TimeYmdHms()

zINI=zAppName:`.ini`
zAppFolder=IniReadPvt(`Settings`,`App Folder`,DirScript(),DirScript():zINI)
If StrSub(zAppFolder,StrLen(zAppFolder),1)!=`\` Then   zAppFolder:=`\`
zINI=zAppFolder:zINI

oHTTP=MapCreate()
objHTTP=CreateObject(`WinHttp.WinHttpRequest.5.1`)
oHTTP[`User-Agent`]=IniReadPvt(`Settings`,`User-Agent`,`Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0`,zINI)
oHTTP[`Proxy`]=`127.0.0.1:8888`
oHTTP[`Proxy Enabled`]=@False

zAPIendpoint=`https://discord.com/api/v9`

zBotToken=`***YOU HAVE TO GENERATE YOUR OWN TOKEN***`

oHTTP[`Content-Type`]=`application/x-www-form-urlencoded`
oHTTP[`Headers`]=`Authorization`:@TAB:`Bot `:zBotToken

oHTTP[`URL`]=zAPIendpoint:`/users/@me`
oHTTP[`Method`]=`GET`

Ret=UDF_www(objHTTP,oHTTP)
Rc=FilePut(DirScript():`Discord2.txt`,Ret[`ResponseText`])

Exit
 

I am in need of some assistance with Maps. The map "Ret" in the UDF_www has an issue. There should be 4 key-values but for the life of me I cannot get this to work. If you set a break point you will also notice some weird stuff happening with the map also - the "ResponseText" key-value does not populate in the debugger until the "GetAllResponseHeaders" value is populated (which it never does - and if you create a dummy key-value beforehand it will be deleted), before the "Status" and the "StatusText" values are populated (correctly).

Any help would be appreciated.

kdmoyers

  • Sr. Member
  • ****
  • Posts: 427
Re: Problem with Maps
« Reply #1 on: August 31, 2021, 10:56:19 am »
Take a look at the extra optional parameters to the MapCreate function.  You have to select two delimiter characters that will NOT appear in the data.  I think you might be getting snagged by commas.  Just a quick thought, I could be wrong.
-Kirby
The mind is everything; What you think, you become.

td

  • Tech Support
  • *****
  • Posts: 3872
    • WinBatch
Re: Problem with Maps
« Reply #2 on: August 31, 2021, 09:14:21 pm »
I believe the problem is a limit on how much text WinBatch Studio will display as the value of a map key/value pair. So the map is being populated but WinBatch Studio isn't displaying the value. Also, you should never try to debug a script with ErrorMode set to off. It just makes the task harder.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 3872
    • WinBatch
Re: Problem with Maps
« Reply #3 on: September 03, 2021, 09:20:26 am »
The next release of WinBatch+Compiler has a change in WinBatch Studio that will place text stating that the value is too large when a long text string is encountered while showing the contents of a map variable in the array view window. This change also preserves the display of the other values in the map when a value is to large.

Thanks for reporting the problem.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts

  • Newbie
  • *
  • Posts: 41
Re: Problem with Maps
« Reply #4 on: September 03, 2021, 07:20:49 pm »
Funny thing is arrays can handle the text. As can a single string variable. But not a Map variable. So I assume they're handled differently - which doesn't make sense to me but anyway. Found an alternative solution that works for me.

td

  • Tech Support
  • *****
  • Posts: 3872
    • WinBatch
Re: Problem with Maps
« Reply #5 on: September 03, 2021, 10:03:25 pm »
You really don't need an alternate solution your map implementation works just fine. It is the data display that is off a bit.

The similarity of arrays and maps could be called the illusion of abstraction. Arrays and maps appear to be almost the same at the script level but underneath the hood, they are implemented in completely different ways in both the interpreter and how they are displayed in WinBatch Studio. Abstraction is a fundamental concept in both computer programming and computer science just like other sciences. 

A side benefit of the map data display changes made to fix this issue is that it increases the permitted display size of map values in key/value pairs. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade