-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex_Arrange-Act-Assert.cs
152 lines (126 loc) · 5.32 KB
/
Complex_Arrange-Act-Assert.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Refactor with the Arrange, Act, Assert pattern
//The following is code used in an Azure Function. It accepts an 'Event',
//transforms the data and environmental variables into an HTTP message and
//sends the message.
//Use the 'Arrange, Act, Assert' pattern throughout your code.
//The 'Arrange' is used to declare all your variable sets
//The 'Act' is how you combine the variables
//The 'Assert' is where we return or execute some procedure
public class SendMessages {
public static async Task Run(EventData eventHubMessage, TraceWriter log)
{
try
{
string body = Encoding.UTF8.GetString(eventHubMessage.GetBytes());
if (body == "")
{
throw new Exception("No body in the Event.");
}
AssetEvent evnt = JsonConvert.DeserializeObject<AssetEvent>(body);
await LogEvent(evnt, log).ConfigureAwait(false);
await SendEvent(evnt, log, eventHubMessage).ConfigureAwait(false);
}
catch(Exception ex)
{
await LogException(ex, log).ConfigureAwait(false);
log.Info(ex.Message);
throw;
}
}
public static async Task SendEvent(AssetEvent evnt,TraceWriter log, EventData eventHubMessage)
{
var _requestUri = GetEnvironmentVariable("RequestUri");
object appToken;
if (false == eventHubMessage.Properties.TryGetValue("AppToken", out appToken))
{
throw new Exception("No app token in the Event.");
}
string appTokenString = (string)appToken;
string subscriberKey = GetEnvironmentVariable($"AppToken:{appTokenString}:{evnt.AccountId}");
var client = GetHttpClient(subscriberKey);
var json = JsonConvert.SerializeObject(evnt);
var stringContent = new StringContent(json,
Encoding.UTF8,
"application/json");
var response = await client.PostAsync(_gpsServiceRequestUri, stringContent).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
await LogError($"Error ocurred while calling endpoint: {response.ToString()}",evnt,log).ConfigureAwait(false);
}
}
private static HttpClient _client;
public static HttpClient GetHttpClient(string subscriberKey)
{
try
{
if (_client != null) return _client;
var _serviceEndpoint = GetEnvironmentVariable("serviceEndpoint");
var _subscriberKeyHeaderName = GetEnvironmentVariable("SubscriberKeyHeaderName");
_client = new HttpClient();
_client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
_client.DefaultRequestHeaders.Add("Keep-Alive", "timeout=600");
_client.BaseAddress = new Uri(_serviceEndpoint);
_client.DefaultRequestHeaders.Add(_subscriberKeyHeaderName, subscriberKey);
return _client;
}
catch (Exception)
{
throw;
}
}
}
//Potential Solution
public class SendMessagesRefactored {
public static async Task Run(EventData eventHubMessage, TraceWriter log)
{
string correlationId = null;
try
{
string body = Encoding.UTF8.GetString(eventHubMessage.GetBytes());
if(body == "")
{
throw new Exception("No body in the Event.");
}
AssetEvent evnt = JsonConvert.DeserializeObject<AssetEvent>(body);
object corrId = null;
eventHubMessage.Properties.TryGetValue("CorrelationId", out corrId);
correlationId = (string) corrId;
await LogEvent(evnt, correlationId, log).ConfigureAwait(false);
object appToken;
if (!eventHubMessage.Properties.TryGetValue("AppToken", out appToken)) {
throw new Exception("No app token in the Event.");
}
string subscriberKey = GetEnvironmentVariable($"AppToken:{(string)appToken}:{evnt.AccountId}");
var _subscriberKeyHeaderName = GetEnvironmentVariable("SubscriberKeyHeaderName");
var _requestUri = GetEnvironmentVariable("RequestUri");
var _serviceEndpoint = GetEnvironmentVariable("Endpoint");
var client = GetHttpClient(_serviceEndpoint);
var json = JsonConvert.SerializeObject(evnt);
var content = new StringContent(json, Encoding.UTF8, "application/json");
content.Headers.Add(_subscriberKeyHeaderName, subscriberKey);
content.Headers.Add("CorrelationId", correlationId);
var response = await client.PostAsync(_requestUri, content).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
await LogError($"Error ocurred while calling endpoint: {response.ToString()}", evnt, correlationId, log).ConfigureAwait(false);
}
log.Info("Function Completed");
}
catch(Exception ex)
{
await LogException(ex, correlationId, log).ConfigureAwait(false);
log.Info("Run Failed. Exception: " + ex.Message);
throw;
}
}
public static HttpClient _HttpClient;
public static HttpClient GetHttpClient(string serviceEndpoint) {
if (_HttpClient == null) {
_HttpClient = new HttpClient();
_HttpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
_HttpClient.BaseAddress = new Uri(serviceEndpoint);
_HttpClient.Timeout.Add(new TimeSpan(0,10,0));
}
return _HttpClient;
}
}