Skip to content

Commit

Permalink
Поправил async
Browse files Browse the repository at this point in the history
  • Loading branch information
Варнавский Владимир Николаевич committed Oct 30, 2023
1 parent 588cfa2 commit 0097a63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Anyor.Tests/AmoToSheetFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
32 changes: 15 additions & 17 deletions Web.Api/Controllers/PreachyBudgetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,29 @@ public async Task<IActionResult> AmoHookHandle()
return await AmoHookHandle(request);
}

[HttpPost("AmoHookHandle/private")]
public async Task<IActionResult> 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();
}
Expand Down
4 changes: 2 additions & 2 deletions Web.Api/Services/AmoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public AmoService(string amoAccessToken, HttpClient? httpClient = null)
_httpClient = httpClient ?? new HttpClient();
}

public AmoLead GetAmoLead(long leadId)
public async Task<AmoLead> GetAmoLead(long leadId)
{
var url = $"https://anyor.amocrm.ru/api/v4/leads/{leadId}?with=contacts";

using var httpClient = new HttpClient();
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");

Expand Down

0 comments on commit 0097a63

Please sign in to comment.