2using System.Diagnostics;
4using System.Reflection;
14 public static partial class API
16 #region Private Properties
27 internal static readonly
string JSON_CONTENT_TYPE =
"application/json";
34 internal static readonly WebExceptionStatus[] TemporaryFailureStatuses = {
35 WebExceptionStatus.ConnectFailure, WebExceptionStatus.RequestCanceled,
36 WebExceptionStatus.ConnectionClosed, WebExceptionStatus.SendFailure,
37 WebExceptionStatus.PipelineFailure, WebExceptionStatus.UnknownError,
41 WebExceptionStatus.ReceiveFailure, WebExceptionStatus.KeepAliveFailure
47 private static volatile bool _initialized;
50 private static volatile RestClient _cachedClient;
54 private static volatile bool _disableReadConfigFile;
58 private static volatile bool _disableWriteConfigFile;
61 private static volatile string _serverURL;
64 private static volatile int _parallelism;
67 private static volatile int _defaultRequestTimeout;
70 private static volatile int _defaultRequestTimeoutCollections;
71 #endregion Private Properties
73 #region Public Properties
74 private static string _userAgent = $
"ARe.Client {Assembly.GetExecutingAssembly().GetName().Version}";
84 if (_cachedClient !=
null)
85 _cachedClient.UserAgent = _userAgent;
109 _parallelism = value;
112 ServicePointManager.DefaultConnectionLimit =
Parallelism;
119 [GreaterThan(0,
true)]
122 get => _defaultRequestTimeout >= 0 ? _defaultRequestTimeout : Int32.MaxValue;
126 throw new ArgumentException(
"Timeout must be greater than 0. " +
127 "Set to int.MaxValue to never time out.");
128 _defaultRequestTimeout = value;
130 TrySaveLibrarySettings(() =>
132 Settings.Default.User_DEFAULT_REQUEST_TIMEOUT = value;
141 [GreaterThan(0,
true)]
144 get => _defaultRequestTimeoutCollections >= 0 ?
145 _defaultRequestTimeoutCollections : Int32.MaxValue;
149 throw new ArgumentException(
"Timeout must be greater than 0. " +
150 "Set to int.MaxValue to never time out.");
151 _defaultRequestTimeoutCollections = value;
153 TrySaveLibrarySettings(() =>
155 Settings.Default.User_DEFAULT_REQUEST_TIMEOUT_COLLECTIONS = value;
172 if (value == _serverURL)
return;
179 _serverURL = value ==
null ? null : value.TrimEnd(
'/') +
"/";
186 RestoreCachedAuthentication();
190 Debug.WriteLine(
"Error restoring cached credentials for the server URL " +
191 $
"{_serverURL}:\n{ex.Message}");
196 if (currentSavedURL != _serverURL)
198 TrySaveLibrarySettings(() =>
200 Settings.Default.ServerURL = _serverURL;
211 #endregion Public Properties
213 #region Public Methods
227 private static void LoadSettingsInternal(
bool overwrite)
233 ServicePointManager.UseNagleAlgorithm =
false;
236 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
238 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
241 if (overwrite || _serverURL ==
null)
250 if (overwrite || _defaultRequestTimeout == 0)
255 _defaultRequestTimeout = timeout > 0 ? timeout : Int32.MaxValue;
258 if (overwrite || _defaultRequestTimeoutCollections == 0)
263 _defaultRequestTimeoutCollections = timeout > 0 ? timeout : Int32.MaxValue;
268 #endregion Public Methods
270 #region Server Connection
274 private static RestClient GetRestClient(
int? timeout =
null)
276 return GetRestClient(
new Uri(
ServerURL), timeout);
287 private static RestClient GetRestClient(Uri serverUri,
int? timeout =
null)
290 LoadSettingsInternal(
false);
291 if (_cachedClient ==
null || !_cachedClient.BaseUrl.Equals(serverUri))
294 _cachedClient =
new RestClient(serverUri.AbsoluteUri)
297 FollowRedirects =
false
299 _cachedClient.AddHandler(JSON_CONTENT_TYPE, Converter);
302 return _cachedClient;
304 #endregion Server Connection
306 #region Settings Helpers
310 private static T? TryGetLibrarySetting<T>(Func<T> getSetting) where T :
struct
312 return (T?)TryGetLibrarySettingObj(() => (
object)getSetting());
318 private static T TryGetLibrarySettingObj<T>(Func<T> getSetting) where T :
class
320 if (_disableReadConfigFile)
return null;
327 _disableReadConfigFile =
true;
328 Debug.WriteLine(
"User scoped settings unavailable:\n" +
329 ex.Message +
"\nDisabling reading user-settings.");
338 private static void TrySaveLibrarySettings(Action modifySetting)
348 _disableWriteConfigFile =
true;
349 Debug.WriteLine(
"The library settings file cannot be modified:\n" +
350 ex.Message +
"\nDisabling updates to settings.");
353 #endregion Settings Helpers
API methods / requests made available to the user.
static bool ManagedSettingsEnabled
By Default, the Analyze Re Client Library will automatically save and restore certain configured sett...
static int Parallelism
The maximum number of concurrent requests that can be made.
static IAccessToken AuthenticationToken
The AccessToken storing authentication information for requests made to the server.
static int DefaultRequestTimeout
The default timeout used for all simple server requests, in milliseconds.
static volatile int MaxRequestRetries
When a temporary communication failure occurs (such as a socket error or an authentication failure fo...
static int DefaultRequestTimeoutCollections
The default timeout used when requesting a resource collection from the server, in milliseconds.
static string DefaultServerURL
The Default ServerURL the API is instantiated with, as configured in the settings.
static string ServerURL
The default server URL to be used for all API requests. On change, voids the current cached authentic...
static readonly string DateTimeFormat
The format used when transmitting DateTime information to Analyze Re servers.
static string UserAgent
The UserAgent identifying the requests made by this client. This can be changed if an application wis...
static readonly string AnalyzeReCustomServerHeader
The Custom Server Header returned by valid AnalyzeRe servers.
static readonly string AnalyzeReServerIdentity
The Server identity returned by valid AnalyzeRe servers.
static void LoadSettings()
Reloads all shared client library settings from the current statically configured SharedSettingsProvi...
A pre-configured RestSharp serializer / deserializer which is made to support parsing of AnalyzeRe AP...
This part of the Settings class is not generated from the Settings.settings designer,...
int User_DEFAULT_REQUEST_TIMEOUT
The user-overridden value for the default request timeout.
static Settings Default
The default settings used by the static Analyze Re API client library.
int DEFAULT_REQUEST_TIMEOUT_COLLECTIONS
The default timeout for collection GET requests, which are generally more expensive than simple resou...
int User_DEFAULT_REQUEST_TIMEOUT_COLLECTIONS
The user-overridden value for the default collection request timeout.
string ServerURL
The last API url to connected to. This can be used to override the default URL to restore a previous ...
string DefaultServerURL
The default API url to connect to. This is typically set to an example url.
int DEFAULT_REQUEST_TIMEOUT
The default timeout for simple resource requests.
int DefaultParallelism
The default number of simultaneous connections to allow to the API.