forked from MarcelRaschke/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueueProcessor.cs
74 lines (57 loc) · 1.99 KB
/
QueueProcessor.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
using PostSharp.Patterns.Diagnostics;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ClientExample
{
public class QueueProcessor
{
private static readonly LogSource logger = LogSource.Get();
private static readonly HttpClient http = new HttpClient();
static QueueProcessor()
{
}
public static async Task ProcessQueue(string queuePath)
{
await ProcessItem(new QueueItem(1, Verb.Create, "grapefruit"));
await ProcessItem(new QueueItem(2, Verb.Create, "pear"));
await ProcessItem(new QueueItem(3, Verb.Create, "grape"));
await ProcessItem(new QueueItem(1, Verb.Get));
await ProcessItem(new QueueItem(2, Verb.Get));
await ProcessItem(new QueueItem(3, Verb.Get));
await ProcessItem(new QueueItem(3, Verb.Create, "grapefruit"));
await ProcessItem(new QueueItem(3, Verb.Delete, "grapefruit"));
await ProcessItem(new QueueItem(3, Verb.Get));
}
private static async Task ProcessItem(QueueItem item)
{
try
{
var url = $"http://localhost:5005/api/values/{item.Id}";
var stringContent = item.Value == null ? null : new StringContent("\"" + item.Value + "\"", Encoding.UTF8, "application/json");
HttpResponseMessage response;
switch (item.Verb)
{
case Verb.Get:
response = await http.GetAsync(url);
break;
case Verb.Create:
response = await http.PostAsync(url, stringContent);
break;
case Verb.AddOrUpdate:
response = await http.PutAsync(url, stringContent);
break;
default:
throw new NotImplementedException();
}
response.EnsureSuccessStatusCode();
var responseValue = await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
logger.Warning.Write(FormattedMessageBuilder.Formatted("Ignoring exception and continuing."), e);
}
}
}
}