Esempio n. 1
0
        private static List <ProcessedProductDelta> GenerateAccountPriceDeltas(IEnumerable <ProcessedAccountPrice> accountPrices, string appKey)
        {
            var validAccountPriceDeltas = new List <ProcessedProductDelta>();
            var deltasValid             = false;
            var productsPerDelta        = 5000;

            while (!deltasValid)
            {
                validAccountPriceDeltas.Clear();
                var accountPricePartitions = Partition(accountPrices, productsPerDelta);

                deltasValid = accountPricePartitions.All((partition) =>
                {
                    var feed           = new ProcessedProductDelta(appKey);
                    feed.AccountPrices = partition.ToArray();
                    validAccountPriceDeltas.Add(feed);
                    var deltaTotal = JSONSerialization.GetByteSizeOfObject(feed);
                    return(deltaTotal < _maxDeltaSize);
                });

                if (!deltasValid)
                {
                    productsPerDelta = productsPerDelta / 5;
                    if (productsPerDelta < 8)
                    {
                        throw new Exception("Individual account prices are too large to fit into a delta to send to PureClarity. Please reduce the amount of data been sent per product.");
                    }
                }
            }

            return(validAccountPriceDeltas);
        }
Esempio n. 2
0
        private static List <ProcessedProductDelta> GenerateDeletedProductDeltas(IEnumerable <DeletedProductSku> deletedProducts, string appKey)
        {
            var validDeletedProductDeltas = new List <ProcessedProductDelta>();
            var deltasValid      = false;
            var productsPerDelta = 5000;

            while (!deltasValid)
            {
                validDeletedProductDeltas.Clear();
                var deletedProductPartitions = Partition(deletedProducts, productsPerDelta);

                deltasValid = deletedProductPartitions.All((partition) =>
                {
                    var feed            = new ProcessedProductDelta(appKey);
                    feed.DeleteProducts = partition.Select((deletedProduct) => deletedProduct.Sku).ToArray();
                    validDeletedProductDeltas.Add(feed);
                    var deltaTotal = JSONSerialization.GetByteSizeOfObject(feed);
                    return(deltaTotal < _maxDeltaSize);
                });

                if (!deltasValid)
                {
                    productsPerDelta = productsPerDelta / 5;
                    if (productsPerDelta < 8)
                    {
                        throw new Exception("Individual deleted product skus are too large to fit into a delta to send to PureClarity. You may have the wrong data in your sku");
                    }
                }
            }

            return(validDeletedProductDeltas);
        }
        public static async Task <T> Get <T>(string getUrl, string queryString)
        {
            using (var httpClient = new HttpClient())
            {
                var result = default(T);

                httpClient.Timeout = new TimeSpan(0, 0, 150);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = await httpClient.GetAsync(getUrl + queryString);

                if (response.IsSuccessStatusCode)
                {
                    result = await JSONSerialization.DeserializeJSONFromHttpResponse <T>(response);
                }
                else
                {
                    throw new Exception($"Request failure for object {typeof(T)}: GET Status {response.StatusCode} returned for {getUrl} with query string {queryString}");
                }

                return(result);
            }
        }
        public static async Task <T> Post <T>(string bodyToPost, string postUrl)
        {
            using (var httpClient = new HttpClient())
            {
                var result = default(T);

                httpClient.Timeout = new TimeSpan(0, 0, 150);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var content  = new StringContent(bodyToPost, Encoding.UTF8, "application/json");
                var response = await httpClient.PostAsync(postUrl, content);

                if (response.IsSuccessStatusCode)
                {
                    result = await JSONSerialization.DeserializeJSONFromHttpResponse <T>(response);
                }
                else
                {
                    throw new Exception($"Request failure for object {typeof(T)}: POST Status {response.StatusCode} with body {await response.Content.ReadAsStringAsync()} returned for {postUrl} with payload {bodyToPost}");
                }

                return(result);
            }
        }