示例#1
0
        public void TestGetFieldValue()
        {
            Resource r1 = new Resource()
            {
                Foo = new Foo()
                {
                    Bar = new Bar()
                    {
                        Num = 5,
                    },
                    Num  = 25,
                    Text = "hello world",
                    Bars =
                    {
                        new Bar()
                        {
                            Num = 10
                        }
                    }
                },
                Foos =
                {
                    new Foo()
                    {
                        Num = 42
                    }
                },
                OptFoos = 45,
                Wrapper = "Test wrapper"
            };

            // Object references should be tested using AreSame.
            Assert.AreSame(FieldMasks.GetFieldValue("foo", r1), r1.Foo);
            Assert.AreSame(FieldMasks.GetFieldValue("foo.bar", r1), r1.Foo.Bar);
            Assert.AreSame(FieldMasks.GetFieldValue("foo.bars", r1), r1.Foo.Bars);
            Assert.AreSame(FieldMasks.GetFieldValue("foos", r1), r1.Foos);
            Assert.AreSame(FieldMasks.GetFieldValue("wrapper", r1), r1.Wrapper);

            // Value type should be tested using AreEqual.
            Assert.AreEqual(FieldMasks.GetFieldValue("foo.bar.num", r1), r1.Foo.Bar.Num);
            Assert.AreEqual(FieldMasks.GetFieldValue("foo.num", r1), r1.Foo.Num);
            Assert.AreEqual(FieldMasks.GetFieldValue("foo.text", r1), r1.Foo.Text);
            Assert.AreEqual(FieldMasks.GetFieldValue("opt_foos", r1), r1.OptFoos);

            Resource r2 = new Resource()
            {
                OptFoos = 32
            };

            Assert.Throws <ArgumentException>(delegate() {
                // r2.Foo is null.
                FieldMasks.GetFieldValue("foo.bar", r2);
            });
            Assert.Throws <ArgumentException>(delegate() {
                // r2.Jazz is not a valid field.
                FieldMasks.GetFieldValue("foo.jazz", r2);
            });
            Assert.Throws <ArgumentException>(delegate() {
                // r2.OptFoos.Blah cannot be recursed because OptFoos is a leaf level node.
                FieldMasks.GetFieldValue("foo.opt_foos.blah", r2);
            });
            Assert.Throws <ArgumentException>(delegate() {
                // YOu cannot recurse r2.Foos because it is a repeated field.
                FieldMasks.GetFieldValue("foo.opt_foos.blah", r2);
            });
        }
