-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiGatewayCaller.cs
85 lines (77 loc) · 2.91 KB
/
ApiGatewayCaller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Diagnostics.Eventing.Reader;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using IndoorCO2App_Android;
using Newtonsoft.Json;
namespace IndoorCO2App_Multiplatform
{
public class ApiGatewayCaller
{
private static HttpClient client = new HttpClient
{
Timeout = TimeSpan.FromSeconds(20)
};
// Modify sendJsonToApiGateway to accept callback
public static async Task SendJsonToApiGateway(string json, SubmissionMode submissionMode)
{
var successState = string.Empty;
try
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response;
if (submissionMode == SubmissionMode.Building)
{
response = await client.PostAsync("https://wzugdkxj15.execute-api.eu-central-1.amazonaws.com/Standard/CO2", content);
}
else if (submissionMode == SubmissionMode.BuildingManual)
{
response = await client.PostAsync("https://40zfjhm5tg.execute-api.eu-central-1.amazonaws.com/SendManualCO2Data", content);
}
else if (submissionMode == SubmissionMode.Transit)
{
response = await client.PostAsync("https://sokwze8jj1.execute-api.eu-central-1.amazonaws.com/SendTransitCO2DataToSQS", content);
}
else return;
if (response.IsSuccessStatusCode)
{
successState = "success";
}
else
{
successState = "failure";
}
}
catch (HttpRequestException e)
{
Logger.circularBuffer.Add($"Request error: {e.Message}");
Console.WriteLine($"Request error: {e.Message}");
successState = "failure";
}
catch (TaskCanceledException e)
{
Logger.circularBuffer.Add($"Request error: {e.Message}");
Console.WriteLine($"Request timeout: {e.Message}");
successState = "timeout";
}
catch (Exception e)
{
Logger.circularBuffer.Add($"Request error: {e.Message}");
successState = "failure";
}
if (successState == "success")
{
MainPage.MainPageSingleton.OnTransmissionSuccess("success");
}
else if (successState == "failure")
{
MainPage.MainPageSingleton.OnTransmissionFailed("Transmission failed. Try again!!");
}
else
{
MainPage.MainPageSingleton.OnTransmissionFailed("No Response from server. Try again!");
}
}
}
}