Ejemplo n.º 1
0
        public List<ShopifyOrder> GetListAllOrders(ShopifyStoreAuth storeAuth)
        {
            List<ShopifyOrder> returnList = null;
            XmlDocument responseDoc = ShopifyGet((_protocol + storeAuth.StoreSubDomain + _domain + "/admin/orders.xml"),
                                                 HashString(_appAuth.Secret + storeAuth.StoreAuthToken));

            if (responseDoc != null)
            {
                XmlNodeList errors = responseDoc.SelectNodes("//errors/error");
                if (errors != null)
                    if (errors.Count != 0)
                    {
                        var sw = new StringWriter();
                        var xw = new XmlTextWriter(sw);
                        responseDoc.WriteTo(xw);

                        Logger.ErrorFormat("ShopifyCommunicator::GetAllOrders(): Shopify returned errors: {0}",
                                           sw);
                        //for now we fall through and take whatever orders we can.
                    }

                XmlNodeList orders = responseDoc.SelectNodes("//orders/order");

                if (orders != null)
                {
                    returnList = new List<ShopifyOrder>(orders.Count);

                    foreach (XmlNode order in orders)
                    {
                        var orderDoc = new XmlDocument();
                        orderDoc.AppendChild(orderDoc.ImportNode(order, true));
                        var serializedResponse = new ShopifyResponse<ShopifyOrder>(orderDoc);

                        if (serializedResponse.State == ResponseState.OK)
                        {
                            returnList.Add(serializedResponse.ResponseObject);
                        }
                        else
                        {
                            XmlNodeList orderId = responseDoc.SelectNodes("//order/id");
                            if (orderId != null)
                            {
                                string id = orderId.Count > 0 ? orderId[0].InnerText : "unknown";
                                Logger.ErrorFormat(
                                    "ShopifyCommunicator()::GetListAllOrders(): Error deserializing order id={0}.", id);
                            }
                        }
                    }
                }
            }
            return returnList;
        }
Ejemplo n.º 2
0
        public List<ShopifyProduct> GetPageOfProducts(ShopifyStoreAuth storeAuth, int numWanted, int pageNumber)
        {
            if (storeAuth == null)
            {
                Logger.Warn("ShopifyCommunicator::GetPageOfProducts: storeAuth cannot be null");
                return null;
            }

            if (!(numWanted > 0 && numWanted < 251 && pageNumber > 0))
            {
                Logger.WarnFormat(
                    "ShopifyCommunicator::GetPageOfProducts(): numWanted({0}) must be between 0 and 251, pageNumber({1}) must be >0.",
                    numWanted, pageNumber);
                return null;
            }

            var url = new StringBuilder();
            url.Append(_protocol);
            url.Append(storeAuth.StoreSubDomain);
            url.Append(_domain);
            url.Append("/admin/products.xml?");

            url.Append("limit=" + numWanted);
            url.Append("&page=" + pageNumber);

            XmlDocument xDoc = ShopifyGet(url.ToString(), HashString(_appAuth.Secret + storeAuth.StoreAuthToken));
            //Now the fun part of parsing this xml.
            if (xDoc == null)
            {
                return null;
            }

            XmlNodeList productNodeList = xDoc.SelectNodes("//products/product");

            var returnList = new List<ShopifyProduct>();
            //			Foreach product found, serialize and add to return list
            if (productNodeList != null)
                foreach (XmlNode productNode in productNodeList)
                {
                    var productDoc = new XmlDocument();
                    productDoc.AppendChild(productDoc.ImportNode(productNode, true));
                    var serializedResponse = new ShopifyResponse<ShopifyProduct>(productDoc);

                    if (serializedResponse.State == ResponseState.OK)
                    {
                        returnList.Add(serializedResponse.ResponseObject);
                    }
                    else
                    {
                        XmlNodeList idNode = productNode.SelectNodes("//id");
                        if (idNode != null)
                        {
                            string productId = idNode.Count > 0 ? idNode[0].InnerText : "null";
                            Logger.ErrorFormat(
                                "ShopifyCommunicator()::GetPageOfProducts(): Couldn't parse product ({0}) returned in page of product.",
                                productId);
                        }
                    }
                }

            return returnList;
        }
Ejemplo n.º 3
0
            /// <summary>
            ///
            /// </summary>
            /// <param name="storeAuth"></param>
            /// <param name="limit"></param>
            /// <param name="createdMin"></param>
            /// <param name="createdMax"></param>
            /// <param name="updatedMin"></param>
            /// <param name="updatedMax"></param>
            /// <param name="fieldNamespace"></param>
            /// <param name="key"></param>
            /// <param name="valueType"></param>
            /// <param name="productId">If productId is >0 get all metafields for that product, if null get metafields tagged to the shop. </param>
            /// <exception cref="ArgumentNullException"></exception>
            /// <returns></returns>
            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;
        }