/// <summary>
        /// Creates a service context that binds the service, callsettings and the client.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <returns>The service context.</returns>
        private GoogleAdsServiceContext CreateServiceContext(GoogleAdsConfig config)
        {
            GoogleAdsServiceContext serviceContext = new GoogleAdsServiceContext();
            CallSettings            callSettings   = CallSettings.FromCallTiming(
                CallTiming.FromRetry(new RetrySettings(
                                         retryBackoff : backoffSettings,
                                         timeoutBackoff : backoffSettings,
                                         totalExpiration : Expiration.FromTimeout(TimeSpan.FromMilliseconds(
                                                                                      config.Timeout)),
                                         retryFilter : retryFilter
                                         )))
                                                     .WithHeader(GoogleAdsConfig.DEVELOPER_TOKEN_KEYNAME, config.DeveloperToken)
                                                     .WithResponseMetadataHandler(delegate(Metadata metadata)
            {
                GoogleAdsResponseMetadata responseMetadata = new GoogleAdsResponseMetadata(metadata);
                serviceContext.OnResponseMetadataReceived(responseMetadata);
            });

            if (!string.IsNullOrEmpty(config.LoginCustomerId))
            {
                callSettings.WithHeader("login-customer-id", config.LoginCustomerId);
            }

            serviceContext.CallSettings = callSettings;
            return(serviceContext);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets an instance of the specified service.
        /// </summary>
        /// <param name="serviceTemplate">The service template.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>A service instance.</returns>
        internal TService GetService <TService, TServiceSetting>(
            ServiceTemplate <TService, TServiceSetting> serviceTemplate, GoogleAdsConfig config)
            where TServiceSetting : ServiceSettingsBase, new()
            where TService : GoogleAdsServiceClientBase
        {
            Channel     channel     = CreateChannel(config);
            CallInvoker callInvoker = channel.Intercept(
                LoggingInterceptor.GetInstance(config));

            // Build a service context to bind the service, configuration and CallSettings.
            GoogleAdsServiceContext serviceContext = new GoogleAdsServiceContext();

            // Build the call settings.
            CallSettings callSettings = CreateCallSettings <TServiceSetting>(config,
                                                                             serviceContext);

            serviceContext.CallSettings = callSettings;

            // Create the service settings.
            TServiceSetting serviceSettings = CreateServiceSettings <TServiceSetting>(
                serviceContext);

            // Create the service.
            TService service = Create(serviceTemplate, callInvoker, serviceSettings);

            serviceContext.Service = service;
            service.ServiceContext = serviceContext;
            return(service);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates the service settings.
        /// </summary>
        /// <typeparam name="TServiceSetting">The type of the service setting.</typeparam>
        /// <param name="serviceContext">The service context.</param>
        /// <returns>A service setting object, to be used for creating the service.</returns>
        private TServiceSetting CreateServiceSettings <TServiceSetting>(
            GoogleAdsServiceContext serviceContext)
            where TServiceSetting : ServiceSettingsBase, new()
        {
            TServiceSetting serviceSettings = new TServiceSetting();

            SetAllPropertySettings(serviceSettings, serviceContext.CallSettings);
            SetAllPropertySettings(serviceSettings, serviceContext.OperationsSettings);
            return(serviceSettings);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates the call settings.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="serviceContext">The service context.</param>
        /// <returns>The call settings.</returns>
        private CallSettings CreateCallSettings <TServiceSetting>(GoogleAdsConfig config,
                                                                  GoogleAdsServiceContext serviceContext)
            where TServiceSetting : ServiceSettingsBase, new()
        {
            // Get the default call settings from the generated stubs.
            CallSettings callSettings = new TServiceSetting().CallSettings;

            // Override various parameters with configuration parameters.
            callSettings = UpdateCallSettingsWithConfigParameters(callSettings,
                                                                  config, serviceContext);

            return(callSettings);
        }
        /// <summary>
        /// Gets an instance of the specified service.
        /// </summary>
        /// <param name="serviceTemplate">The service template.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>A service instance.</returns>
        internal TService GetService <TService, TServiceSetting>(
            ServiceTemplate <TService, TServiceSetting> serviceTemplate, GoogleAdsConfig config)
            where TServiceSetting : ServiceSettingsBase, new()
            where TService : GoogleAdsServiceClientBase
        {
            Channel     channel     = CreateChannel(config);
            CallInvoker callInvoker = channel.Intercept(
                LoggingInterceptor.GetInstance(config));

            GoogleAdsServiceContext serviceContext  = CreateServiceContext(config);
            TServiceSetting         serviceSettings = CreateServiceSettings <TServiceSetting>(
                serviceContext);
            TService service = Create <TService, TServiceSetting>(
                callInvoker, serviceSettings);

            serviceContext.Service = service;
            service.ServiceContext = serviceContext;
            return(service);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets an instance of the specified service.
        /// </summary>
        /// <param name="serviceTemplate">The service template.</param>
        /// <param name="config">The configuration.</param>
        /// <returns>A service instance.</returns>
        internal TService GetService <TService, TServiceSetting>(
            ServiceTemplate <TService, TServiceSetting, GoogleAdsConfig> serviceTemplate,
            GoogleAdsConfig config)
            where TServiceSetting : ServiceSettingsBase, new()
            where TService : GoogleAdsServiceClientBase
        {
            ChannelBase channel            = CreateChannel(config);
            CallInvoker interceptedInvoker = channel
                                             .Intercept(new GoogleAdsGrpcInterceptor(config));

            CallInvoker callInvoker = config.EnableProfiling ?
                                      new ProfilingCallInvoker(interceptedInvoker, config) : interceptedInvoker;

            // Build a service context to bind the service, configuration and CallSettings.
            GoogleAdsServiceContext serviceContext = new GoogleAdsServiceContext();

            // Build the call settings.
            CallSettings callSettings = CreateCallSettings <TServiceSetting>(config,
                                                                             serviceContext);

            serviceContext.CallSettings = callSettings;

            // Create the service settings.
            TServiceSetting serviceSettings =
                CreateServiceSettings <TServiceSetting, GoogleAdsConfig>(serviceContext);

            serviceSettings.VersionHeaderBuilder.AppendAssemblyVersion("gccl", this.GetType());
            serviceSettings.VersionHeaderBuilder.AppendAssemblyVersion("protobuf",
                                                                       typeof(IMessage));

            // Create the service.
            TService service = Create(serviceTemplate, callInvoker, serviceSettings);

            serviceContext.Service             = service;
            service.ServiceContext             = serviceContext;
            service.ServiceContext.ChannelBase = channel;
            return(service);
        }
        /// <summary>
        /// Creates the service settings.
        /// </summary>
        /// <typeparam name="TServiceSetting">The type of the service setting.</typeparam>
        /// <param name="serviceContext">The service context.</param>
        /// <returns>A service setting object, to be used for creating the service.</returns>
        private TServiceSetting CreateServiceSettings <TServiceSetting>(
            GoogleAdsServiceContext serviceContext)
            where TServiceSetting : ServiceSettingsBase, new()
        {
            TServiceSetting serviceSettings = new TServiceSetting();

            // Service settings have a client-specific callsetting, and
            // one callsetting per method. Since these are all kept separate
            // and not merged, we will instead set each of them using reflection.
            PropertyInfo[] callSettingProperties = serviceSettings.GetType()
                                                   .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                                   .Where(delegate(PropertyInfo x)
            {
                return(x.PropertyType == typeof(CallSettings) && x.Name != "CallSettings");
            }
                                                          ).ToArray();

            foreach (PropertyInfo pi in callSettingProperties)
            {
                pi.SetValue(serviceSettings, serviceContext.CallSettings);
            }
            return(serviceSettings);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Updates the call settings with configuration parameters.
        /// </summary>
        /// <param name="callSettings">The call settings.</param>
        /// <param name="config">The configuration.</param>
        /// <param name="serviceContext">The service context.</param>
        /// <returns></returns>
        private CallSettings UpdateCallSettingsWithConfigParameters(CallSettings callSettings,
                                                                    GoogleAdsConfig config, GoogleAdsServiceContext serviceContext)
        {
            callSettings = callSettings.WithHeader(GoogleAdsConfig.DEVELOPER_TOKEN_KEYNAME,
                                                   config.DeveloperToken)
                           .WithResponseMetadataHandler(delegate(Metadata metadata)
            {
                GoogleAdsResponseMetadata responseMetadata =
                    new GoogleAdsResponseMetadata(metadata);
                serviceContext.OnResponseMetadataReceived(responseMetadata);
            });

            if (!string.IsNullOrEmpty(config.LoginCustomerId))
            {
                callSettings = callSettings.WithHeader(GoogleAdsConfig.LOGIN_CUSTOMER_ID_KEYNAME,
                                                       config.LoginCustomerId);
            }

            if (!string.IsNullOrEmpty(config.LibraryIdentifierOverride))
            {
                callSettings = callSettings.WithHeader(GoogleAdsConfig.LIBRARY_IDENTIFIER_KEYNAME,
                                                       config.LibraryIdentifierOverride);
            }

            callSettings = callSettings.WithCallTiming(CallTiming.FromTimeout(
                                                           TimeSpan.FromMilliseconds(config.Timeout)));

            return(callSettings);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Updates the call settings with configuration parameters.
        /// </summary>
        /// <param name="callSettings">The call settings.</param>
        /// <param name="config">The configuration.</param>
        /// <param name="serviceContext">The service context.</param>
        /// <returns></returns>
        private CallSettings UpdateCallSettingsWithConfigParameters(CallSettings callSettings,
                                                                    GoogleAdsConfig config, GoogleAdsServiceContext serviceContext)
        {
            callSettings = callSettings.WithHeader(MetadataKeyNames.DeveloperToken,
                                                   config.DeveloperToken)
                           .WithResponseMetadataHandler(delegate(Metadata metadata)
            {
                AdsResponseMetadata responseMetadata = new AdsResponseMetadata(metadata);
                serviceContext.OnResponseMetadataReceived(responseMetadata);
            });

            if (!string.IsNullOrEmpty(config.LoginCustomerId))
            {
                callSettings = callSettings.WithHeader(MetadataKeyNames.LoginCustomerId,
                                                       config.LoginCustomerId);
            }

            if (!string.IsNullOrEmpty(config.LinkedCustomerId))
            {
                callSettings = callSettings.WithHeader(MetadataKeyNames.LinkedCustomerId,
                                                       config.LinkedCustomerId);
            }

            if (!string.IsNullOrEmpty(config.LibraryIdentifierOverride))
            {
                callSettings = callSettings.WithHeader(MetadataKeyNames.LibraryIdentifier,
                                                       config.LibraryIdentifierOverride);
            }

            callSettings = callSettings.WithExpiration(Expiration.FromTimeout(
                                                           TimeSpan.FromMilliseconds(config.Timeout)));

            return(callSettings);
        }
        /// <summary>
        /// Creates the call settings.
        /// </summary>
        /// <typeparam name="TServiceSetting">The type of the service setting.</typeparam>
        /// <param name="userCallSettings">The user call settings.</param>
        /// <param name="config">The configuration.</param>
        /// <param name="serviceContext">The service context.</param>
        /// <returns>The call settings.</returns>
        private CallSettings CreateCallSettings <TServiceSetting>(CallSettings userCallSettings,
                                                                  GoogleAdsConfig config, GoogleAdsServiceContext serviceContext)
            where TServiceSetting : ServiceSettingsBase, new()
        {
            // Get the default call settings from the generated stubs.
            CallSettings callSettings = new TServiceSetting().CallSettings;

            // Override various parameters with configuration parameters.
            callSettings = UpdateCallSettingsWithConfigParameters(callSettings,
                                                                  config, serviceContext);

            // If the user has provided a call settings at the runtime, then override specific
            // settings from there.
            if (userCallSettings != null)
            {
                callSettings = callSettings.MergedWith(userCallSettings);
            }

            return(callSettings);
        }