-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWebClient.lua
More file actions
102 lines (82 loc) · 3.36 KB
/
WebClient.lua
File metadata and controls
102 lines (82 loc) · 3.36 KB
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
WebClient = {};
local apiKey = nil;
local types = {};
types["log4net.LogManager"] = luanet.import_type("log4net.LogManager");
types["System.Net.WebClient"] = luanet.import_type("System.Net.WebClient");
types["System.Text.Encoding"] = luanet.import_type("System.Text.Encoding");
types["System.Xml.XmlTextReader"] = luanet.import_type("System.Xml.XmlTextReader");
types["System.Xml.XmlDocument"] = luanet.import_type("System.Xml.XmlDocument");
types["System.IO.StreamReader"] = luanet.import_type("System.IO.StreamReader");
-- Create a logger
local log = types["log4net.LogManager"].GetLogger(rootLogger .. ".AlmaApi");
local function Initialize(key)
apiKey = key;
end
local function Redact(value)
if value == nil or apiKey == nil or apiKey == "" then
return value;
end
-- Escape any pattern-special characters so the API key is treated literally.
local literalPattern = apiKey:gsub("(%W)", "%%%1");
-- gsub returns the replacement count as a second value; the parens drop it.
return (tostring(value):gsub(literalPattern, "[REDACTED]"));
end
local function GetWebExceptionMessage(exception)
local message = "";
if exception and exception.Message then
message = exception.Message;
if (exception.InnerException) then
message = message .. "\r\n" .. GetWebExceptionMessage(exception.InnerException);
-- luanet returns the literal string "Response" when this property is unbound on the exception type.
if exception.InnerException.Response and exception.InnerException.Response ~= "Response" then
local streamReader = types["System.IO.StreamReader"](exception.InnerException.Response:GetResponseStream());
local responseContent = streamReader:ReadToEnd();
if responseContent and #responseContent > 0 then
message = message .. "\r\nResponse body: " .. responseContent;
end
end
end
elseif exception then
message = exception;
end
return message;
end
local function GetRequest(requestUrl, headers)
local webClient = types["System.Net.WebClient"]();
local response = nil;
webClient.Encoding = types["System.Text.Encoding"].UTF8;
for _, header in ipairs(headers) do
webClient.Headers:Add(header);
end
log:DebugFormat("Request URL: {0}", Redact(requestUrl));
local success, error = pcall(function ()
response = webClient:DownloadString(requestUrl);
end);
webClient:Dispose();
if(success) then
return response;
else
log:ErrorFormat("Unable to get response from the request url: {0}", Redact(GetWebExceptionMessage(error)));
end
end
local function ReadResponse( responseString )
if (responseString and #responseString > 0) then
local responseDocument = types["System.Xml.XmlDocument"]();
local documentLoaded, error = pcall(function ()
responseDocument:LoadXml(responseString);
end);
if (documentLoaded) then
return responseDocument;
else
log:WarnFormat("Unable to load response content as XML: {0}", error);
return nil;
end
else
log:Warn("Unable to read response content.");
end
return nil;
end
--Exports
WebClient.Initialize = Initialize;
WebClient.GetRequest = GetRequest;
WebClient.ReadResponse = ReadResponse;