BinaryBrother Posted November 7, 2021 Posted November 7, 2021 (edited) I'm working on interfacing with an HTTP Rest API. It's pretty simple stuff, mostly. The target API allows Basic Auth requests... In fact, it only supports Basic Auth, for now. Here is an example of my implementation of interfacing with the WinHTTP Object, for the GET Method. expandcollapse popupFunc __GET($pEndpoint, $pAuth = Null) Local $lHTTP_Object, $lReturn[3] $lHTTP_Object = ObjCreate("winhttp.winhttprequest.5.1") If @error Then _CW("> GET: Error Creating WinHTTP Object!") Return SetError(1,0, False) ;Failed to interface with the COM Object EndIf _CW("> GET: WinHTTP Object sucessfully created.") $lHTTP_Object.Open("GET", $pEndpoint, False) If @error Then _CW("> GET: Error @ WinHTTP.Open") Return SetError(2,0,False) ; Failed to Open Connection. EndIf _CW("> GET: WinHTTP.Open("&$pEndpoint&") was opened sucessfully.") If IsArray($pAuth) Then _CW("> GET::Auth - Credentials supplied...") If Not IsArray($pAuth) Then _CW("> GET::Auth - Supplied Credential Property was not an Array!! Provide Credentials in an Array!") Return SetError(3,0,False) ; An Array wasn't provided for Authentication Credentials EndIf _CW("> GET::Auth - Credential Array Detected...") If Not UBound($pAuth) = 2 Then _CW("> Get::Auth - Credential Array is the wrong size. $Array[2] Required. (Username[0], $Password[1])") Return SetError(3,1,False) ; The supplied Array wasn't the correct size. EndIf _CW("> GET::Auth - Credential Array Validated...") $lHTTP_Object.SetCredentials($pAuth[0], $pAuth[1], 0) If @error Then _CW("> GET::Auth - A COM Error Occured at WinHTTP.SetCredentials()!!!") Else _CW("> GET::Auth - Supplied Username: " & $pAuth[0]) _CW("> GET::Auth - Supplied Password: " & $pAuth[1]) EndIf EndIf $lHTTP_Object.Send(False) If @error Then _CW("> GET: A COM Error Occured at WinHTTP.Send(False)!!!") Return SetError(4,0, False) ; Failed to transmit request. EndIf _CW("> GET: WinHTTP.Send(False) was a success.") $lReturn[0] = $lHTTP_Object.Status If @error Then _CW("> GET: Accessing the Property WinHTTP.Status caused a COM Error!!!") Return SetError(5,0,False) ; Failed to access Object Property EndIf _CW("> GET: WinHTTP.Status = " & $lReturn[0]) $lReturn[1] = $lHTTP_Object.ResponseText If @error Then _CW("> GET: Accessing the Propert WinHTTP.ResponseText caused a COM Error!!!") Return SetError(6,0,False) ; Failed to access Object Property EndIf _CW("> GET: WinHTTP.ResponseText = ") _CW($lReturn[1]) Return $lReturn EndFunc The _CW function is simply a ConsoleWrite() and logging method... If you'll notice, I am @error checking the COM interfaces using basic @error checking, which is only made available in AutoIt by registering an Error Handler for "AutoIt.Error", through the ObjEvent method, like this... Local $oErrorHandler = ObjEvent("AutoIt.Error", "ErrorHandlingFunction") My Summarized Problem: I need to make sure my UDF registers an Error handler for the COM Interface, or I will not be able to catch COM Errors using @Error. What would the proper procedure be for insuring that an Error Handler has been established and if not, then set one. Can I set a unique handler without causing issues, in the event that the user has already established a handler? Local $Unique_0X1234567778237237237 = ObjEvent("AutoIt.Error", "UniqueHandler_12345554545566") Or will that simply override/redirect an existing handler, regardless? I know I could figure some of this out, but I consider this to be a challenge that could redirect my focus from the project that I'm trying to complete and figured someone may have already solved this equation. As usual, thanks for your replies. Edited November 7, 2021 by BinaryBrother Solved SIGNATURE_0X800007D NOT FOUND
water Posted November 7, 2021 Posted November 7, 2021 See the Remarks section of ObjEvent: "If the second parameter is omitted, it will return a string containing the name of the current Error handler function. If no Error handler function has been set, it will return an empty string." My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
BinaryBrother Posted November 7, 2021 Author Posted November 7, 2021 Doh! That's all I needed! Thanks, water! SIGNATURE_0X800007D NOT FOUND
water Posted November 7, 2021 Posted November 7, 2021 If you want to close a thread and mark one of the replies as the one which solved the problem simply click on "Mark as Solution" at the bottom of the respective post So others can see which post solved the problem. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now