-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathOpenStackNet.cs
More file actions
302 lines (265 loc) · 11.5 KB
/
Copy pathOpenStackNet.cs
File metadata and controls
302 lines (265 loc) · 11.5 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Extensions;
using System.Net.Http.Headers;
using Flurl.Http;
using Flurl.Http.Configuration;
using Newtonsoft.Json;
using OpenStack.Authentication;
using OpenStack.Serialization;
namespace OpenStack
{
/// <summary>
/// A static container for global configuration settings affecting OpenStack.NET behavior.
/// </summary>
/// <threadsafety static="true" instance="false"/>
public static class OpenStackNet
{
/// <summary>
/// Global configuration which affects OpenStack.NET's behavior.
/// <para>Customize using the <see cref="Configuring"/> event.</para>
/// </summary>
public static OpenStackNetConfigurationOptions Configuration
{
get
{
if (!_isConfigured)
Configure();
return _config;
}
}
private static OpenStackNetConfigurationOptions _config;
private static readonly object ConfigureLock = new object();
private static bool _isConfigured;
/// <summary>
/// Occurs when initializing the global configuration for OpenStack.NET.
/// </summary>
public static event Action<OpenStackNetConfigurationOptions> Configuring;
/// <summary>
/// <para>DEPRECATED, use the <see cref="Configuring"/> event instead.</para>
/// <para>Provides thread-safe accesss to OpenStack.NET's global configuration options.</para>
/// <para>Can only be called once at application start-up, before instantiating any OpenStack.NET objects.</para>
/// </summary>
/// <param name="configureFlurl">Addtional configuration of the OpenStack.NET Flurl client settings <seealso cref="Flurl.Http.FlurlHttp.Configure" />.</param>
/// <param name="configure">Additional configuration of OpenStack.NET's global settings.</param>
[Obsolete("This will be removed in v2.0. Use the OpenStackNet.Configuring event instead.")]
public static void Configure(Action<FlurlHttpSettings> configureFlurl = null, Action<OpenStackNetConfigurationOptions> configure = null)
{
lock (ConfigureLock)
{
if (_isConfigured)
{
// Check if a user is attempting to apply custom configuration after the default config has been applied
if(configureFlurl != null || configure != null)
Trace.TraceError("Ignoring additional call to OpenStackNet.Configure. It can only be called once at application start-up, before instantiating any OpenStack.NET objects.");
return;
}
// Give the application an opportunity to tweak the default config
_config = OpenStackNetConfigurationOptions.Create();
Configuring?.Invoke(_config);
// Apply legacy custom configuration, removed in 2.0 as it's replaced by the Configuring event
configureFlurl?.Invoke(_config.FlurlHttpSettings);
// Finish configuration and lock it
_config.CompleteInitialization();
_isConfigured = true;
}
}
/// <summary>
/// <par>Resets all configuration (OpenStack.NET, Flurl and Json.NET).</par>
/// <para>After this is called, you must re-register any <see cref="Configuring"/> event handlers.</para>
/// </summary>
public static void ResetDefaults()
{
lock (ConfigureLock)
{
_config = null;
Configuring = null;
_isConfigured = false;
}
}
/// <summary>
/// Deserializes an object from a json string representation.
/// </summary>
/// <typeparam name="T">The object type.</typeparam>
/// <param name="json">The json string.</param>
public static T Deserialize<T>(string json)
{
return Configuration.FlurlHttpSettings.JsonSerializer.Deserialize<T>(json);
}
/// <summary>
/// Serializes an object to a json string representation
/// </summary>
/// <param name="obj">The object.</param>
/// <returns>The json string representation of the object.</returns>
public static string Serialize(object obj)
{
return Configuration.FlurlHttpSettings.JsonSerializer.Serialize(obj);
}
/// <summary>
/// Provides global point for programmatically configuraing tracing
/// </summary>
public static class Tracing
{
/// <summary>
/// Trace source for all HTTP requests. Default level is Error.
/// <para>
/// In your app or web.config the trace soruce name is "Flurl.Http".
/// </para>
/// </summary>
public static readonly TraceSource Http = new TraceSource("Flurl.Http", SourceLevels.Error);
/// <summary>
/// Traces a failed HTTP request
/// </summary>
/// <param name="httpCall">The Flurl HTTP call instance, containing information about the request and response.</param>
public static void TraceFailedHttpCall(HttpCall httpCall)
{
Http.TraceData(TraceEventType.Error, 0, SerializeHttpCall(httpCall));
Http.Flush();
}
/// <summary>
/// Traces an HTTP request
/// </summary>
/// <param name="httpCall">The Flurl HTTP call instance, containing information about the request and response.</param>
public static void TraceHttpCall(HttpCall httpCall)
{
Http.TraceData(TraceEventType.Information, 0, SerializeHttpCall(httpCall));
}
private static string SerializeHttpCall(HttpCall httpCall)
{
var settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
return JsonConvert.SerializeObject(httpCall, Formatting.Indented, settings);
}
}
}
/// <summary>
/// A readonly set of properties that affect OpenStack.NET's behavior.
/// <para>To customize, register an event handler for <see cref="OpenStackNet.Configuring"/>.</para>
/// </summary>
public class OpenStackNetConfigurationOptions
{
private bool _isInitialized;
private readonly FlurlHttpSettings _flurlHttpSettings;
private readonly JsonSerializerSettings _jsonSerializerSettings;
private readonly List<ProductInfoHeaderValue> _userAgents;
/// <summary/>
protected OpenStackNetConfigurationOptions()
{
_flurlHttpSettings = new FlurlHttpSettings();
_jsonSerializerSettings = new JsonSerializerSettings();
_userAgents = new List<ProductInfoHeaderValue>();
}
/// <summary />
public static event Action<CreateEvent> Creating;
/// <summary />
internal static OpenStackNetConfigurationOptions Create()
{
var createEvent = new CreateEvent();
Creating?.Invoke(createEvent);
return createEvent.Result;
}
/// <summary />
public void CompleteInitialization()
{
OnCompleteInitialization();
ApplyDefaults();
_isInitialized = true;
}
/// <summary />
protected virtual void OnCompleteInitialization()
{}
/// <summary>
/// Custom Flurl.Http configuration settings which are specific to requests made by this SDK.
/// </summary>
public FlurlHttpSettings FlurlHttpSettings
{
get
{
if(_isInitialized)
return _flurlHttpSettings.Clone();
return _flurlHttpSettings;
}
}
/// <summary>
/// Custom Json.NET configuration settings which are specific to requests made by this SDK.
/// </summary>
public JsonSerializerSettings JsonSerializerSettings
{
get
{
if (_isInitialized)
return _jsonSerializerSettings.Clone();
return _jsonSerializerSettings;
}
}
/// <summary>
/// Additional application specific user agents which should be set in the UserAgent header on all requests made by this SDK.
/// </summary>
public IList<ProductInfoHeaderValue> UserAgents
{
get
{
if(_isInitialized)
return _userAgents.AsReadOnly();
return _userAgents;
}
}
private void ApplyDefaults()
{
//
// Apply our default settings on top of user customizations, hopefully without clobbering anything
//
UserAgents.Add(new ProductInfoHeaderValue("openstack.net", GetType().GetAssemblyFileVersion()));
_jsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
_jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
_jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
if(!(_jsonSerializerSettings.ContractResolver is OpenStackContractResolver))
_jsonSerializerSettings.ContractResolver = new OpenStackContractResolver();
_flurlHttpSettings.JsonSerializer = new NewtonsoftJsonSerializer(_jsonSerializerSettings);
// When in test mode (set via HttpTest), this will be a custom class (TestHttpClientFactory)
if (_flurlHttpSettings.HttpClientFactory?.GetType() == typeof(DefaultHttpClientFactory))
_flurlHttpSettings.HttpClientFactory = new AuthenticatedHttpClientFactory();
// Apply our event handling and optionally include any custom application handlers
var applicationBeforeCall = _flurlHttpSettings.BeforeCall;
_flurlHttpSettings.BeforeCall = call =>
{
SetUserAgentHeader(call);
applicationBeforeCall?.Invoke(call);
};
var applicationAfterCall = _flurlHttpSettings.AfterCall;
_flurlHttpSettings.AfterCall = call =>
{
OpenStackNet.Tracing.TraceHttpCall(call);
applicationAfterCall?.Invoke(call);
};
var applicationOnError = _flurlHttpSettings.OnError;
_flurlHttpSettings.OnError = call =>
{
OpenStackNet.Tracing.TraceFailedHttpCall(call);
applicationOnError?.Invoke(call);
};
}
private void SetUserAgentHeader(HttpCall call)
{
foreach (ProductInfoHeaderValue userAgent in UserAgents)
{
call.Request.Headers.UserAgent.Add(userAgent);
}
}
/// <summary>
/// Raised when creating the SDK configuration options class.
/// <para>Intended for vendors to override.</para>
/// </summary>
/// <exclude />
public class CreateEvent
{
/// <summary>
/// An instance of the configuration class to use when configuring the SDK.
/// </summary>
public OpenStackNetConfigurationOptions Result = new OpenStackNetConfigurationOptions();
}
}
}