Ejemplo n.º 1
0
        private Dictionary <string, Dictionary <string, string> > getClientInventory(string pid, string clientID)
        {
            Dictionary <string, Dictionary <string, string> > products = new Dictionary <string, Dictionary <string, string> >();
            string responseString = string.Empty;
            string locale         = Properties.Settings.Default.code;
            string url;

            if (locale == "US" || locale == "CA" || locale == "MX")
            {
                url = String.Format("http://production-us-adidasgroup.demandware.net/s/adidas-{0}/dw/shop/v15_6/products/({1})?client_id={2}&expand=availability,variations,prices", locale, pid, clientID);
            }
            else if (locale == "PT")
            {
                url = String.Format("http://production-store-adidasgroup.demandware.net/s/adidas-MLT/dw/shop/v15_6/products/({0})?client_id={1}&expand=availability,variations,prices", pid, clientID);
            }
            else
            {
                url = String.Format("http://production.store.adidasgroup.demandware.net/s/adidas-{0}/dw/shop/v15_6/products/({1})?client_id={2}&expand=availability,variations,prices", locale, pid, clientID);
            }


            using (var client = new TimedWebClient())
            {
                client.Headers[HttpRequestHeader.Accept]    = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
                client.Headers[HttpRequestHeader.Referer]   = String.Format("http://www.{0}/", Properties.Settings.Default.locale);

                responseString = client.DownloadString(url);
            }

            Dictionary <string, dynamic> json = json_decode(responseString);

            for (int i = 0; i < json["data"][0]["variants"].Count; i++)
            {
                int    index = i;
                string id    = json["data"][0]["variants"][index]["product_id"];

                products[id] = new Dictionary <string, string>();

                using (var client = new TimedWebClient())
                {
                    client.Headers[HttpRequestHeader.Accept]    = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
                    client.Headers[HttpRequestHeader.Referer]   = String.Format("http://www.{0}/", Properties.Settings.Default.locale);
                    responseString = client.DownloadString(url.Replace(pid, id));
                }

                json = json_decode(responseString);

                products[id]["size"]       = json["data"][0]["variation_attributes"][0]["values"][index]["name"];
                products[id]["stockcount"] = Convert.ToInt32(json["data"][0]["inventory"]["ats"]).ToString();
            }

            return(products);
        }
Ejemplo n.º 2
0
        public Dictionary <string, Dictionary <string, string> > getInventory(string pid, string clientID, string splash_url = null)
        {
            string url = String.Format("http://www.{0}/on/demandware.store/Sites-adidas-{1}-Site/{2}/Product-GetVariants?pid={3}", Properties.Settings.Default.locale, Properties.Settings.Default.code, marketsList[Properties.Settings.Default.code], pid);

            string responseString = string.Empty;

            using (var client = new TimedWebClient())
            {
                try
                {
                    client.Headers[HttpRequestHeader.Accept]    = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";

                    if (!String.IsNullOrEmpty(splash_url))
                    {
                        client.Headers[HttpRequestHeader.Referer] = splash_url;
                    }
                    else
                    {
                        client.Headers[HttpRequestHeader.Referer] = String.Format("http://www.{0}/", Properties.Settings.Default.locale);
                    }

                    responseString = client.DownloadString(url);
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null || ex.Status == WebExceptionStatus.Timeout)
                    {
                        var resp = (HttpWebResponse)ex.Response;
                        if (String.IsNullOrEmpty(clientID))
                        {
                            MessageBox.Show(String.Format("WebRequest error({0}): Could not get variant stock for product {1}, please specify a valid client ID to get client stock and try again.", resp.StatusCode, pid), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return(null);
                        }
                        else
                        {
                            return(getClientInventory(pid, clientID));
                        }
                    }
                }
            }

            if (responseString.Contains("<html"))
            {
                if (String.IsNullOrEmpty(clientID))
                {
                    MessageBox.Show("Could not get product variant stock, please specify a valid client ID to get client stock and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }
                else
                {
                    return(getClientInventory(pid, clientID));
                }
            }

            Dictionary <string, Dictionary <string, string> > products = new Dictionary <string, Dictionary <string, string> >();
            Dictionary <string, dynamic> json = json_decode(responseString);

            for (int i = 0; i < json["variations"]["variants"].Count; i++)
            {
                int    index     = i;
                string stylecode = json["variations"]["variants"][index]["id"];
                products[stylecode] = new Dictionary <string, string>();

                products[stylecode]["size"]       = json["variations"]["variants"][index]["attributes"]["size"];
                products[stylecode]["stockcount"] = Convert.ToInt32(json["variations"]["variants"][index]["ATS"]).ToString();
            }

            return(products);
        }