-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhttp-request.brs
136 lines (114 loc) · 4.44 KB
/
http-request.brs
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
'********************************************************************
'** http-request.brs
'********************************************************************
'** Example:
'** req = HttpRequest({
'** url: "http://www.apiserver.com/login",
'** method: "POST",
'** headers: { "Content-Type": "application/json" },
'** data: { user: "johndoe", password: "12345" }
'** })
'** req.send()
'********************************************************************
function HttpRequest(params = invalid as Dynamic) as Object
url = invalid
method = invalid
headers = invalid
data = invalid
timeout = 0
retries = 1
interval = 500
if params <> invalid then
if params.url <> invalid then url = params.url
if params.method <> invalid then method = params.method
if params.headers <> invalid then headers = params.headers
if params.data <> invalid then data = params.data
if params.timeout <> invalid then timeout = params.timeout
if params.retries <> invalid then retries = params.retries
if params.interval <> invalid then interval = params.interval
end if
obj = {
_timeout: timeout
_retries: retries
_interval: interval
_deviceInfo: createObject("roDeviceInfo")
_url: url
_method: method
_requestHeaders: headers
_data: data
_http: invalid
_isAborted: false
_isProtocolSecure: function(url as String) as Boolean
return left(url, 6) = "https:"
end function
_createHttpRequest: function() as Object
request = createObject("roUrlTransfer")
request.setPort(createObject("roMessagePort"))
request.setUrl(m._url)
request.retainBodyOnError(true)
request.enableCookies()
if m._requestHeaders <> invalid then request.setHeaders(m._requestHeaders)
if m._method <> invalid then request.setRequest(m._method)
'Checks if URL protocol is secured, and adds appropriate parameters if needed
if m._isProtocolSecure(m._url) then
request.setCertificatesFile("common:/certs/ca-bundle.crt")
request.addHeader("X-Roku-Reserved-Dev-Id", "")
request.initClientCertificates()
end if
return request
end function
getPort: function()
if m._http <> invalid then
return m._http.getPort()
else
return invalid
end if
end function
getCookies: function(domain as String, path as String) as Object
if m._http <> invalid then
return m._http.getCookies(domain, path)
else
return invalid
end if
end function
send: function(data = invalid as Dynamic) as Dynamic
timeout = m._timeout
retries = m._retries
response = invalid
if data <> invalid then m._data = data
if m._data <> invalid and getInterface(m._data, "ifString") = invalid then
m._data = formatJson(m._data)
end if
while retries > 0 and m._deviceInfo.getLinkStatus()
if m._sendHttpRequest(m._data) then
event = m._http.getPort().waitMessage(timeout)
if m._isAborted then
m._isAborted = false
m._http.asyncCancel()
exit while
else if type(event) = "roUrlEvent" then
response = event
exit while
end if
m._http.asyncCancel()
timeout *= 2
sleep(m._interval)
end if
retries--
end while
return response
end function
_sendHttpRequest: function(data = invalid as Dynamic) as Dynamic
m._http = m._createHttpRequest()
if data <> invalid then
return m._http.asyncPostFromString(data)
else
return m._http.asyncGetToString()
end if
end function
abort: function()
m._isAborted = true
end function
}
return obj
end function