Пример #1
0
        /// <summary>Snippet for SearchStream</summary>
        public async Task SearchStreamRequestObject()
        {
            // Snippet: SearchStream(SearchGoogleAdsStreamRequest, CallSettings)
            // Create client
            GoogleAdsServiceClient googleAdsServiceClient = GoogleAdsServiceClient.Create();
            // Initialize request argument(s)
            SearchGoogleAdsStreamRequest request = new SearchGoogleAdsStreamRequest
            {
                CustomerId        = "",
                Query             = "",
                SummaryRowSetting = SummaryRowSettingEnum.Types.SummaryRowSetting.Unspecified,
            };

            // Make the request, returning a streaming response
            GoogleAdsServiceClient.SearchStreamStream response = googleAdsServiceClient.SearchStream(request);

            // Read streaming responses from server until complete
            // Note that C# 8 code can use await foreach
            AsyncResponseStream <SearchGoogleAdsStreamResponse> responseStream = response.GetResponseStream();

            while (await responseStream.MoveNextAsync())
            {
                SearchGoogleAdsStreamResponse responseItem = responseStream.Current;
                // Do something with streamed response
            }
            // The response stream has completed
            // End snippet
        }
        /// <summary>
        /// Runs a streaming search query and returns all rows that matches the query.
        /// </summary>
        /// <param name="request">The search request.</param>
        /// <param name="responseCallback">The callback that will be called for each
        /// <see cref="SearchGoogleAdsStreamResponse"/> returned by the server.</param>
        /// <param name="callSettings">The call settings to customize this API call.</param>
        /// <returns>The search response task.</returns>
        /// <remarks>This method reads the response stream and invokes the callback for each
        /// <see cref="SearchGoogleAdsStreamResponse"/> object in the response, you cannot
        /// iterate further on the <see cref="SearchStreamStream"/> object to obtain results. You
        /// can however perform additional tasks like retrieving the trailing metadata. If you do
        /// not care about this additional information, it is fine to ignore the return value
        /// of this method.</remarks>
        public virtual Task <SearchStreamStream> SearchStreamAsync(
            SearchGoogleAdsStreamRequest request,
            Action <SearchGoogleAdsStreamResponse> responseCallback,
            CallSettings callSettings = null)
        {
            SearchStreamStream searchStream = this.SearchStream(request, callSettings);

            // Issue a search request.
            Task <SearchStreamStream> t = Task.Run(async() =>
            {
                var responseStream = searchStream.GetResponseStream();
                bool emptyResult   = true;
                while (await responseStream.MoveNextAsync())
                {
                    emptyResult = false;
                    SearchGoogleAdsStreamResponse resp = responseStream.Current;
                    responseCallback(resp);
                }
                // Invoke the callback at least once to avoid confusion when there are no results
                // and no errors.
                if (emptyResult)
                {
                    responseCallback(new SearchGoogleAdsStreamResponse());
                }
                return(searchStream);
            });

            return(t);
        }
        /// <summary>
        /// Makes a server streaming call using a custom client timeout.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        // [START set_custom_client_timeouts]
        private void MakeServerStreamingCall(GoogleAdsClient client, long customerId)
        {
            StringWriter writer = new StringWriter();

            // Set a custom timeout.
            CallSettings callSettings = CallSettings.FromExpiration(
                Expiration.FromTimeout(TimeSpan.FromMilliseconds(CLIENT_TIMEOUT_MILLIS)));

            try
            {
                // Get the GoogleAdsService.
                GoogleAdsServiceClient googleAdsService = client.GetService(
                    Services.V10.GoogleAdsService);

                string query = "SELECT campaign.id FROM campaign";
                SearchGoogleAdsStreamRequest request = new SearchGoogleAdsStreamRequest()
                {
                    CustomerId = customerId.ToString(),
                    Query      = query
                };
                // Issue a search request.
                googleAdsService.SearchStream(request,
                                              delegate(SearchGoogleAdsStreamResponse resp)
                {
                    // Iterates over all rows in all messages and collects the campaign IDs.
                    foreach (GoogleAdsRow googleAdsRow in resp.Results)
                    {
                        writer.WriteLine(googleAdsRow.Campaign.Id);
                    }
                },
                                              callSettings
                                              );
                Console.WriteLine("The server streaming call completed before the timeout");
            }
            catch (GoogleAdsException ex)
            {
                if (ex.StatusCode == StatusCode.DeadlineExceeded)
                {
                    Console.WriteLine("The server streaming call did not complete before the " +
                                      "timeout.");
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                Console.WriteLine($"All campaign IDs retrieved: {writer}.");
            }
        }
        /// <summary>
        /// Runs a streaming search query and returns all rows that matches the query.
        /// </summary>
        /// <param name="customerId">ID of the customer for which the query is run.</param>
        /// <param name="query">The query to run.</param>
        /// <param name="responseCallback">The callback that will be called for each
        /// <see cref="SearchGoogleAdsStreamResponse"/> returned by the server.</param>
        /// <param name="summaryRowSetting">The summary row setting.</param>
        /// <param name="callSettings">The call settings to customize this API call.</param>
        /// <returns>The search response task.</returns>
        /// <remarks>This method reads the response stream and invokes the callback for each
        /// <see cref="SearchGoogleAdsStreamResponse"/> object in the response, you cannot
        /// iterate further on the <see cref="SearchStreamStream"/> object to obtain results. You
        /// can however perform additional tasks like retrieving the trailing metadata. If you do
        /// not care about this additional information, it is fine to ignore the return value
        /// of this method.</remarks>
        public virtual Task <SearchStreamStream> SearchStreamAsync(
            string customerId,
            string query,
            Action <SearchGoogleAdsStreamResponse> responseCallback,
            SummaryRowSetting summaryRowSetting = SummaryRowSetting.NoSummaryRow,
            CallSettings callSettings           = null)
        {
            var request = new SearchGoogleAdsStreamRequest()
            {
                CustomerId        = customerId.ToString(),
                Query             = query,
                SummaryRowSetting = summaryRowSetting,
            };

            return(SearchStreamAsync(request, responseCallback, callSettings));
        }
        /// <summary>
        /// Runs a streaming search query and returns all rows that matches the query.
        /// </summary>
        /// <param name="request">The search request.</param>
        /// <param name="responseCallback">The response callback, which will be called
        /// for each <see cref="SearchGoogleAdsStreamResponse"/> in the response stream.</param>
        /// <param name="callSettings">The call settings to customize this API call.</param>
        /// <returns>The <see cref="SearchStreamStream"/> object for further processing.</returns>
        /// <remarks>This method reads the response stream and invokes the callback for each
        /// <see cref="SearchGoogleAdsStreamResponse"/> object in the response, you cannot
        /// iterate further on the <see cref="SearchStreamStream"/> object to obtain results. You
        /// can however perform additional tasks like retrieving the trailing metadata. If you do
        /// not care about this additional information, it is fine to ignore the return value
        /// of this method.</remarks>
        public virtual SearchStreamStream SearchStream(SearchGoogleAdsStreamRequest request,
                                                       Action <SearchGoogleAdsStreamResponse> responseCallback,
                                                       CallSettings callSettings = null)
        {
            try
            {
                Task <SearchStreamStream> t = SearchStreamAsync(request, responseCallback,
                                                                callSettings);
                // Since we call Task.Run() in SearchStreamAsync, it is safe to call
                // t.Result here without causing a deadlock. Also, no need to call t.Wait(),
                // because t.Result is blocking in nature.
                return(t.Result);
            }
            catch (AggregateException ae)
            {
                GoogleAdsException gae = GoogleAdsException.FromTaskException(ae);

                if (gae != null)
                {
                    throw gae;
                }
                throw;
            }
        }