-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_NetRequest.ahk
219 lines (201 loc) · 6.83 KB
/
class_NetRequest.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Allows us to requests data from the web.
* You can use either WinHttpRequests or create a com instance of a browser
*
*
* @Parameters
* @url - [string] expects URL
*
* @Remarks
* for better output control JSON.ahk has been included.
* This is by no means necessary, but it's easier dealing with
* JSON objects, than working with the string responses.
*/
#Include %A_LineFile%\..\JSON.ahk
Class NetRequest {
request_header := {"GET":"application/json", "POST":"application/x-www-form-urlencoded;charset=utf-8"}
method := {"GET":"GET", "POST":"POST"}
status := "instantiated"
__New() {
}
/**
* sets the host and endpoint for the api you want to use
*
* @Parameters
* @host - [string] expects valid URL
* @endpoint - [string] valid endpoint for API you want to hit
* /rest/asset/v1/programs.json
*
* @Return
* Return
*/
apiSetup(host, endpoint) {
this.host := host
this.endpoint := endpoint
}
/**
* used for requesting data from APIs
* returns the response text from the http request
*
*
* @Parameters
* @id - [string] id of object you want to request from api
* @parms - [string] parameters for api in this format
* "name=Alf", "birthplace="space" etc
*
* @Return
* JSON Object
*/
apiRequest(method:="GET", body:="", parms*) {
start_query := 1
for each, parm in parms {
if (!Instr(parm, "=")) {
parameters := "/" . parm
} else {
parameters .= (start_query == 1 ? "?" : "&") . parm
start_query :=
}
}
request := this.createRequestObj(this.host . "/" . this.endpoint . parameters, method, body)
if (!request) {
return false
}
if (!Isobject(request)) {
return request
}
json_response := JSON.Load(request.ResponseText)
return json_response
}
/**
* checks status of URL by returning status code
*
* @Parameters
* @URL - [string] expects valid URL
*
* @Return
* string
*/
checkStatus(URL){
request := this.createRequestObj(URL)
return request.status
}
/**
* opens Internet explorer and navigates to URL
* can be visible or hidden
* returns com Object to interact with
*
* @Parameters
* @URL - [string] expects valid URL
* @visibility - [boolean] expects true of false
*
* @Return
* object
*/
webBrowser(URL, visibility:=false){
wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := visibility
wb.Navigate(URL)
while wb.Busy || wb.ReadyState != 4 {
Sleep, 100
}
return wb
}
/**
* creates a com object used for http requests
* can be used with both "GET" and "POST"
*
* @Parameters
* @endpoint - [string] expects valid URL or endpoint
* @method - [string] expects "GET" or "POST"
* @body - [string] values we want to pass via POST (optional for GET)
* @timouts - [object] expects timeouts passed in a key:value pairs exactly like below
* {"resolve_timeout": 0, "connect_timeout": 30000, "send_timeout": 30000, "receive_timeout": 60000}
*
* @Return
* object
*/
createRequestObj(endpoint:="", method:="GET", body:="", timeouts:=""){
request_obj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
if (!IsObject(request_obj)) {
msgbox, 4112,"Fatal Error","Unable to create HTTP object"
return "Fatal Error. Unable to create HTTP object"
}
this.defineTimeout(request_obj, timeouts)
try {
request_obj.Open(method, endpoint)
} catch error {
MsgBox, % error.message
FormatTime, current_time, %A_Now%, yyyy.MM.dd hh:mm:ss
fileappend % current_time ": " error.Message ", line:" error.line "`n`nEndpoint: " . endpoint, % A_ScriptDir "\netrequest_error.txt"
return error.Message
}
request_obj.SetRequestHeader("Content-Type", this.request_header[method])
if (body == "") {
request_obj.Send()
} else {
request_obj.Send(body)
}
try {
request_obj.WaitForResponse()
} catch error {
MsgBox, % error.message
FormatTime, current_time, %A_Now%, yyyy.MM.dd hh:mm:ss
fileappend % current_time ": " error.Message ", line:" error.line "`n`nEndpoint: " . endpoint, % A_ScriptDir "\netrequest_error.txt"
return error.Message
}
if (request_obj == "") {
msgbox, 4112,"Fatal Error","Couldn't receive response."
return "Fatal Error. Couldn't receive response."
}
return request_obj
}
/**
* defines the default timeouts or allows them to be adjusted
* timeouts are always defined in milliseconds
*
* @Parameters
* @com_obj - [object] expects valid com_object
* for http requests
* @resolve_timeout - [int] expects time in ms (default 0ms)
* @connect_timeout - [int] expects time in ms (default 30s)
* @send_timeout - [int] expects time in ms (default 30s)
* @receive_timeout - [int] expects time in ms (default 60s)
*
* @Return
* no return value
*/
defineTimeout(ByRef com_obj, timeouts){
if (IsObject(timeouts)) {
resolve_timeout := timeouts.resolve_timeout
connect_timeout := timeouts.connect_timeout
send_timeout := timeouts.send_timeout
receive_timeout := timeouts.receive_timeout
} else {
resolve_timeout := 0
connect_timeout := 30000
send_timeout := 30000
receive_timeout := 600000
}
com_obj.SetTimeouts(resolve_timeout, connect_timeout, send_timeout, receive_timeout)
}
/**
* searches the DOM for tags with passed attributes
*
* @Parameters
* @browser_obj - [object] expects browser_obj + .document
* @item - [string] expects valid attribute tag enclosed by "[]"
*
* @Return
* object
*/
searchForMatches(browser_obj, item){
matching_elements := []
all_elements := browser_obj.querySelectorAll(item)
Loop, % all_elements.length {
if (all_elements[A_Index-1]) {
matching_elements.push(all_elements[A_Index-1])
}
}
return matching_elements
}
}