Esempio n. 1
0
        public Message GetMessage(IngredientsToUpsert entity)
        {
            var json     = JsonConvert.SerializeObject(entity);
            var jsonByte = Encoding.UTF8.GetBytes(json);

            return(new Message
            {
                Body = jsonByte,
                MessageId = entity.ProductId.ToString()
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Faz a leitura dos ingredientes na imagem e
        /// envia para a fila
        /// </summary>
        /// <param name="pathImage"></param>
        public void ReadImageVisonService(string pathImage)
        {
            OcrResults results;

            var visionServiceClient = new VisionServiceClient(_configuration["API:VisionAPIKey"], _configuration["API:VisionUrl"]);

            var imageByte = DownloadImageFromHttp(pathImage);
            var memStream = new MemoryStream(imageByte);

            memStream.Seek(0, SeekOrigin.Begin);

            results = visionServiceClient.RecognizeTextAsync(memStream).Result;

            var lines     = results.Regions.SelectMany(region => region.Lines);
            var words     = lines.SelectMany(line => line.Words);
            var wordsText = words.Select(word => word.Text.ToUpper());

            var wordsJoint =
                string.Join(' ', wordsText).Replace(AndWithSpace, CommaWithSpace, StringComparison.InvariantCultureIgnoreCase);

            foreach (var item in blacklist)
            {
                wordsJoint = wordsJoint.Replace(item, ",",
                                                StringComparison.InvariantCultureIgnoreCase);
            }

            var wordsSplitByComma = wordsJoint.Split(',').ToList();

            var request = new IngredientsToUpsert
            {
                ProductId   = Guid.NewGuid(),
                Ingredients = new List <string>()
            };

            wordsSplitByComma.Distinct().ToList()
            .ForEach(wordText =>
            {
                var text = wordText.Trim();
                if (!String.IsNullOrWhiteSpace(text))
                {
                    request.Ingredients.Add(text);
                }
            });

            _lababelLoaderChangedService
            .AddToMessageList(request);

            _lababelLoaderChangedService.SendMessagesAsync();
        }
Esempio n. 3
0
        public async Task <bool> CreateIngredients(IngredientsToUpsert request)
        {
            var urlApi = config["API:IngredientsUrl"];

            using (HttpClient client = new HttpClient())
            {
                var stringData  = JsonConvert.SerializeObject(request);
                var contentData = new StringContent(stringData, Encoding.UTF8, "application/json");

                HttpResponseMessage response = client.PostAsync(urlApi, contentData).Result;
                string responseData          = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <bool>(responseData));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Get Products filtred by restricion of users.
        /// </summary>
        /// <param name="storeId">StoreID</param>
        /// <param name="restrictions">Restrictions of User</param>
        /// <returns>Products and ingredients</returns>
        public async Task <List <IngredientsToUpsert> > GetProductsByRestrictions(Guid storeId, string restrictions)
        {
            CancellationToken cancellationToken = new CancellationToken();
            var client = new HttpClient();
            var result = string.Empty;
            var uri    = new Uri(_appSettings.IngredientsApiSettings.Url);

            try
            {
                var policy = Policy.Handle <HttpRequestException>().WaitAndRetry(new[] {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(30),
                    TimeSpan.FromSeconds(60)
                }, (exception, timeSpan) => {
                    string mensagem = $"API ingredients is not responding. Error: {exception.Message}";
                    _logService.SendMessagesAsync(mensagem);
                    Debug.Write(mensagem);
                });

                var response = await policy.Execute(ct => client.GetAsync(uri + $"api/Products/byrestrictions/{storeId}/{restrictions}", ct), cancellationToken);

                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception ex)
            {
                //Necessary mock because this APP it's not On-Line.
                _logService.SendMessagesAsync("Serviço http://geekburgergrupotres.azurewebsites.net/ Fora do AR.");
                List <Ingredients.Contract.Response.IngredientsToUpsert> ingredientsToUpserts = new List <IngredientsToUpsert>();
                IngredientsToUpsert ingredient     = new IngredientsToUpsert();
                List <string>       lstIngredients = new List <string>();
                lstIngredients.Add("soy");
                lstIngredients.Add("gluten");
                ingredient.Ingredients = lstIngredients;
                ingredient.ProductId   = Guid.Parse("0d61770a-9c75-4922-a1bd-b1b853a7e04c");
                ingredientsToUpserts.Add(ingredient);
                return(ingredientsToUpserts);
            }

            return(JsonConvert.DeserializeObject <List <Ingredients.Contract.Response.IngredientsToUpsert> >(result));
        }
Esempio n. 5
0
 public void AddToMessageList(IngredientsToUpsert ingredient)
 {
     _messages.Add(GetMessage(ingredient));
 }