-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
169 lines (130 loc) · 6.43 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
using CodecExample.Client;
using CodecExample.Common;
using CodecExample.Common.Codecs.Custom;
using CodecExample.Common.Codecs.Serialized;
using Flurl.Http;
public class Program
{
/// <summary>
/// Uses the WeatherForecastClient to make various HTTP requests to the CodecExample server.
/// </summary>
public static async Task Main(string[] args)
{
// -------------------
// Setup
// -------------------
LogHeading("Initializing Transcoder and client");
var transcoder = GetTranscoder();
var client = new WeatherForecastClient(transcoder);
// -------------------
// POST requests
// -------------------
LogHeading("Posting weather forecast - Send CUSTOM V1, Accept CUSTOM V2");
var forecastToSend = new WeatherForecast() { Date = DateTime.UtcNow, Summary = "Raining json!", TemperatureC = 25 };
LogForecasts(forecastToSend, "Sending Forecast:");
var forecastPostReply = await client.PostForecastV1Custom(forecastToSend);
LogForecasts(forecastPostReply);
LogHeading("Posting weather forecasts - Send CUSTOM V1, Accept CUSTOM V2");
var forecastsToSend = new WeatherForecast[] {
new WeatherForecast() { Date = DateTime.UtcNow.AddDays(1), Summary = "Raining cats!", TemperatureC = 25 },
new WeatherForecast() { Date = DateTime.UtcNow.AddDays(2), Summary = "Raining dogs!", TemperatureC = 30 },
new WeatherForecast() { Date = DateTime.UtcNow.AddDays(3), Summary = "Raining pennies from heaven!", TemperatureC = 35 }
};
LogForecasts(forecastsToSend, "Sending Forecasts:");
var forecastsPostReply = await client.PostForecastV1Custom(forecastsToSend);
LogForecasts(forecastsPostReply);
LogHeading("Posting weather forecast - Send PROTOBUF V1, Accept PROTOBUF V2");
var forecastToSendPB = new WeatherForecast() { Date = DateTime.UtcNow.AddDays(4), Summary = "Raining buffers!", TemperatureC = 10 };
LogForecasts(forecastToSendPB, "Sending Forecast:");
var forecastPostReplyPB = await client.PostForecastV1Protobuf(forecastToSendPB);
LogForecasts(forecastPostReplyPB);
// -------------------
// GET requests
// -------------------
LogHeading("Fetching weather forecasts - CUSTOM V1");
var forecasts1C = await client.GetForecastsV1Custom();
LogForecasts(forecasts1C);
LogHeading("Fetching weather forecasts - CUSTOM V1 (Over 30C)");
var forecasts1CHot = await client.GetForecastsV1Custom(30);
LogForecasts(forecasts1CHot);
LogHeading("Fetching weather forecasts - CUSTOM V2");
var forecasts2C = await client.GetForecastsV2Custom();
LogForecasts(forecasts2C);
LogHeading("Fetching weather forecasts - SERIALIZED V1");
var forecasts1S = await client.GetForecastsV1Serialized();
LogForecasts(forecasts1S);
LogHeading("Fetching weather forecasts - CONTENT NEGOTIATION");
var forecasts = await client.GetForecasts();
LogForecasts(forecasts);
LogHeading("Fetching weather forecasts - PROTOBUF V1");
var forecast1PB = await client.GetForecastV1Protobuf(2);
LogForecasts(forecast1PB);
// -------------------
// Demonstrate Errors
// -------------------
LogHeading("Fetching with bad accept header");
try
{
var unacceptable = await client.GetUnacceptableMediaType();
}
catch (Exception ex)
{
Console.WriteLine($"Returned exception: {ex}");
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void LogHeading(string value)
{
Console.WriteLine("\r\n" + new String('-', 80));
Console.WriteLine(value);
}
private static void LogForecasts(IEnumerable<WeatherForecast> forecasts, string description = "Received Forecasts:")
{
Console.WriteLine(description);
foreach (var forecast in forecasts)
{
Console.WriteLine($"\t{forecast.Date} \tTemp: {forecast.TemperatureC} C \tSummary: {forecast.Summary}.");
}
}
private static void LogForecasts(WeatherForecast forecast, string description = "Received Forecast:")
{
Console.WriteLine(description);
Console.WriteLine($"\t{forecast.Date} \tTemp: {forecast.TemperatureC} C \tSummary: {forecast.Summary}.");
}
/// <summary>
/// Shows a typical client side setup of the transcoder.
///
/// Note:
/// 1. We use a single instance of the transcoder
/// 2. It has all known/supported codecs.
///
/// </summary>
private static Transcoder GetTranscoder()
{
var transcoder = new Transcoder();
// V1 Custom Codecs
transcoder.Encoders.Add(new WeatherForecastCustomV1Encoder());
transcoder.Encoders.Add(new WeatherForecastCollectionCustomV1Encoder());
transcoder.Decoders.Add(new WeatherForecastCustomV1Decoder());
transcoder.Decoders.Add(new WeatherForecastCollectionCustomV1Decoder());
// V2 Custom Codecs
transcoder.Encoders.Add(new WeatherForecastCustomV2Encoder());
transcoder.Encoders.Add(new WeatherForecastCollectionCustomV2Encoder());
transcoder.Decoders.Add(new WeatherForecastCustomV2Decoder());
transcoder.Decoders.Add(new WeatherForecastCollectionCustomV2Decoder());
// V1 Serilization-based Codecs
transcoder.Encoders.Add(new WeatherForecastSerializedV1Encoder());
transcoder.Encoders.Add(new WeatherForecastCollectionSerializedV1Encoder());
transcoder.Decoders.Add(new WeatherForecastSerializedV1Decoder());
transcoder.Decoders.Add(new WeatherForecastCollectionSerializedV1Decoder());
// V1 Protobuf Codecs
transcoder.Encoders.Add(new CodecExample.Common.Protobuf.Codecs.WeatherForecastV1Encoder());
transcoder.Decoders.Add(new CodecExample.Common.Protobuf.Codecs.WeatherForecastV1Decoder());
transcoder.Encoders.Add(new CodecExample.Common.Protobuf.Codecs.WeatherForecastCollectionV1Encoder());
transcoder.Decoders.Add(new CodecExample.Common.Protobuf.Codecs.WeatherForecastCollectionV1Decoder());
// Other
transcoder.Encoders.Add(new ValidationProblemsEncoder());
return transcoder;
}
}