/// <summary> /// Gets the quota usage of an account in units, broken down by /// method name. /// </summary> /// <param name="user">The AdWordsUser object for which the quota usage /// should be retrieved.</param> /// <param name="startDate">Start date for the date range for which /// results are to be retrieved.</param> /// <param name="endDate">End date for the date range for which results /// are to be retrieved.</param> /// <returns>A list of MethodQuotaUsage objects, with one entry for each /// method.</returns> public static List<MethodQuotaUsage> GetMethodQuotaUsage(AdWordsUser user, DateTime startDate, DateTime endDate) { List<MethodQuotaUsage> methodQuotaUsageList = new List<MethodQuotaUsage>(); SortedList<string, List<string>> serviceToMethodsMap = GetAllMethods(); InfoService service = (InfoService) user.GetService(AdWordsService.v201109.InfoService); foreach (string serviceName in serviceToMethodsMap.Keys) { List<string> methods = serviceToMethodsMap[serviceName]; foreach (string methodName in methods) { InfoSelector selector = new InfoSelector(); selector.apiUsageTypeSpecified = true; selector.apiUsageType = ApiUsageType.UNIT_COUNT; selector.dateRange = new DateRange(); selector.dateRange.min = startDate.ToString("YYYYMMDD"); selector.dateRange.max = endDate.ToString("YYYYMMDD"); selector.serviceName = serviceName; if (methodName.Contains(".")) { string[] splits = methodName.Split('.'); selector.methodName = splits[0]; selector.operatorSpecified = true; selector.@operator = (Operator) Enum.Parse(typeof(Operator), splits[1]); } else { selector.methodName = methodName; } methodQuotaUsageList.Add(new MethodQuotaUsage(serviceName, methodName, service.get(selector).cost)); } } return methodQuotaUsageList; }
/// <summary> /// Gets the quota usage for client accounts. /// </summary> /// <param name="user">The AdWordsUser object for which the quota usage /// should be retrieved.</param> /// <param name="startDate">Start date for the date range for which /// results are to be retrieved.</param> /// <param name="endDate">End date for the date range for which results are /// to be retrieved.</param> /// <returns>A ClientQuotaUsage object that stores the API usage breakup. /// </returns> public static ClientQuotaUsage GetClientQuotaUsage(AdWordsUser user, DateTime startDate, DateTime endDate) { ClientQuotaUsage retVal = new ClientQuotaUsage(); AccountService accountService = (AccountService) user.GetService(AdWordsService.v13.AccountService); AdWordsAccount rootUser = new AdWordsAccount(); rootUser.Email = accountService.emailValue.Value[0]; rootUser.IsManager = true; Hashtable allUsers = new Hashtable(StringComparer.InvariantCultureIgnoreCase); BuildUserGraph(accountService, rootUser, allUsers); InfoService infoService = (InfoService) user.GetService(AdWordsService.v201109.InfoService); FetchUnitUsages(infoService, rootUser, startDate, endDate); foreach (string email in allUsers.Keys) { retVal.UsageMap[email] = GetUnits((AdWordsAccount)allUsers[email], allUsers); } InfoSelector selector = new InfoSelector(); selector.apiUsageTypeSpecified = true; selector.apiUsageType = ApiUsageType.UNIT_COUNT; selector.dateRange = new DateRange(); selector.dateRange.min = startDate.ToString("YYYYMMDD"); selector.dateRange.max = endDate.ToString("YYYYMMDD"); retVal.TotalUnits = infoService.get(selector).cost; retVal.DiffUnits = retVal.TotalUnits - retVal.UsageMap[rootUser.Email]; return retVal; }
/// <summary> /// Recursively walks the account graph hierarchy and fetches the unit /// usages for each account. /// </summary> /// <param name="infoService">InfoService instance to be used while making /// calls.</param> /// <param name="account">The account to be traversed recursively.</param> /// <param name="startDate">Start date for fetching API usage.</param> /// <param name="endDate">End date for fetching API usage.</param> private static void FetchUnitUsages(InfoService infoService, AdWordsAccount account, DateTime startDate, DateTime endDate) { string oldClientEmail = infoService.RequestHeader.clientEmail; startDate = new DateTime(2009, 1, 1); infoService.RequestHeader.clientEmail = account.Email; InfoSelector selector = new InfoSelector(); selector.apiUsageTypeSpecified = true; selector.apiUsageType = ApiUsageType.UNIT_COUNT_FOR_CLIENTS; selector.dateRange = new DateRange(); selector.dateRange.min = startDate.ToString("yyyyMMdd"); selector.dateRange.max = endDate.ToString("yyyyMMdd"); ApiUsageInfo usageInfo = infoService.get(selector); foreach (AdWordsAccount child in account.Children) { if (usageInfo.apiUsageRecords != null) { foreach (ApiUsageRecord usageRecord in usageInfo.apiUsageRecords) { if (child.Email == usageRecord.clientEmail) { child.Units = usageRecord.cost; break; } } } if (child.IsManager) { FetchUnitUsages(infoService, child, startDate, endDate); } } infoService.RequestHeader.clientEmail = oldClientEmail; }
public virtual ApiUsageInfo get(InfoSelector selector) { object[] results = this.Invoke("get", new object[] {selector}); return ((ApiUsageInfo) (results[0])); }