Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 30 matching lines...) Expand all Loading... | |
41 /// This class adds a special <see cref="Google.Apis.Http.IHttpExecuteInterc eptor"/> to the· | 41 /// This class adds a special <see cref="Google.Apis.Http.IHttpExecuteInterc eptor"/> to the· |
42 /// <see cref="Google.Apis.Http.ConfigurableMessageHandler"/> execute interc eptor list, which uses the given· | 42 /// <see cref="Google.Apis.Http.ConfigurableMessageHandler"/> execute interc eptor list, which uses the given· |
43 /// Authenticator. It calls to its applying authentication method, and injec ts the "Authorization" header in the· | 43 /// Authenticator. It calls to its applying authentication method, and injec ts the "Authorization" header in the· |
44 /// request. | 44 /// request. |
45 /// If the given Authenticator implements <see cref="Google.Apis.Http.IHttpU nsuccessfulResponseHandler"/>, this· | 45 /// If the given Authenticator implements <see cref="Google.Apis.Http.IHttpU nsuccessfulResponseHandler"/>, this· |
46 /// class adds the Authenticator to the <see cref="Google.Apis.Http.Configur ableMessageHandler"/>'s unsuccessful· | 46 /// class adds the Authenticator to the <see cref="Google.Apis.Http.Configur ableMessageHandler"/>'s unsuccessful· |
47 /// response handler list. | 47 /// response 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> An initializer class for the client service. </summary> | 56 /// <summary>An initializer class for the client service.</summary> |
57 public class Initializer | 57 public class Initializer |
58 { | 58 { |
59 /// <summary> | 59 /// <summary> |
60 /// Gets or sets the factory for creating <see cref="System.Net.Http .HttpClient"/> instance. If this· | 60 /// Gets or sets the factory for creating <see cref="System.Net.Http .HttpClient"/> instance. If this· |
61 /// property is not set the service uses a new <see cref="Google.Api s.Http.HttpClientFactory"/> instance. | 61 /// property is not set the service uses a new <see cref="Google.Api s.Http.HttpClientFactory"/> instance. |
62 /// </summary> | 62 /// </summary> |
63 public IHttpClientFactory HttpClientFactory { get; set; } | 63 public IHttpClientFactory HttpClientFactory { get; set; } |
64 | 64 |
65 /// <summary> | 65 /// <summary> |
66 /// Gets or sets a HTTP client initializer which is able to customiz e properties on· | 66 /// Gets or sets a HTTP client initializer which is able to customiz e properties on· |
67 /// <see cref="Google.Apis.Http.ConfigurableHttpClient"/> and· | 67 /// <see cref="Google.Apis.Http.ConfigurableHttpClient"/> and· |
68 /// <see cref="Google.Apis.Http.ConfigurableMessageHandler"/>. | 68 /// <see cref="Google.Apis.Http.ConfigurableMessageHandler"/>. |
69 /// </summary> | 69 /// </summary> |
70 public IConfigurableHttpClientInitializer HttpClientInitializer { ge t; set; } | 70 public IConfigurableHttpClientInitializer HttpClientInitializer { ge t; set; } |
71 | 71 |
72 /// <summary> | 72 /// <summary> |
73 /// Get or sets the exponential back-off policy used by the service. Default value is· | 73 /// Get or sets the exponential back-off policy used by the service. Default value is· |
74 /// <c>UnsuccessfulResponse503</c>, which means that exponential bac k-off is used on 503 abnormal HTTP | 74 /// <c>UnsuccessfulResponse503</c>, which means that exponential bac k-off is used on 503 abnormal HTTP |
75 /// response. | 75 /// response. |
76 /// If the value is set to <c>None</c>, no exponential back-off poli cy is used, and it's up to user to | 76 /// If the value is set to <c>None</c>, no exponential back-off poli cy is used, and it's up to user to |
77 /// configure the <seealso cref="Google.Apis.Http.ConfigurableMessag eHandler"/> in an | 77 /// configure the <seealso cref="Google.Apis.Http.ConfigurableMessag eHandler"/> in an |
78 /// <seealso cref="Google.Apis.Http.IConfigurableHttpClientInitializ er"/> to set a specific back-off | 78 /// <seealso cref="Google.Apis.Http.IConfigurableHttpClientInitializ er"/> to set a specific back-off |
79 /// implementation (using <seealso cref="Google.Apis.Http.BackOffHan dler"/>). | 79 /// implementation (using <seealso cref="Google.Apis.Http.BackOffHan dler"/>). |
80 /// </summary> | 80 /// </summary> |
81 public ExponentialBackOffPolicy DefaultExponentialBackOffPolicy { ge t; set; } | 81 public ExponentialBackOffPolicy DefaultExponentialBackOffPolicy { ge t; set; } |
82 | 82 |
83 /// <summary> Gets or sets whether this service supports GZip. Defau lt value is <c>true</c>. </summary> | 83 /// <summary>Gets or sets whether this service supports GZip. Defaul t value is <c>true</c>.</summary> |
84 public bool GZipEnabled { get; set; } | 84 public bool GZipEnabled { get; set; } |
85 | 85 |
86 /// <summary> | 86 /// <summary> |
87 /// Gets and Sets the serializer. Default value is <see cref="Google .Apis.Json.NewtonsoftJsonSerializer"/>. | 87 /// Gets and Sets the serializer. Default value is <see cref="Google .Apis.Json.NewtonsoftJsonSerializer"/>. |
88 /// </summary> | 88 /// </summary> |
89 public ISerializer Serializer { get; set; } | 89 public ISerializer Serializer { get; set; } |
90 | 90 |
91 /// <summary> Gets or sets the API Key. Default value is <c>null</c> . </summary> | 91 /// <summary>Gets or sets the API Key. Default value is <c>null</c>. </summary> |
92 public string ApiKey { get; set; } | 92 public string ApiKey { get; set; } |
93 | 93 |
94 /// <summary> | 94 /// <summary> |
95 /// Gets or sets the Authenticator. Default value is· | 95 /// Gets or sets the Authenticator. Default value is· |
96 /// <see cref="Google.Apis.Authentication.NullAuthenticator.Instance "/>. | 96 /// <see cref="Google.Apis.Authentication.NullAuthenticator.Instance "/>. |
97 /// </summary> | 97 /// </summary> |
98 public IAuthenticator Authenticator { get; set; } | 98 public IAuthenticator Authenticator { get; set; } |
99 | 99 |
100 /// <summary> | 100 /// <summary> |
101 /// Gets or sets Application name to be used in the User-Agent heade r. Default value is <c>null</c>.· | 101 /// Gets or sets Application name to be used in the User-Agent heade r. Default value is <c>null</c>.· |
102 /// </summary> | 102 /// </summary> |
103 public string ApplicationName { get; set; } | 103 public string ApplicationName { get; set; } |
104 | 104 |
105 /// <summary> Constructs a new initializer with default values. </su mmary> | 105 /// <summary>Constructs a new initializer with default values.</summ ary> |
106 public Initializer() | 106 public Initializer() |
107 { | 107 { |
108 GZipEnabled = true; | 108 GZipEnabled = true; |
109 Serializer = new NewtonsoftJsonSerializer(); | 109 Serializer = new NewtonsoftJsonSerializer(); |
110 Authenticator = NullAuthenticator.Instance; | 110 Authenticator = NullAuthenticator.Instance; |
111 DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.Unsuc cessfulResponse503; | 111 DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.Unsuc cessfulResponse503; |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 #endregion | 115 #endregion |
116 | 116 |
117 /// <summary> Constructs a new base client with the specified initialize r. </summary> | 117 /// <summary>Constructs a new base client with the specified initializer .</summary> |
118 protected BaseClientService(Initializer initializer) | 118 protected BaseClientService(Initializer initializer) |
119 { | 119 { |
120 // sets the right properties by the initializer's properties | 120 // Set the right properties by the initializer's properties. |
121 GZipEnabled = initializer.GZipEnabled; | 121 GZipEnabled = initializer.GZipEnabled; |
122 Serializer = initializer.Serializer; | 122 Serializer = initializer.Serializer; |
123 ApiKey = initializer.ApiKey; | 123 ApiKey = initializer.ApiKey; |
124 Authenticator = initializer.Authenticator; | 124 Authenticator = initializer.Authenticator; |
125 ApplicationName = initializer.ApplicationName; | 125 ApplicationName = initializer.ApplicationName; |
126 if (ApplicationName == null) | 126 if (ApplicationName == null) |
127 { | 127 { |
128 Logger.Warning("Application name is not set. Please set Initiali zer.ApplicationName property"); | 128 Logger.Warning("Application name is not set. Please set Initiali zer.ApplicationName property"); |
129 } | 129 } |
130 HttpClientInitializer = initializer.HttpClientInitializer; | 130 HttpClientInitializer = initializer.HttpClientInitializer; |
131 | 131 |
132 // create a HTTP client for this service | 132 // Create a HTTP client for this service. |
class
2013/09/16 23:47:29
Add capital and period
peleyal
2013/09/17 15:53:40
Done.
| |
133 HttpClient = CreateHttpClient(initializer); | 133 HttpClient = CreateHttpClient(initializer); |
134 } | 134 } |
135 | 135 |
136 /// <summary> Return <c>true</c> if this service contains the specified feature. </summary> | 136 /// <summary>Returns <c>true</c> if this service contains the specified feature.</summary> |
137 private bool HasFeature(Features feature) | 137 private bool HasFeature(Features feature) |
138 { | 138 { |
139 return Features.Contains(feature.GetStringValue()); | 139 return Features.Contains(feature.GetStringValue()); |
140 } | 140 } |
141 | 141 |
142 private ConfigurableHttpClient CreateHttpClient(Initializer initializer) | 142 private ConfigurableHttpClient CreateHttpClient(Initializer initializer) |
143 { | 143 { |
144 // if factory wasn't set use the default HTTP client factory | 144 // If factory wasn't set use the default HTTP client factory. |
class
2013/09/16 23:47:29
Cap / period
peleyal
2013/09/17 15:53:40
Done.
| |
145 var factory = initializer.HttpClientFactory ?? new HttpClientFactory (); | 145 var factory = initializer.HttpClientFactory ?? new HttpClientFactory (); |
146 var args = new CreateHttpClientArgs | 146 var args = new CreateHttpClientArgs |
147 { | 147 { |
148 GZipEnabled = GZipEnabled, | 148 GZipEnabled = GZipEnabled, |
149 ApplicationName = ApplicationName, | 149 ApplicationName = ApplicationName, |
150 }; | 150 }; |
151 | 151 |
152 // add the user's input initializer | 152 // Add the user's input initializer. |
153 if (HttpClientInitializer != null) | 153 if (HttpClientInitializer != null) |
154 { | 154 { |
155 args.Initializers.Add(HttpClientInitializer); | 155 args.Initializers.Add(HttpClientInitializer); |
156 } | 156 } |
157 | 157 |
158 // add exponential back-off initializer if necessary | 158 // Add exponential back-off initializer if necessary. |
159 if (initializer.DefaultExponentialBackOffPolicy != ExponentialBackOf fPolicy.None) | 159 if (initializer.DefaultExponentialBackOffPolicy != ExponentialBackOf fPolicy.None) |
160 { | 160 { |
161 args.Initializers.Add(new ExponentialBackOffInitializer(initiali zer.DefaultExponentialBackOffPolicy, | 161 args.Initializers.Add(new ExponentialBackOffInitializer(initiali zer.DefaultExponentialBackOffPolicy, |
162 CreateBackOffHandler)); | 162 CreateBackOffHandler)); |
163 } | 163 } |
164 | 164 |
165 // add authenticator initializer to intercept a request and add the "Authorization" header and also handle | 165 // Add authenticator initializer to intercept a request and add the "Authorization" header and also handle |
166 // abnormal 401 responses in case the authenticator is an instance o f unsuccessful response handler. | 166 // abnormal 401 responses in case the authenticator is an instance o f unsuccessful response handler. |
167 args.Initializers.Add(new AuthenticatorMessageHandlerInitializer(Aut henticator)); | 167 args.Initializers.Add(new AuthenticatorMessageHandlerInitializer(Aut henticator)); |
168 | 168 |
169 return factory.CreateHttpClient(args); | 169 return factory.CreateHttpClient(args); |
170 } | 170 } |
171 | 171 |
172 /// <summary> | 172 /// <summary> |
173 /// Creates the back-off handler with <seealso cref="Google.Apis.Util.Ex ponentialBackOff"/>.· | 173 /// Creates the back-off handler with <seealso cref="Google.Apis.Util.Ex ponentialBackOff"/>.· |
174 /// Overrides this method to change the default behavior of back-off han dler (e.g. you can change the maximum | 174 /// Overrides this method to change the default behavior of back-off han dler (e.g. you can change the maximum |
175 /// waited request's time span, or create a back-off handler with you ow n implementation of· | 175 /// waited request's time span, or create a back-off handler with you ow n implementation of· |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 public ISerializer Serializer { get; private set; } | 229 public ISerializer Serializer { get; private set; } |
230 | 230 |
231 public virtual string SerializeObject(object obj) | 231 public virtual string SerializeObject(object obj) |
232 { | 232 { |
233 if (HasFeature(Discovery.Features.LegacyDataResponse)) | 233 if (HasFeature(Discovery.Features.LegacyDataResponse)) |
234 { | 234 { |
235 // Legacy path | 235 // Legacy path |
236 var request = new StandardResponse<object> { Data = obj }; | 236 var request = new StandardResponse<object> { Data = obj }; |
237 return Serializer.Serialize(request); | 237 return Serializer.Serialize(request); |
238 } | 238 } |
239 | |
240 // New v1.0 path | |
241 return Serializer.Serialize(obj); | 239 return Serializer.Serialize(obj); |
242 } | 240 } |
243 | 241 |
244 public virtual async Task<T> DeserializeResponse<T>(HttpResponseMessage response) | 242 public virtual async Task<T> DeserializeResponse<T>(HttpResponseMessage response) |
245 { | 243 { |
246 var text = await response.Content.ReadAsStringAsync().ConfigureAwait (false); | 244 var text = await response.Content.ReadAsStringAsync().ConfigureAwait (false); |
247 | 245 |
248 // If a string is request, don't parse the response. | 246 // If a string is request, don't parse the response. |
249 if (typeof(T).Equals(typeof(string))) | 247 if (typeof(T).Equals(typeof(string))) |
250 { | 248 { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 public abstract string Name { get; } | 329 public abstract string Name { get; } |
332 public abstract string BaseUri { get; } | 330 public abstract string BaseUri { get; } |
333 public abstract string BasePath { get; } | 331 public abstract string BasePath { get; } |
334 | 332 |
335 public abstract IList<string> Features { get; } | 333 public abstract IList<string> Features { get; } |
336 | 334 |
337 #endregion | 335 #endregion |
338 | 336 |
339 #endregion | 337 #endregion |
340 | 338 |
341 /// <summary> Creates a GZip stream by the given serialized object. </su mmary> | 339 /// <summary>Creates a GZip stream by the given serialized object.</summ ary> |
342 private static Stream CreateGZipStream(string serializedObject) | 340 private static Stream CreateGZipStream(string serializedObject) |
343 { | 341 { |
344 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(serializedObject); | 342 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(serializedObject); |
345 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) | 343 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) |
346 { | 344 { |
347 using (GZipStream gzip = new GZipStream(ms, CompressionMode.Comp ress, true)) | 345 using (GZipStream gzip = new GZipStream(ms, CompressionMode.Comp ress, true)) |
348 { | 346 { |
349 gzip.Write(bytes, 0, bytes.Length); | 347 gzip.Write(bytes, 0, bytes.Length); |
350 } | 348 } |
351 | 349 |
352 // reset the stream to the beginning. It doesn't work otherwise! | 350 // reset the stream to the beginning. It doesn't work otherwise! |
353 ms.Position = 0; | 351 ms.Position = 0; |
354 byte[] compressed = new byte[ms.Length]; | 352 byte[] compressed = new byte[ms.Length]; |
355 ms.Read(compressed, 0, compressed.Length); | 353 ms.Read(compressed, 0, compressed.Length); |
356 return new MemoryStream(compressed); | 354 return new MemoryStream(compressed); |
357 } | 355 } |
358 } | 356 } |
359 | 357 |
360 public virtual void Dispose() | 358 public virtual void Dispose() |
361 { | 359 { |
362 if (HttpClient != null) | 360 if (HttpClient != null) |
363 { | 361 { |
364 HttpClient.Dispose(); | 362 HttpClient.Dispose(); |
365 } | 363 } |
366 } | 364 } |
367 } | 365 } |
368 } | 366 } |
LEFT | RIGHT |