private Cart GetCart(string cartId, CommerceContext commerceContext, string baseUrl)
        {
            var shopName    = commerceContext.CurrentShopName();
            var shopperId   = commerceContext.CurrentShopperId();
            var customerId  = commerceContext.CurrentCustomerId();
            var environment = commerceContext.Environment.Name;

            var url = string.Format(Constants.Settings.EndpointUrl, baseUrl, cartId);

            var client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.Settings.AppJson));
            client.DefaultRequestHeaders.Add(Constants.Settings.ShopName, shopName);
            client.DefaultRequestHeaders.Add(Constants.Settings.ShopperId, shopperId);
            client.DefaultRequestHeaders.Add(Constants.Settings.Language, "en-US");
            client.DefaultRequestHeaders.Add(Constants.Settings.Environment, environment);
            client.DefaultRequestHeaders.Add(Constants.Settings.CustomerId, customerId);
            client.DefaultRequestHeaders.Add(Constants.Settings.Currency, commerceContext.CurrentCurrency());
            client.DefaultRequestHeaders.Add(Constants.Settings.Roles, Constants.Settings.CartRoles);


            try
            {
                var cart = new Cart();

                var response = client.GetAsync(url).Result;

                if (response != null)
                {
                    var task = response.Content.ReadAsStreamAsync().ContinueWith(t =>
                    {
                        var stream = t.Result;
                        using (var reader = new StreamReader(stream))
                        {
                            var responseValue = reader.ReadToEnd();
                            cart = JsonConvert.DeserializeObject <Cart>(responseValue);
                        }
                    });

                    task.Wait();
                }

                client.Dispose();
                return(cart);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                client.Dispose();
                return(null);
            }
        }
예제 #2
0
        public async Task <List <Order> > Resolve(CommerceContext commerceContext)
        {
            if (commerceContext == null || !commerceContext.CurrentUserIsRegistered())
            {
                return(null);
            }

            string listName = string.Format(CultureInfo.InvariantCulture,
                                            commerceContext.GetPolicy <KnownOrderListsPolicy>().CustomerOrders,
                                            commerceContext.CurrentCustomerId());

            CommerceList <Order> result =
                await findEntitiesInListCommand.Process <Order>(commerceContext, listName, 0, int.MaxValue);

            return(result?.Items);
        }