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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
Copyright 2013 Google Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using Google.Apis.Core;
using Google.Apis.Discovery;
using Google.Apis.Http;
using Google.Apis.Json;
using Google.Apis.Logging;
using Google.Apis.Requests;
using Google.Apis.Responses;
using Google.Apis.Testing;
using Google.Apis.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Google.Apis.Services
{
    /// <summary>
    /// A base class for a client service which provides common mechanism for all services, like 
    /// serialization and GZip support.  It should be safe to use a single service instance to make server requests
    /// concurrently from multiple threads. 
    /// This class adds a special <see cref="IHttpExecuteInterceptor"/> to the 
    /// <see cref="ConfigurableMessageHandler"/> execute interceptor list, which uses the given 
    /// Authenticator. It calls to its applying authentication method, and injects the "Authorization" header in the 
    /// request.
    /// If the given Authenticator implements <see cref="IHttpUnsuccessfulResponseHandler"/>, this 
    /// class adds the Authenticator to the <see cref="ConfigurableMessageHandler"/>'s unsuccessful 
    /// response handler list.
    /// </summary>
    public abstract class BaseClientService : IClientService
    {
        private const string DefaultUniverseDomain = "googleapis.com";
        private const string UniverseDomainEnvironmentVariable = "GOOGLE_CLOUD_UNIVERSE_DOMAIN";

        /// <summary>The class logger.</summary>
        private static readonly ILogger Logger = ApplicationContext.Logger.ForType<BaseClientService>();

        /// <summary>The default maximum allowed length of a URL string for GET requests.</summary>
        [VisibleForTestOnly]
        public const uint DefaultMaxUrlLength = 2048;

        #region Initializer

        /// <summary>An initializer class for the client service.</summary>
        public class Initializer
        {
            /// <summary>
            /// Gets or sets the factory for creating <see cref="System.Net.Http.HttpClient"/> instance. If this 
            /// property is not set the service uses a new <see cref="Http.HttpClientFactory"/> instance.
            /// </summary>
            public IHttpClientFactory HttpClientFactory { get; set; }

            /// <summary>
            /// Gets or sets a HTTP client initializer which is able to customize properties on 
            /// <see cref="ConfigurableHttpClient"/> and 
            /// <see cref="ConfigurableMessageHandler"/>.
            /// </summary>
            public IConfigurableHttpClientInitializer HttpClientInitializer { get; set; }

            /// <summary>
            /// Indicates which of exceptions and / or HTTP status codes are automatically retried using exponential backoff.
            /// The default value is <see cref="ExponentialBackOffPolicy.UnsuccessfulResponse503"/> which means HTTP Status code 503
            /// will be retried with exponential backoff.
            /// If set to <see cref="ExponentialBackOffPolicy.None" /> no automatic retries will happen.
            /// Calling code may still specify custom retries by configuring <see cref="HttpClient"/>.
            /// </summary>
            public ExponentialBackOffPolicy DefaultExponentialBackOffPolicy { get; set; }

            /// <summary>Gets or sets whether this service supports GZip. Default value is <c>true</c>.</summary>
            public bool GZipEnabled { get; set; }

            /// <summary>
            /// Gets or sets the serializer. Default value is <see cref="NewtonsoftJsonSerializer"/>.
            /// </summary>
            public ISerializer Serializer { get; set; }

            /// <summary>Gets or sets the API Key. Default value is <c>null</c>.</summary>
            public string ApiKey { get; set; }

            /// <summary>
            /// Gets or sets Application name to be used in the User-Agent header. Default value is <c>null</c>. 
            /// </summary>
            public string ApplicationName { get; set; }

            /// <summary>
            /// Maximum allowed length of a URL string for GET requests. Default value is <c>2048</c>. If the value is
            /// set to <c>0</c>, requests will never be modified due to URL string length.
            /// </summary>
            public uint MaxUrlLength { get; set; }

            /// <summary>
            /// Gets or sets the base URI to use for the service. If the value is <c>null</c>,
            /// the default base URI for the service is used.
            /// </summary>
            public string BaseUri { get; set; }

            /// <summary>
            /// The universe domain to connect to, or null to use the default universe domain <see cref="DefaultUniverseDomain"/>.
            /// </summary>
            /// <remarks>
            /// <para>
            /// <see cref="UniverseDomain"/> is used to build the endpoint to connect to, unless <see cref="BaseUri"/>
            /// is set, in which case <see cref="BaseUri"/> will be used without further modification.
            /// <see cref="UniverseDomain"/> may also be used by the credential, if any, to validate against its
            /// own universe domain.
            /// </para>
            /// </remarks>
            public string UniverseDomain { get; set; }

            /// <summary>
            /// The timeout to set on <see cref="HttpClient"/> instances used by the service.
            /// May be null, in which case the default timeout values on <see cref="HttpClient"/> instances
            /// used by this service will be left unchanged.
            /// </summary>
            public TimeSpan? HttpClientTimeout { get; set; }

            /// <summary>
            /// Builder for the x-goog-api-client header, collecting version information.
            /// Services automatically add the API library version to this.
            /// Most users will never need to configure this, but higher level abstraction Google libraries
            /// may add their own version here.
            /// </summary>
            public VersionHeaderBuilder VersionHeaderBuilder { get; }

            /// <summary>
            /// Determines whether request parameters are validated (client-side) by default.
            /// Defaults to true. This can be overridden on a per-request basis using <see cref="ClientServiceRequest{TResponse}.ValidateParameters"/>.
            /// </summary>
            public bool ValidateParameters { get; set; } = true;

            /// <summary>Constructs a new initializer with default values.</summary>
            public Initializer()
            {
                GZipEnabled = true;
                Serializer = new NewtonsoftJsonSerializer();
                DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.UnsuccessfulResponse503;
                MaxUrlLength = DefaultMaxUrlLength;
                VersionHeaderBuilder = new VersionHeaderBuilder()
                    .AppendDotNetEnvironment();
            }

            // HttpRequestMessage.Headers fails if any of these characters are included in a User-Agent header.
            private const string InvalidApplicationNameCharacters = "\"(),:;<=>?@[\\]{}";

            internal void Validate()
            {
                if (ApplicationName != null && ApplicationName.Any(c => InvalidApplicationNameCharacters.Contains(c)))
                {
                    throw new ArgumentException("Invalid Application name", nameof(ApplicationName));
                }
            }
        }

        #endregion

        /// <summary>Constructs a new base client with the specified initializer.</summary>
        protected BaseClientService(Initializer initializer)
        {
            initializer.Validate();
            // Note that GetType() will get the *actual* type, which will be the service type in the API-specific library.
            // That's the version we want to append.
            // It's important that we clone the VersionHeaderBuilder, so we don't modify the initializer - otherwise
            // a single initializer can't be used for multiple services (which can be useful).
            string versionHeader = initializer.VersionHeaderBuilder.Clone()
                .AppendAssemblyVersion("gdcl", GetType())
                .ToString();
            // Set the right properties by the initializer's properties.
            GZipEnabled = initializer.GZipEnabled;
            Serializer = initializer.Serializer;
            ApiKey = initializer.ApiKey;
            ApplicationName = initializer.ApplicationName;
            BaseUriOverride = initializer.BaseUri;
#pragma warning disable CS0618 // We still need to initialize the value on construction but the setter is rightly obsolete.
            UniverseDomain = initializer.UniverseDomain;
#pragma warning restore CS0618 // Type or member is obsolete
            HttpClientTimeout = initializer.HttpClientTimeout;
            ValidateParameters = initializer.ValidateParameters;
            if (ApplicationName == null)
            {
                Logger.Warning("Application name is not set. Please set Initializer.ApplicationName property");
            }
            HttpClientInitializer = initializer.HttpClientInitializer;

            // Create a HTTP client for this service.
            HttpClient = CreateHttpClient(initializer, versionHeader);
        }

        /// <summary>
        /// Determines whether or not request parameters should be validated client-side.
        /// This may be overridden on a per-request basis.
        /// </summary>
        internal bool ValidateParameters { get; }

        /// <summary>
        /// The BaseUri provided in the initializer, which may be null.
        /// </summary>
        protected string BaseUriOverride { get; }

        /// <summary>
        /// The universe domain to connect to, or null to use the default universe domain,
        /// which may be configured via the <see cref="UniverseDomainEnvironmentVariable"/> .
        /// </summary>
        /// <remarks>
        /// <para>
        /// <see cref="UniverseDomain"/> is used to build the endpoint to connect to, unless <see cref="BaseUriOverride"/>
        /// is set, in which case <see cref="BaseUriOverride"/> will be used without further modification.
        /// </para>
        /// </remarks>
        public string UniverseDomain
        { 
            get;
            [Obsolete(
                "Using this setter never had any effect, the UniverseDomain value is used on Client construction only. " +
                "Build a new Client specifying a new universe domain value instead.")]
            set; 
        }

        /// <summary>
        /// The configured universe domain, which is:
        /// <list type="bullet">
        /// <item>
        /// <see cref="UniverseDomain"/> if not null.
        /// </item>
        /// <item>
        /// Otherwise, the value of the environment variable with name <see cref="UniverseDomainEnvironmentVariable"/>
        /// if set to a non empty or blank-only value.
        /// </item>
        /// <item>
        /// Otherwise, null.
        /// </item>
        /// </list>
        /// </summary>
        private string EffectiveConfiguredUniverseDomain =>
            UniverseDomain ?? GetNonWhiteSpaceOrNullEnvironmentVariable(UniverseDomainEnvironmentVariable);

        /// <summary>
        /// The timeout to set on <see cref="HttpClient"/> instances used by the service.
        /// May be null, in which case the default timeout values on <see cref="HttpClient"/> instances
        /// used by this service will be left unchanged.
        /// </summary>
        public TimeSpan? HttpClientTimeout { get; }

        /// <summary>Returns <c>true</c> if this service contains the specified feature.</summary>
        private bool HasFeature(Features feature)
        {
            return Features.Contains(Utilities.GetEnumStringValue(feature));
        }

        private ConfigurableHttpClient CreateHttpClient(Initializer initializer, string versionHeader)
        {
            // If factory wasn't set use the default HTTP client factory.
            var factory = initializer.HttpClientFactory ?? new HttpClientFactory();
            var args = new CreateHttpClientArgs
            {
                GZipEnabled = GZipEnabled,
                ApplicationName = ApplicationName,
                GoogleApiClientHeader = versionHeader,
                UniverseDomain = EffectiveConfiguredUniverseDomain ?? DefaultUniverseDomain,
            };

            // Add the user's input initializer.
            if (HttpClientInitializer != null)
            {
                args.Initializers.Add(HttpClientInitializer);
            }

            // Add exponential back-off initializer if necessary.
            if (initializer.DefaultExponentialBackOffPolicy != ExponentialBackOffPolicy.None)
            {
                var effectiveRetryPolicy = GetEffectiveRetryPolicy(initializer.DefaultExponentialBackOffPolicy);
                args.Initializers.Add(new ExponentialBackOffInitializer(initializer.DefaultExponentialBackOffPolicy, CreateBackOffHandler));
            }

            // Add HTTP client timeout initializer if necessary.
            if (HttpClientTimeout is not null)
            {
                args.Initializers.Add(new HttpTimeoutInitializer(HttpClientTimeout.Value));
            }

            var httpClient = factory.CreateHttpClient(args);
            if (initializer.MaxUrlLength > 0)
            {
                httpClient.MessageHandler.AddExecuteInterceptor(new MaxUrlLengthInterceptor(initializer.MaxUrlLength));
            }
            return httpClient;

            static ExponentialBackOffPolicy GetEffectiveRetryPolicy(ExponentialBackOffPolicy retryPolicy) =>
                retryPolicy.HasFlag(ExponentialBackOffPolicy.RecommendedOrDefault) ?
                // At this level there's no recommendation, but we know default is retry 503.
                // Remove RecommendedOrDefault and add UnsuccessfulResponse503.
                (retryPolicy & ~ExponentialBackOffPolicy.RecommendedOrDefault) | ExponentialBackOffPolicy.UnsuccessfulResponse503 :
                retryPolicy;
        }

        /// <summary>
        /// Creates the back-off handler with <see cref="ExponentialBackOff"/>. 
        /// Overrides this method to change the default behavior of back-off handler (e.g. you can change the maximum
        /// waited request's time span, or create a back-off handler with you own implementation of 
        /// <see cref="IBackOff"/>).
        /// </summary>
        protected virtual BackOffHandler CreateBackOffHandler()
        {
            // TODO(peleyal): consider return here interface and not the concrete class
            return new BackOffHandler(new ExponentialBackOff());
        }

        /// <summary>
        /// Gets the effective URI taking into account the <see cref="UniverseDomain"/> and the value of
        /// the <see cref="UniverseDomainEnvironmentVariable"/>.
        /// </summary>
        /// <param name="explicitUri">An explicit URI. May be null.</param>
        /// <param name="defaultUri">A default URI. May be null.</param>
        /// <returns>
        /// <list type="bullet">
        /// <item><paramref name="explicitUri"/> if not null.</item>
        /// <item>
        /// Otherwise, if <see cref="EffectiveConfiguredUniverseDomain"/> is not null, the result of replacing
        /// <see cref="DefaultUniverseDomain"/> with <see cref="EffectiveConfiguredUniverseDomain"/>
        /// in <paramref name="defaultUri"/>.
        /// </item>
        /// Otherwise <paramref name="defaultUri"/>.
        /// </list>
        /// </returns>
        protected internal string GetEffectiveUri(string explicitUri, string defaultUri) =>
            // Note this method needs to receive explicit and default URIs so we can use
            // it for the batch endpoint as well. The batch endpoint does not have an
            // override mechanism, so we pass explicitUri as null in that case.
            explicitUri ??
            (EffectiveConfiguredUniverseDomain is string configureUniverseDomain ? defaultUri?.Replace(DefaultUniverseDomain, UniverseDomain) :
            defaultUri);

        #region IClientService Members

        /// <inheritdoc/>
        public ConfigurableHttpClient HttpClient { get; private set; }

        /// <inheritdoc/>
        public IConfigurableHttpClientInitializer HttpClientInitializer { get; private set; }

        /// <inheritdoc/>
        public bool GZipEnabled { get; private set; }

        /// <inheritdoc/>
        public string ApiKey { get; private set; }

        /// <inheritdoc/>
        public string ApplicationName { get; private set; }

        /// <inheritdoc/>
        public void SetRequestSerailizedContent(HttpRequestMessage request, object body)
        {
            request.SetRequestSerailizedContent(this, body, GZipEnabled);
        }

        #region Serialization

        /// <inheritdoc/>
        public ISerializer Serializer { get; private set; }

        /// <inheritdoc/>
        public virtual string SerializeObject(object obj) => Serializer.Serialize(obj);

        /// <inheritdoc/>
        public virtual async Task<T> DeserializeResponse<T>(HttpResponseMessage response)
        {
            var text = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            // If a string is request, don't parse the response.
			if (Type.Equals(typeof(T), typeof(string)))
            {
                return (T)(object)text;
            }

            // Check if there was an error returned. The error node is returned in both paths
            // Deserialize the stream based upon the format of the stream.
            if (HasFeature(Discovery.Features.LegacyDataResponse))
            {
                // Legacy path (deprecated!)
                StandardResponse<T> sr = null;
                try
                {
                    sr = Serializer.Deserialize<StandardResponse<T>>(text);
                }
                catch (Exception ex)
                {
                    throw new GoogleApiException(Name,
                        $"Failed to parse response from server as {Serializer.Format ?? "unknown format"} [" + text + "]", ex);
                }

                if (sr.Error != null)
                {
                    throw new GoogleApiException(Name, "Server error - " + sr.Error)
                    {
                        Error = sr.Error
                    };
                }

                if (sr.Data == null)
                {
                    throw new GoogleApiException(Name, "The response could not be deserialized.");
                }
                return sr.Data;
            }

            // New path: Deserialize the object directly.
            T result = default(T);
            try
            {
                result = Serializer.Deserialize<T>(text);
            }
            catch (Exception ex)
            {
                throw new GoogleApiException(Name, $"Failed to parse response from server as {Serializer.Format ?? "unknown format"} [" + text + "]", ex);
            }

            // TODO(peleyal): is this the right place to check ETag? it isn't part of deserialization!
            // If this schema/object provides an error container, check it.
            var eTag = response.Headers.ETag != null ? response.Headers.ETag.Tag : null;
            if (result is IDirectResponseSchema && eTag != null)
            {
                (result as IDirectResponseSchema).ETag = eTag;
            }
            return result;
        }

        /// <inheritdoc/>
        public virtual Task<RequestError> DeserializeError(HttpResponseMessage response) =>
            response.DeserializeErrorAsync(Name, Serializer);

        #endregion

        #region Abstract Members

        /// <inheritdoc/>
        public abstract string Name { get; }

        /// <inheritdoc/>
        public abstract string BaseUri { get; }

        /// <inheritdoc/>
        public abstract string BasePath { get; }

        /// <summary>The URI used for batch operations.</summary>
        public virtual string BatchUri { get { return null; } }

        /// <summary>The path used for batch operations.</summary>
        public virtual string BatchPath { get { return null; } }

        /// <inheritdoc/>
        public abstract IList<string> Features { get; }

        #endregion

        #endregion

        /// <inheritdoc/>
        public virtual void Dispose()
        {
            if (HttpClient != null)
            {
                HttpClient.Dispose();
            }
        }

        /// <summary>
        /// Retrieves the value of the environment variable with <paramref name="name"/>,
        /// mapping empty or whitespace-only strings to null.
        /// </summary>
        private static string GetNonWhiteSpaceOrNullEnvironmentVariable(string name)
        {
            var value = Environment.GetEnvironmentVariable(name);
            return string.IsNullOrWhiteSpace(value) ? null : value;
        }
    }
}