-
Notifications
You must be signed in to change notification settings - Fork 199
/
LookupLocation.cs
executable file
·144 lines (130 loc) · 5.53 KB
/
LookupLocation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
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
// Copyright 2018 Google LLC
//
// 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.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201806;
using System;
using System.Collections.Generic;
namespace Google.Api.Ads.AdWords.Examples.CSharp.v201806
{
/// <summary>
/// This code example gets location criteria by name.
/// </summary>
public class LookupLocation : ExampleBase
{
/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
/// <param name="args">The command line arguments.</param>
public static void Main(string[] args)
{
LookupLocation codeExample = new LookupLocation();
Console.WriteLine(codeExample.Description);
try
{
codeExample.Run(new AdWordsUser());
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while running this code example. {0}",
ExampleUtilities.FormatException(e));
}
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description
{
get { return "This code example gets location criteria by name."; }
}
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="user">The AdWords user.</param>
public void Run(AdWordsUser user)
{
using (LocationCriterionService locationCriterionService =
(LocationCriterionService) user.GetService(AdWordsService.v201806
.LocationCriterionService))
{
string[] locationNames = new string[]
{
"Paris",
"Quebec",
"Spain",
"Deutschland"
};
Selector selector = new Selector()
{
fields = new string[]
{
Location.Fields.Id,
Location.Fields.LocationName,
LocationCriterion.Fields.CanonicalName,
Location.Fields.DisplayType,
Location.Fields.ParentLocations,
LocationCriterion.Fields.Reach,
Location.Fields.TargetingStatus
},
predicates = new Predicate[]
{
// Location names must match exactly, only EQUALS and IN are supported.
Predicate.In(Location.Fields.LocationName, locationNames),
// Set the locale of the returned location names.
Predicate.Equals(LocationCriterion.Fields.Locale, "en")
}
};
try
{
// Make the get request.
LocationCriterion[] locationCriteria = locationCriterionService.get(selector);
// Display the resulting location criteria.
foreach (LocationCriterion locationCriterion in locationCriteria)
{
string parentLocations = "N/A";
if (locationCriterion.location != null &&
locationCriterion.location.parentLocations != null)
{
List<string> parentLocationList = new List<string>();
foreach (Location location in locationCriterion.location.parentLocations
)
{
parentLocationList.Add(GetLocationString(location));
}
parentLocations = string.Join(", ", parentLocationList);
}
Console.WriteLine(
"The search term '{0}' returned the location '{1}' of type '{2}' " +
"with parent locations '{3}', reach '{4}' and targeting status '{5}.",
locationCriterion.searchTerm, locationCriterion.location.locationName,
locationCriterion.location.displayType, parentLocations,
locationCriterion.reach, locationCriterion.location.targetingStatus);
}
}
catch (Exception e)
{
throw new System.ApplicationException("Failed to get location criteria.", e);
}
}
}
/// <summary>
/// Gets a string representation for a location.
/// </summary>
/// <param name="location">The location</param>
/// <returns>The string representation</returns>
public string GetLocationString(Location location)
{
return string.Format("{0} ({1})", location.locationName, location.displayType);
}
}
}