From 0097a634eb6739b903937891ae4a0b5eb1cae00b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=80=D0=BD=D0=B0=D0=B2=D1=81=D0=BA=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5=D0=B2=D0=B8=D1=87?= Date: Mon, 30 Oct 2023 23:44:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20async?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Anyor.Tests/AmoToSheetFunctionTest.cs | 4 +-- .../Controllers/PreachyBudgetController.cs | 32 +++++++++---------- Web.Api/Services/AmoService.cs | 4 +-- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Anyor.Tests/AmoToSheetFunctionTest.cs b/Anyor.Tests/AmoToSheetFunctionTest.cs index 2485dc1..05c8b02 100644 --- a/Anyor.Tests/AmoToSheetFunctionTest.cs +++ b/Anyor.Tests/AmoToSheetFunctionTest.cs @@ -36,9 +36,9 @@ public void AllFunctionTest() } [Test] - public void GetLeadTest() + public async Task GetLeadTest() { - var lead = _amoService.GetAmoLead(9997167); + var lead = await _amoService.GetAmoLead(9997167); Assert.That(lead.Embedded.Contacts.First().Id, Is.EqualTo(13180131)); } diff --git a/Web.Api/Controllers/PreachyBudgetController.cs b/Web.Api/Controllers/PreachyBudgetController.cs index 9ecf5db..cbbe9fe 100644 --- a/Web.Api/Controllers/PreachyBudgetController.cs +++ b/Web.Api/Controllers/PreachyBudgetController.cs @@ -31,31 +31,29 @@ public async Task AmoHookHandle() return await AmoHookHandle(request); } + [HttpPost("AmoHookHandle/private")] public async Task AmoHookHandle(string request) { var hook = ParseHook(request); Console.WriteLine("LEAD_ID: " + hook.LeadId); - Task.Run(() => - { - var lead = _amoService.GetAmoLead(hook.LeadId); - var contact = _amoService.GetAmoContact(lead.Embedded.Contacts.First().Id); - Console.WriteLine("CONTACT_NAME: " + contact.Name); + var lead = await _amoService.GetAmoLead(hook.LeadId); + var contact = _amoService.GetAmoContact(lead.Embedded.Contacts.First().Id); + Console.WriteLine("CONTACT_NAME: " + contact.Name); - var amount = lead.CustomFieldsValues - .FirstOrDefault(f => f.FieldName == "tinkoff_amount")?.Values - .FirstOrDefault()?.Value; + var amount = lead.CustomFieldsValues + .FirstOrDefault(f => f.FieldName == "tinkoff_amount")?.Values + .FirstOrDefault()?.Value; - var sheetDonationService = new SheetDonationsAddService(_envConfig.GoogleCredsJson); - sheetDonationService.AddNewRow(new Donation - { - AmoLeadId = lead.Id, - Amount = amount, - Date = DateTime.Now.AddHours(3), - ContactName = contact.Name - }); - }); + var sheetDonationService = new SheetDonationsAddService(_envConfig.GoogleCredsJson); + sheetDonationService.AddNewRow(new Donation + { + AmoLeadId = lead.Id, + Amount = amount, + Date = DateTime.Now.AddHours(3), + ContactName = contact.Name + }); return Ok(); } diff --git a/Web.Api/Services/AmoService.cs b/Web.Api/Services/AmoService.cs index 6bacceb..80e8a28 100644 --- a/Web.Api/Services/AmoService.cs +++ b/Web.Api/Services/AmoService.cs @@ -15,7 +15,7 @@ public AmoService(string amoAccessToken, HttpClient? httpClient = null) _httpClient = httpClient ?? new HttpClient(); } - public AmoLead GetAmoLead(long leadId) + public async Task GetAmoLead(long leadId) { var url = $"https://anyor.amocrm.ru/api/v4/leads/{leadId}?with=contacts"; @@ -23,7 +23,7 @@ public AmoLead GetAmoLead(long leadId) httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _amoAccessToken); - using var response = httpClient.GetAsync(url).Result; + var response = await httpClient.GetAsync(url); if (!response.IsSuccessStatusCode) throw new Exception("Error while getting Amo lead");