-
Notifications
You must be signed in to change notification settings - Fork 3
/
AlmaApi.lua
94 lines (70 loc) · 3.41 KB
/
AlmaApi.lua
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
local AlmaApiInternal = {};
AlmaApiInternal.ApiUrl = nil;
AlmaApiInternal.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");
-- Create a logger
local log = types["log4net.LogManager"].GetLogger(rootLogger .. ".AlmaApi");
AlmaApi = AlmaApiInternal;
local function RetrieveHoldingsList( mmsId )
local requestUrl = AlmaApiInternal.ApiUrl .."bibs/"..
Utility.URLEncode(mmsId) .."/holdings?apikey=" .. Utility.URLEncode(AlmaApiInternal.ApiKey);
local headers = {"Accept: application/xml", "Content-Type: application/xml"};
local response = WebClient.GetRequest(requestUrl, headers);
return WebClient.ReadResponse(response);
end
-- idType is either "mms_id" or "ie_id"
local function RetrieveBibs(id, idType)
local requestUrl = AlmaApiInternal.ApiUrl .. "bibs?apikey="..
Utility.URLEncode(AlmaApiInternal.ApiKey) .. "&" .. idType .. "=" .. Utility.URLEncode(id);
local headers = {"Accept: application/xml", "Content-Type: application/xml"};
local response = WebClient.GetRequest(requestUrl, headers);
return WebClient.ReadResponse(response);
end
local function RetrieveItemsSublist(mmsId, holdingId, offset )
local requestUrl = AlmaApiInternal.ApiUrl .."bibs/" ..
Utility.URLEncode(mmsId) .."/holdings/" .. Utility.URLEncode(holdingId) .. "/items?limit=100&offset=" .. tostring(offset) .. "&apikey=" ..
Utility.URLEncode(AlmaApiInternal.ApiKey);
local headers = {"Accept: application/xml", "Content-Type: application/xml"};
local response = WebClient.GetRequest(requestUrl, headers);
return WebClient.ReadResponse(response);
end
local function RetrieveItemsList(mmsId, holdingId)
local xmlResult, itemsNode;
local offset, totalItems = 0, 0;
repeat
local xmlSubresult = RetrieveItemsSublist(mmsId, holdingId, offset);
if (xmlResult == nil) then
xmlResult = xmlSubresult;
itemsNode = xmlResult:SelectSingleNode("/items");
totalItems = tonumber(itemsNode:SelectSingleNode("@total_record_count").Value);
log:DebugFormat("Holding contains {0} items", totalItems);
else
--Merge the next subset results with the working list
local itemNodes = xmlSubresult:SelectNodes("/items/item");
for i = 0, itemNodes.Count - 1 do
local nodeCopy = xmlResult:ImportNode(itemNodes:Item(i), true);
itemsNode:AppendChild(nodeCopy);
end
end
offset = offset + 100;
until totalItems <= offset;
return xmlResult;
end
local function RetrieveHoldingsRecordInfo(mmsId, holdingId)
local requestUrl = AlmaApiInternal.ApiUrl .."bibs/" ..
Utility.URLEncode(mmsId) .."/holdings/" .. Utility.URLEncode(holdingId) .. "/?apikey=" ..
Utility.URLEncode(AlmaApiInternal.ApiKey);
local headers = {"Accept: application/xml", "Content-Type: application/xml"};
local response = WebClient.GetRequest(requestUrl, headers);
return WebClient.ReadResponse(response);
end
-- Exports
AlmaApi.RetrieveHoldingsList = RetrieveHoldingsList;
AlmaApi.RetrieveBibs = RetrieveBibs;
AlmaApi.RetrieveItemsList = RetrieveItemsList;
AlmaApi.RetrieveHoldingsRecordInfo = RetrieveHoldingsRecordInfo;