示例#2
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        // [START get_change_details]
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Get the GoogleAdsService.
            GoogleAdsServiceClient googleAdsService = client.GetService(
                Services.V10.GoogleAdsService);

            // Construct a query to find details for recent changes in your account.
            // The LIMIT clause is required for the change_event resource.
            // The maximum size is 10000, but a low limit was set here for demonstrative
            // purposes.
            // The WHERE clause on change_date_time is also required. It must specify a
            // window of at most 30 days within the past 30 days.

            string startDate   = DateTime.Today.Subtract(TimeSpan.FromDays(25)).ToString("yyyyMMdd");
            string endDate     = DateTime.Today.Add(TimeSpan.FromDays(1)).ToString("yyyyMMdd");
            string searchQuery = $@"
                SELECT
                    change_event.resource_name,
                    change_event.change_date_time,
                    change_event.change_resource_name,
                    change_event.user_email,
                    change_event.client_type,
                    change_event.change_resource_type,
                    change_event.old_resource,
                    change_event.new_resource,
                    change_event.resource_change_operation,
                    change_event.changed_fields
                FROM
                    change_event
                WHERE
                    change_event.change_date_time >= '{startDate}' AND
                    change_event.change_date_time <= '{endDate}'
                ORDER BY
                    change_event.change_date_time DESC
                LIMIT 5";

            try
            {
                // Issue a search request.
                googleAdsService.SearchStream(customerId.ToString(), searchQuery,
                                              delegate(SearchGoogleAdsStreamResponse resp)
                {
                    // Display the results.
                    foreach (GoogleAdsRow googleAdsRow in resp.Results)
                    {
                        ChangeEvent changeEvent     = googleAdsRow.ChangeEvent;
                        ChangedResource oldResource = changeEvent.OldResource;
                        ChangedResource newResource = changeEvent.NewResource;

                        bool knownResourceType     = true;
                        IMessage oldResourceEntity = null;
                        IMessage newResourceEntity = null;
                        switch (changeEvent.ChangeResourceType)
                        {
                        case ChangeEventResourceType.Ad:
                            oldResourceEntity = oldResource.Ad;
                            newResourceEntity = newResource.Ad;
                            break;

                        case ChangeEventResourceType.AdGroup:
                            oldResourceEntity = oldResource.AdGroup;
                            newResourceEntity = newResource.AdGroup;
                            break;

                        case ChangeEventResourceType.AdGroupAd:
                            oldResourceEntity = oldResource.AdGroupAd;
                            newResourceEntity = newResource.AdGroupAd;
                            break;

                        case ChangeEventResourceType.AdGroupAsset:
                            oldResourceEntity = oldResource.AdGroupAsset;
                            newResourceEntity = newResource.AdGroupAsset;
                            break;

                        case ChangeEventResourceType.AdGroupBidModifier:
                            oldResourceEntity = oldResource.AdGroupBidModifier;
                            newResourceEntity = newResource.AdGroupBidModifier;
                            break;

                        case ChangeEventResourceType.AdGroupCriterion:
                            oldResourceEntity = oldResource.AdGroupCriterion;
                            newResourceEntity = newResource.AdGroupCriterion;
                            break;

                        case ChangeEventResourceType.AdGroupFeed:
                            oldResourceEntity = oldResource.AdGroupFeed;
                            newResourceEntity = newResource.AdGroupFeed;
                            break;

                        case ChangeEventResourceType.Asset:
                            oldResourceEntity = oldResource.Asset;
                            newResourceEntity = newResource.Asset;
                            break;

                        case ChangeEventResourceType.AssetSet:
                            oldResourceEntity = oldResource.AssetSet;
                            newResourceEntity = newResource.AssetSet;
                            break;

                        case ChangeEventResourceType.AssetSetAsset:
                            oldResourceEntity = oldResource.AssetSetAsset;
                            newResourceEntity = newResource.AssetSetAsset;
                            break;

                        case ChangeEventResourceType.Campaign:
                            oldResourceEntity = oldResource.Campaign;
                            newResourceEntity = newResource.Campaign;
                            break;

                        case ChangeEventResourceType.CampaignAsset:
                            oldResourceEntity = oldResource.CampaignAsset;
                            newResourceEntity = newResource.CampaignAsset;
                            break;

                        case ChangeEventResourceType.CampaignAssetSet:
                            oldResourceEntity = oldResource.CampaignAssetSet;
                            newResourceEntity = newResource.CampaignAssetSet;
                            break;

                        case ChangeEventResourceType.CampaignBudget:
                            oldResourceEntity = oldResource.CampaignBudget;
                            newResourceEntity = newResource.CampaignBudget;
                            break;

                        case ChangeEventResourceType.CampaignCriterion:
                            oldResourceEntity = oldResource.CampaignCriterion;
                            newResourceEntity = newResource.CampaignCriterion;
                            break;

                        case ChangeEventResourceType.CampaignFeed:
                            oldResourceEntity = oldResource.CampaignFeed;
                            newResourceEntity = newResource.CampaignFeed;
                            break;

                        case ChangeEventResourceType.CustomerAsset:
                            oldResourceEntity = oldResource.CustomerAsset;
                            newResourceEntity = newResource.CustomerAsset;
                            break;

                        case ChangeEventResourceType.Feed:
                            oldResourceEntity = oldResource.Feed;
                            newResourceEntity = newResource.Feed;
                            break;

                        case ChangeEventResourceType.FeedItem:
                            oldResourceEntity = oldResource.FeedItem;
                            newResourceEntity = newResource.FeedItem;
                            break;

                        default:
                            knownResourceType = false;
                            break;
                        }

                        if (!knownResourceType)
                        {
                            Console.WriteLine($"Unknown change_resource_type " +
                                              $"'{changeEvent.ChangeResourceType}'.");
                            continue;
                        }

                        Console.WriteLine($"On #{changeEvent.ChangeDateTime}, user " +
                                          $"{changeEvent.UserEmail} used interface {changeEvent.ClientType} " +
                                          $"to perform a(n) '{changeEvent.ResourceChangeOperation}' " +
                                          $"operation on a '{changeEvent.ChangeResourceType}' with " +
                                          $"resource name {changeEvent.ChangeResourceName}.");

                        foreach (string fieldMaskPath in changeEvent.ChangedFields.Paths)
                        {
                            if (changeEvent.ResourceChangeOperation ==
                                ResourceChangeOperation.Create)
                            {
                                object newValue = FieldMasks.GetFieldValue(
                                    fieldMaskPath, newResourceEntity);
                                Console.WriteLine($"\t{fieldMaskPath} set to '{newValue}'.");
                            }
                            else if (changeEvent.ResourceChangeOperation ==
                                     ResourceChangeOperation.Update)
                            {
                                object oldValue = FieldMasks.GetFieldValue(fieldMaskPath,
                                                                           oldResourceEntity);
                                object newValue = FieldMasks.GetFieldValue(fieldMaskPath,
                                                                           newResourceEntity);

                                Console.WriteLine($"\t{fieldMaskPath} changed from " +
                                                  $"'{oldValue}' to '{newValue}'.");
                            }
                        }
                    }
                });
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }