コード例 #1
0
        public static async Task<ShopCart> addToCart(Listing listing)
        {
            int listing_id = listing.listing_id;
            HttpClient client = new HttpClient();
            string baseURL = App.baseURL, errorMessage = "";
            ShopCart current_cart = new ShopCart();

            if (App.logged_in == false)         // this function only applies to a logged in user
                return current_cart;

            List<Parameter> parameters = new List<Parameter>();
            parameters.Add(new Parameter("listing_id", listing_id.ToString()));

            baseURL = string.Format("{0}/users/{1}/carts", baseURL, App.userID);
            baseURL = AuthenticationAccess.addAuthentication(baseURL, parameters, "POST");

            // Create the POST request content
            var values = new List<KeyValuePair<string, string>>
            {
                // add the information pair to the values list
                new KeyValuePair<string, string>("listing_id", listing_id.ToString())
            };          

            try
            {
                HttpResponseMessage response = await client.PostAsync(baseURL, new FormUrlEncodedContent(values));

                var jsonStream = await response.Content.ReadAsStreamAsync();
                //var jsonString = await response.Content.ReadAsStringAsync();

                using (StreamReader reader = new StreamReader(jsonStream))
                {
                    var serializer = new DataContractJsonSerializer(typeof(ShopCart));

                    current_cart = (ShopCart)serializer.ReadObject(jsonStream);
                }                
            }
            catch(Exception e)
            {
                errorMessage = e.Message;
            }

            return current_cart;
        }
コード例 #2
0
        public static async Task<ShopCart> addToCart_WithVariations(Listing listing)
        {
            int listing_id = listing.listing_id;
            HttpClient client = new HttpClient();
            string baseURL = App.baseURL, errorMessage = "";
            ShopCart current_cart = new ShopCart();

            if (App.logged_in == false)         // this function only applies to a logged in user
                return current_cart;

            // Build variations string
            string variations = "{";
            foreach (var variation in listing.variations.results)
            {
                if (variation.selected_option_id != -1)
                {
                    variations += "\"" + Convert.ToString(variation.property_id) + "\"" + ":" + Convert.ToString(variation.selected_option_id) + ",";
                }
            }
            if (variations[variations.Length - 1] == ',')
                variations = variations.Substring(0, variations.Length - 1) + '}';

            // parameters
            List<Parameter> parameters = new List<Parameter>();
            parameters.Add(new Parameter("listing_id", listing_id.ToString()));
            parameters.Add(new Parameter("quantity", listing.quantity_chosen.ToString()));
            parameters.Add(new Parameter("selected_variations", variations));

            baseURL = string.Format("{0}/users/{1}/carts", baseURL, App.userID);
            baseURL = AuthenticationAccess.addAuthentication(baseURL, parameters, "POST");       

            // Create the POST request content
            var values = new List<KeyValuePair<string, string>>
            {
                // add the information pair to the values list
                new KeyValuePair<string, string>("listing_id", listing_id.ToString()),
                new KeyValuePair<string, string>("quantity", listing.quantity_chosen.ToString()),
                new KeyValuePair<string, string>("selected_variations", variations)
            };

            try
            {
                HttpResponseMessage response = await client.PostAsync(baseURL, new FormUrlEncodedContent(values));

                var jsonStream = await response.Content.ReadAsStreamAsync();
                //var jsonString = await response.Content.ReadAsStringAsync();

                using (StreamReader reader = new StreamReader(jsonStream))
                {
                    var serializer = new DataContractJsonSerializer(typeof(ShopCart));

                    current_cart = (ShopCart)serializer.ReadObject(jsonStream);
                }
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
            }

            return current_cart;
        }