private XmlDocument ShopifyGet(string path, string storePassword)
{
if (path == null || storePassword == null)
{
Logger.Warn("ShopifyCommunicator::ShopifyGet(): Was passed a null path or storePassword, failing.");
return null;
}
//create XmlUrlResover so we can use authentication
var urlResolver = new XmlUrlResolver();
var cCache = new CredentialCache
{{new Uri(path), "Basic", new NetworkCredential(_appAuth.User, storePassword)}};
//path = path.Replace("http://", "http://" + appAuth.User + ":" + storePassword + "@");
urlResolver.Credentials = cCache;
//Settings for the XmlReader
var settings = new XmlReaderSettings {XmlResolver = urlResolver};
var xDoc = new XmlDocument();
//Try to create the connection, create our Reader
try
{
ApiLogger.DebugFormat("ShopifyCommunicator:: ShopifyGet(): Attempting HTTP GET path={0}", path);
XmlReader xRead = XmlReader.Create(path, settings);
xDoc.Load(xRead);
xRead.Close();
ApiLogger.DebugFormat("ShopifyCommunicator:: ShopifyGet(): Success: HTTP GET path={0} .", path);
return CleanupXmlForDeserialization(xDoc);
}
catch (WebException web)
{
//Log the exception
Logger.Error("ShopifyCommunicator::ShopifyGet() web exception: " + web.Message + " " + web.Response);
if (web.Response != null)
{
if (web.Response.Headers.AllKeys.Contains("Status"))
{
if (web.Response.Headers["Status"] == "401")
{
Logger.ErrorFormat(
"ShopifyCommunicator::ShopifyGet(): Invalid authenitcation tokens for path ={0}", path);
}
else if (web.Response.Headers["Status"] == "404")
{
Logger.ErrorFormat("ShopifyCommunicator::ShopifyGet(): Couldnt GET from path ={0}", path);
}
}
Stream wResponse = web.Response.GetResponseStream();
var reader = new StreamReader(wResponse);
string responseMessage = reader.ReadToEnd();
reader.Close();
Logger.ErrorFormat("ShopifyCommunicator::ShopifyGet() web exception, Shopify's Response is: {0} ",
responseMessage);
return IsValidXml(responseMessage); //returns XmlDocument if valid error, or null.
}
}
Logger.Warn("ShopifyCommunicator:: ShopifyGet() is returning null XmlDocument");
return null;
}