Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-isaac authored Jul 13, 2017
1 parent a26c4d3 commit f3e90df
Show file tree
Hide file tree
Showing 8 changed files with 1,502 additions and 0 deletions.
85 changes: 85 additions & 0 deletions AlmaApi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 GetRequest( requestUrl )
local webClient = types["System.Net.WebClient"]();
local response = nil;
log:Debug("Created Web Client");
webClient.Encoding = types["System.Text.Encoding"].UTF8;
webClient.Headers:Add("Accept: application/xml");
webClient.Headers:Add("Content-Type: application/xml");

local success, error = pcall(function ()
response = webClient:DownloadString(requestUrl);
end);

webClient:Dispose();
log:Debug("Disposed Web Client");

if(success) then
return response;
else
log:InfoFormat("Unable to get response from the request url: {0}", 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:InfoFormat("Unable to load response content as XML: {0}", error);
return nil;
end
else
log:Info("Unable to read response content");
end

return nil;
end

local function RetrieveHoldingsList( mmsId )
local requestUrl = AlmaApiInternal.ApiUrl .."bibs/"..
Utility.URLEncode(mmsId) .."/holdings?apikey=" .. Utility.URLEncode(AlmaApiInternal.ApiKey);
log:DebugFormat("Request URL: {0}", requestUrl);
local response = GetRequest(requestUrl);
log:DebugFormat("response = {0}", response);

return ReadResponse(response);
end

local function RetrieveBibs( mmsId )
local requestUrl = AlmaApiInternal.ApiUrl .. "bibs?apikey="..
Utility.URLEncode(AlmaApiInternal.ApiKey) .. "&mms_id=" .. Utility.URLEncode(mmsId);
log:DebugFormat("Request URL: {0}", requestUrl);

local response = GetRequest(requestUrl);
log:DebugFormat("response = {0}", response);

return ReadResponse(response);
end

-- Exports
AlmaApi.RetrieveHoldingsList = RetrieveHoldingsList;
AlmaApi.RetrieveBibs = RetrieveBibs;
Loading

0 comments on commit f3e90df

Please sign in to comment.