OLD | NEW |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 /// handler list. | 47 /// handler list. |
48 /// </summary> | 48 /// </summary> |
49 public abstract class BaseClientService : IClientService | 49 public abstract class BaseClientService : IClientService |
50 { | 50 { |
51 /// <summary> The class logger. </summary> | 51 /// <summary> The class logger. </summary> |
52 private static readonly ILogger Logger = ApplicationContext.Logger.ForTy
pe<BaseClientService>(); | 52 private static readonly ILogger Logger = ApplicationContext.Logger.ForTy
pe<BaseClientService>(); |
53 | 53 |
54 #region Initializer | 54 #region Initializer |
55 | 55 |
56 /// <summary>· | 56 /// <summary>· |
57 /// Indicates if exponential back-off is used automatically on exception
in a service request and\or when 5xx· | 57 /// Indicates if exponential back-off is used automatically on exception
in a service request and\or when 503· |
58 /// response is returned form the server. | 58 /// response is returned form the server. |
59 /// </summary> | 59 /// </summary> |
60 [Flags] | 60 [Flags] |
61 public enum ExponentialBackOffPolicy | 61 public enum ExponentialBackOffPolicy |
62 { | 62 { |
63 None = 0, | 63 None = 0, |
64 Exception = 1, | 64 Exception = 1, |
65 UnsuccessfulResponse5xx = 2 | 65 UnsuccessfulResponse503 = 2 |
66 } | 66 } |
67 | 67 |
68 /// <summary> An initializer class for the client service. </summary> | 68 /// <summary> An initializer class for the client service. </summary> |
69 public class Initializer | 69 public class Initializer |
70 { | 70 { |
71 /// <summary>· | 71 /// <summary>· |
72 /// Gets or sets the factory for creating <see cref="System.Net.Http
.HttpClient"/> instance. If this· | 72 /// Gets or sets the factory for creating <see cref="System.Net.Http
.HttpClient"/> instance. If this· |
73 /// property is not set the service uses a new <see cref="Google.Api
s.Http.HttpClientFactory"/> instance. | 73 /// property is not set the service uses a new <see cref="Google.Api
s.Http.HttpClientFactory"/> instance. |
74 /// </summary> | 74 /// </summary> |
75 public IHttpClientFactory HttpClientFactory { get; set; } | 75 public IHttpClientFactory HttpClientFactory { get; set; } |
76 | 76 |
77 /// <summary> | 77 /// <summary> |
78 /// Gets or sets an Http client initializer which is able to customi
ze properties on· | 78 /// Gets or sets an Http client initializer which is able to customi
ze properties on· |
79 /// <see cref="Google.Apis.Http.ConfigurableHttpClient"/> and· | 79 /// <see cref="Google.Apis.Http.ConfigurableHttpClient"/> and· |
80 /// <see cref="Google.Apis.Http.ConfigurableMessageHandler"/>. | 80 /// <see cref="Google.Apis.Http.ConfigurableMessageHandler"/>. |
81 /// </summary> | 81 /// </summary> |
82 public IConfigurableHttpClientInitializer HttpClientInitializer { ge
t; set; } | 82 public IConfigurableHttpClientInitializer HttpClientInitializer { ge
t; set; } |
83 | 83 |
84 /// <summary> | 84 /// <summary> |
85 /// Gets or sets the exponential back-off policy used by the service
. Default value is <c>Exception</c> | | 85 /// Get or sets the exponential back-off policy used by the service.
Default value is· |
86 /// <c>UnsuccessfulResponse5xx</c>, which means that exponential bac
k-off is used on any 5xx abnormal Http | 86 /// <c>UnsuccessfulResponse503</c>, which means that exponential bac
k-off is used on 503 abnormal HTTP |
87 /// response and on any exception whose thrown when sending a reques
t (except task canceled exception). | 87 /// response. |
88 /// If the value is set to <c>None</c>, no exponential back-off poli
cy is used, and it's up to user to | 88 /// If the value is set to <c>None</c>, no exponential back-off poli
cy is used, and it's up to user to |
89 /// configure the <seealso cref="Google.Apis.Http.ConfigurableMessag
eHandler"/> in an | 89 /// configure the <seealso cref="Google.Apis.Http.ConfigurableMessag
eHandler"/> in an |
90 /// <seealso cref="Google.Apis.Http.IConfigurableHttpClientInitializ
er"/> to set a specific back-off | 90 /// <seealso cref="Google.Apis.Http.IConfigurableHttpClientInitializ
er"/> to set a specific back-off |
91 /// implementation (using <seealso cref="Google.Api.Http.BackOffHand
ler"/>). | 91 /// implementation (using <seealso cref="Google.Api.Http.BackOffHand
ler"/>). |
92 /// </summary> | 92 /// </summary> |
93 public ExponentialBackOffPolicy DefaultExponentialBackOffPolicy { ge
t; set; } | 93 public ExponentialBackOffPolicy DefaultExponentialBackOffPolicy { ge
t; set; } |
94 | 94 |
95 /// <summary> Gets or sets whether this service supports GZip. Defau
lt value is <c>true</c>. </summary> | 95 /// <summary> Gets or sets whether this service supports GZip. Defau
lt value is <c>true</c>. </summary> |
96 public bool GZipEnabled { get; set; } | 96 public bool GZipEnabled { get; set; } |
97 | 97 |
(...skipping 15 matching lines...) Expand all Loading... |
113 /// Gets or sets Application name to be used in the User-Agent heade
r. Default value is <c>null</c>.· | 113 /// Gets or sets Application name to be used in the User-Agent heade
r. Default value is <c>null</c>.· |
114 /// </summary> | 114 /// </summary> |
115 public string ApplicationName { get; set; } | 115 public string ApplicationName { get; set; } |
116 | 116 |
117 /// <summary> Constructs a new initializer with default values. </su
mmary> | 117 /// <summary> Constructs a new initializer with default values. </su
mmary> |
118 public Initializer() | 118 public Initializer() |
119 { | 119 { |
120 GZipEnabled = true; | 120 GZipEnabled = true; |
121 Serializer = new NewtonsoftJsonSerializer(); | 121 Serializer = new NewtonsoftJsonSerializer(); |
122 Authenticator = NullAuthenticator.Instance; | 122 Authenticator = NullAuthenticator.Instance; |
123 DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.Excep
tion | | 123 DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.Unsuc
cessfulResponse503; |
124 ExponentialBackOffPolicy.UnsuccessfulResponse5xx; | |
125 } | 124 } |
126 } | 125 } |
127 | 126 |
128 /// <summary> | 127 /// <summary> |
129 /// An initializer which adds exponential back-off as exception handler
and\or unsuccessful response handler by | 128 /// An initializer which adds exponential back-off as exception handler
and\or unsuccessful response handler by |
130 /// the given <seealso cref="BaseClientService.ExponentialBackOffPolicy"
/>. | 129 /// the given <seealso cref="BaseClientService.ExponentialBackOffPolicy"
/>. |
131 /// </summary> | 130 /// </summary> |
132 private class ExponentialBackOffInitializer : IConfigurableHttpClientIni
tializer | 131 private class ExponentialBackOffInitializer : IConfigurableHttpClientIni
tializer |
133 { | 132 { |
134 private ExponentialBackOffPolicy Policy { get; set; } | 133 private ExponentialBackOffPolicy Policy { get; set; } |
(...skipping 11 matching lines...) Expand all Loading... |
146 public void Initialize(ConfigurableHttpClient httpClient) | 145 public void Initialize(ConfigurableHttpClient httpClient) |
147 { | 146 { |
148 var backOff = CreateBackOff(); | 147 var backOff = CreateBackOff(); |
149 | 148 |
150 // add exception handler and\or unsuccessful response handler | 149 // add exception handler and\or unsuccessful response handler |
151 if ((Policy & ExponentialBackOffPolicy.Exception) == Exponential
BackOffPolicy.Exception) | 150 if ((Policy & ExponentialBackOffPolicy.Exception) == Exponential
BackOffPolicy.Exception) |
152 { | 151 { |
153 httpClient.MessageHandler.ExceptionHandlers.Add(backOff); | 152 httpClient.MessageHandler.ExceptionHandlers.Add(backOff); |
154 } | 153 } |
155 | 154 |
156 if ((Policy & ExponentialBackOffPolicy.UnsuccessfulResponse5xx)
== | 155 if ((Policy & ExponentialBackOffPolicy.UnsuccessfulResponse503)
== |
157 ExponentialBackOffPolicy.UnsuccessfulResponse5xx) | 156 ExponentialBackOffPolicy.UnsuccessfulResponse503) |
158 { | 157 { |
159 httpClient.MessageHandler.UnsuccessfulResponseHandlers.Add(b
ackOff); | 158 httpClient.MessageHandler.UnsuccessfulResponseHandlers.Add(b
ackOff); |
160 } | 159 } |
161 } | 160 } |
162 } | 161 } |
163 | 162 |
164 #endregion | 163 #endregion |
165 | 164 |
166 /// <summary> Constructs a new base client with the specified initialize
r. </summary> | 165 /// <summary> Constructs a new base client with the specified initialize
r. </summary> |
167 protected BaseClientService(Initializer initializer) | 166 protected BaseClientService(Initializer initializer) |
168 { | 167 { |
169 // sets the right properties by the initializer's properties | 168 // sets the right properties by the initializer's properties |
170 GZipEnabled = initializer.GZipEnabled; | 169 GZipEnabled = initializer.GZipEnabled; |
171 Serializer = initializer.Serializer; | 170 Serializer = initializer.Serializer; |
172 ApiKey = initializer.ApiKey; | 171 ApiKey = initializer.ApiKey; |
173 Authenticator = initializer.Authenticator; | 172 Authenticator = initializer.Authenticator; |
174 ApplicationName = initializer.ApplicationName; | 173 ApplicationName = initializer.ApplicationName; |
175 if (ApplicationName == null) | 174 if (ApplicationName == null) |
176 { | 175 { |
177 Logger.Warning("Application name is not set. Please set Initiali
zer.ApplicationName property"); | 176 Logger.Warning("Application name is not set. Please set Initiali
zer.ApplicationName property"); |
178 } | 177 } |
179 HttpClientInitializer = initializer.HttpClientInitializer; | 178 HttpClientInitializer = initializer.HttpClientInitializer; |
180 | 179 |
181 // create an Http client for this service | 180 // create an Http client for this service |
182 HttpClient = CreateHttpClient(initializer); | 181 HttpClient = CreateHttpClient(initializer); |
183 } | 182 } |
184 | 183 |
185 /// <summary> | 184 /// <summary> Return <c>true</c> if this service contains the specified
feature. </summary> |
186 /// Return true if this service contains the specified feature. | |
187 /// </summary> | |
188 private bool HasFeature(Features feature) | 185 private bool HasFeature(Features feature) |
189 { | 186 { |
190 return Features.Contains(feature.GetStringValue()); | 187 return Features.Contains(feature.GetStringValue()); |
191 } | 188 } |
192 | 189 |
193 private ConfigurableHttpClient CreateHttpClient(Initializer initializer) | 190 private ConfigurableHttpClient CreateHttpClient(Initializer initializer) |
194 { | 191 { |
195 // if factory wasn't set use the default Http client factory | 192 // if factory wasn't set use the default Http client factory |
196 var factory = initializer.HttpClientFactory ?? new HttpClientFactory
(); | 193 var factory = initializer.HttpClientFactory ?? new HttpClientFactory
(); |
197 var args = new CreateHttpClientArgs | 194 var args = new CreateHttpClientArgs |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 407 |
411 public virtual void Dispose() | 408 public virtual void Dispose() |
412 { | 409 { |
413 if (HttpClient != null) | 410 if (HttpClient != null) |
414 { | 411 { |
415 HttpClient.Dispose(); | 412 HttpClient.Dispose(); |
416 } | 413 } |
417 } | 414 } |
418 } | 415 } |
419 } | 416 } |
OLD | NEW |