forked from signalfx/tracing-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
184 lines (163 loc) · 6.47 KB
/
Program.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ClientExample
{
public class Program
{
private static string exampleUrl = "http://aspnetcore:5000/api/items";
private static Dictionary<string, Item> itemStore = new Dictionary<string, Item>();
private static List<Item> itemsToUse = new List<Item>()
{
new Item()
{
Name = "Heatron 10000",
Description = "Ceramic space heater.",
Maker = "DL",
Price = 100.00m,
Url = "http://example.com/heatron/10000"
},
new Item()
{
Name = "Wool Blanket",
Description = "Navajo inspired.",
Maker = "PT",
Price = 300.00m,
Url = "http://example.com/wool/blanket"
},
new Item()
{
Name = "Slippers",
Description = "Handmade from wool and leather.",
Maker = "FJ",
Price = 20.00m,
Url = "http://example.com/slippers"
},
new Item()
{
Name = "Clay Mug",
Description = "Large drinking vessel.",
Maker = "FM",
Price = 7.00m,
Url = "http://example.com/clay/mug"
},
};
public static async Task Main(string[] args)
{
await CreateItems(itemsToUse);
await FetchItems();
await UpdateItems();
await DeleteItems();
}
public static async Task CreateItems(List<Item> items)
{
foreach (var item in items)
{
var created = await CreateItem(item);
itemStore[created.Id] = created;
}
}
public static async Task FetchItems()
{
foreach (var item in itemStore)
{
await FetchItem(item.Value);
}
}
public static async Task UpdateItems()
{
foreach (var item in itemStore)
{
await UpdateItem(item.Value);
}
}
public static async Task DeleteItems()
{
foreach (var item in itemStore)
{
var deleted = await DeleteItem(item.Value);
if (deleted)
{
itemStore.Remove(item.Key);
}
}
}
public static async Task<Item> CreateItem(Item item)
{
Console.WriteLine($"Creating {item.Name}: {item.Maker} - {item.Description}");
var jsonObject = JsonConvert.SerializeObject(item);
using (var client = new HttpClient())
{
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
using (var responseMessage = await client.PostAsync($"{exampleUrl}", content))
{
var responseContent = await responseMessage.Content.ReadAsStringAsync();
var responseStatus = responseMessage.StatusCode;
Console.WriteLine($"CreateItem response - {responseStatus}: {responseContent}");
return JsonConvert.DeserializeObject<Item>(responseContent);
}
}
}
public static async Task<Item> FetchItem(Item item)
{
Console.WriteLine($"Fetching {item.Id}");
Item fetchedItem = null;
using (var client = new HttpClient())
{
using (var responseMessage = await client.GetAsync($"{exampleUrl}/{item.Id}"))
{
var responseContent = await responseMessage.Content.ReadAsStringAsync();
var responseStatus = responseMessage.StatusCode;
Console.WriteLine($"FetchItem response - {responseStatus}: {responseContent}");
fetchedItem = JsonConvert.DeserializeObject<Item>(responseContent);
}
}
return fetchedItem;
}
public static async Task<bool> UpdateItem(Item item)
{
Console.WriteLine($"Updating {item.Name}: {item.Maker} - {item.Description}");
var updatedDescription = $"updated: {item.Description}";
item.Description = updatedDescription;
var jsonObject = JsonConvert.SerializeObject(item);
using (var client = new HttpClient())
{
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
using (var responseMessage = await client.PutAsync($"{exampleUrl}/{item.Id}", content))
{
var responseContent = await responseMessage.Content.ReadAsStringAsync();
var responseStatus = responseMessage.StatusCode;
Console.WriteLine($"UpdateItem response - {responseStatus}: {responseContent}");
return responseStatus == HttpStatusCode.NoContent;
}
}
}
public static async Task<bool> DeleteItem(Item item)
{
Console.WriteLine($"Deleting {item.Name}: {item.Maker} - {item.Description}");
using (var client = new HttpClient())
{
using (var responseMessage = await client.DeleteAsync($"{exampleUrl}/{item.Id}"))
{
var responseContent = await responseMessage.Content.ReadAsStringAsync();
var responseStatus = responseMessage.StatusCode;
Console.WriteLine($"DeleteItem response - {responseStatus}: {responseContent}");
return responseStatus == HttpStatusCode.NoContent;
}
}
}
}
public class Item
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Maker { get; set; }
public decimal Price { get; set; }
public string Url { get; set; }
}
}