public List<ShopifyMetafield> GetAllMetafields(ShopifyStoreAuth storeAuth, int? limit,
DateTime? createdMin, DateTime? createdMax, DateTime? updatedMin,
DateTime? updatedMax,
string fieldNamespace, string key, string valueType,
int? productId)
{
//Todo: Should we make this take a "dictionary" and then simply do "foreach key in dictionary"
if (!(valueType == "string" || valueType == "integer"))
{
throw new ArgumentException("valueType can only be 'string' or 'integer'");
}
if (limit > 250 || limit < 0)
{
throw new ArgumentException("limit must be between 1 and 250 inclusive");
}
if (storeAuth == null)
{
throw new ArgumentNullException("storeAuth");
}
if (productId == null && productId < 1)
{
throw new ArgumentException("productId must be >0");
}
List<ShopifyMetafield> returnList = limit != null
? new List<ShopifyMetafield>((int) limit)
: new List<ShopifyMetafield>();
var sbUrl = new StringBuilder();
sbUrl.Append(_protocol + storeAuth.StoreSubDomain + _domain + "/admin/");
if (productId != null)
{
sbUrl.Append("products/" + productId + "/");
}
sbUrl.Append("metafields.xml?");
//Append each of the filter terms.
if (limit != null)
{
sbUrl.Append("limit=" + limit);
}
if (createdMin != null)
{
sbUrl.Append("&created_at_min=" + HttpUtility.UrlEncode(DateTimeToShopifyString((DateTime) createdMin)));
}
if (createdMax != null)
{
sbUrl.Append("&created_at_max=" + HttpUtility.UrlEncode(DateTimeToShopifyString((DateTime) createdMax)));
}
if (updatedMin != null)
{
sbUrl.Append("&updated_at_min=" + HttpUtility.UrlEncode(DateTimeToShopifyString((DateTime) updatedMin)));
}
if (updatedMax != null)
{
sbUrl.Append("&updated_at_max=" + HttpUtility.UrlEncode(DateTimeToShopifyString((DateTime) updatedMax)));
}
if (!string.IsNullOrEmpty(key))
{
sbUrl.Append("&key=" + HttpUtility.UrlEncode(key));
}
if (!string.IsNullOrEmpty(fieldNamespace))
{
sbUrl.Append("&namespace=" + HttpUtility.UrlEncode(fieldNamespace));
}
if (valueType != string.Empty)
{
sbUrl.Append("&value_type=" + HttpUtility.UrlEncode(valueType));
}
XmlDocument xDoc = ShopifyGet(sbUrl.ToString().Replace("?&", "?"),
HashString(_appAuth.Secret + storeAuth.StoreAuthToken));
if (xDoc == null)
{
//ShopifyGet should be logging/reporting the error
return null;
}
XmlNodeList metaFields = xDoc.SelectNodes("//metafields/metafield");
if (metaFields != null)
foreach (XmlNode metaField in metaFields)
{
var metaFieldDoc = new XmlDocument();
metaFieldDoc.AppendChild(metaFieldDoc.ImportNode(metaField, true));
var serializedResponse =
new ShopifyResponse<ShopifyMetafield>(metaFieldDoc);
if (serializedResponse.State == ResponseState.OK)
{
returnList.Add(serializedResponse.ResponseObject);
}
else
{
//Log this error, ignore this metafield and continue with call so we can return the properly formatted metafields
XmlNodeList idNode = metaField.SelectNodes("//id");
if (idNode != null)
{
string metafieldId = idNode.Count > 0 ? idNode[0].InnerText : "null";
Logger.ErrorFormat(
"ShopifyCommunicator()::GetAllMetafields(): Couldn't parse metafield ({0}) returned in page of metafields.",
metafieldId);
}
}
}
return returnList;
}