public HttpResponseMessage UpdateGeoTargets(string campaignId, UpdateGeoTargetsRequest updateGeoTargetsRequest) { try { CampaignControllerPut put = new CampaignControllerPut(); UpdateGeoTargetsResponse updatedTargets = put.UpdateCampaignGeoTargets(campaignId, updateGeoTargetsRequest); return(ResponseHelper.CreateResponse(Request, HttpStatusCode.OK, updatedTargets, PublicApiCommonErrorCode.SUCCESS)); } catch (Exception e) { return(ResponseHelper.CreateResponse(Request, HttpStatusCode.NotFound, e.Message + " " + e.InnerException + " " + e.StackTrace, PublicApiCommonErrorCode.NOT_FOUND)); } }
public UpdateGeoTargetsResponse UpdateCampaignGeoTargets(string campaignId, UpdateGeoTargetsRequest updateGeoTargetsRequest) { UpdateGeoTargetsResponse updateResponse = new UpdateGeoTargetsResponse() { Success = true }; Operator action = Operator.ADD; if (updateGeoTargetsRequest.UpdateMode == UpdateMode.Remove) { action = Operator.REMOVE; } List <KeyValuePair <string, string> > pairs = ExtractCityStatePairsFromRequets(updateGeoTargetsRequest); CampaignCriterionService campaignCriterionService = (CampaignCriterionService)_adwordsUser.GetService(AdWordsService.v201708.CampaignCriterionService); List <GeoTarget> targets = _locationNameHelper.GetTargetIdsByLocationNames(pairs); List <CampaignCriterionOperation> operations = new List <CampaignCriterionOperation>(); foreach (GeoTarget target in targets) { Location location = new Location() { id = Convert.ToInt64(target.Id) }; CampaignCriterionOperation operation = new CampaignCriterionOperation(); CampaignCriterion campaignCriterion = new CampaignCriterion(); campaignCriterion.campaignId = Convert.ToInt64(campaignId); campaignCriterion.criterion = location; campaignCriterion.CampaignCriterionType = "Location"; operation.operand = campaignCriterion; operation.@operator = action; operations.Add(operation); } try { CampaignCriterionReturnValue result = campaignCriterionService.mutate(operations.ToArray()); } catch (Exception ex) { var exception = ex.Message; updateResponse.Success = false; } return(updateResponse); }
public UpdateGeoTargetsResponse UpdateCampaignGeoTargets(string campaignId, UpdateGeoTargetsRequest updateGeoTargetsRequest) { if (string.IsNullOrWhiteSpace(campaignId)) { throw new ValidationException("CampaignId is required."); } try { return(_adwordsClient.UpdateCampaignGeoTargets(campaignId, updateGeoTargetsRequest)); } catch (Exception ex) { throw new ValidationException(ex.Message); } }
public void UpdateGeoTargetsPut_RemoveTarget() { CampaignControllerPut controller = new CampaignControllerPut(); List <GeoLocation> list = new List <GeoLocation>(); list.Add(new GeoLocation() { City = "Miami", State = "Florida" }); UpdateGeoTargetsRequest request = new UpdateGeoTargetsRequest() { GeoLocation = list, UpdateMode = UpdateMode.Remove }; UpdateGeoTargetsResponse response = controller.UpdateCampaignGeoTargets("927060915", request); Assert.That(response.Success, Is.True); }
public void UpdateGeoTargetsPut_EmptyGeoLocationList() { CampaignControllerPut controller = new CampaignControllerPut(); List <GeoLocation> list = new List <GeoLocation>(); UpdateGeoTargetsRequest request = new UpdateGeoTargetsRequest() { GeoLocation = list, UpdateMode = UpdateMode.Remove }; try { UpdateGeoTargetsResponse response = controller.UpdateCampaignGeoTargets("927060915", request); Assert.That(false); } catch (Exception e) { Assert.That(e.Message, Is.Not.Empty); } }
public bool SendAdservicesTargetsPost(List <KeyValuePair <string, string> > targets, UpdateMode updateMode) { bool success = false; string url = String.Format(_config.Update.AdwordsTargetsUrl, _config.Update.AdwordsCampaignId); List <GeoLocation> targetsList = new List <GeoLocation>(); foreach (KeyValuePair <string, string> pair in targets) { targetsList.Add(new GeoLocation() { City = pair.Key, State = pair.Value }); } UpdateGeoTargetsRequest request = new UpdateGeoTargetsRequest() { GeoLocation = targetsList, UpdateMode = updateMode }; using (HttpClient client = new HttpClient()) { try { var response = client.PostAsJsonAsync(url, request); string resultString = response.Result.Content.ReadAsStringAsync().Result; var json = JsonConvert.DeserializeObject <dynamic>(resultString); if (json.data.Success == "true") { success = true; } } catch (Exception e) { _logHelper.WriteError(e.Message); } } return(success); }
private List <KeyValuePair <string, string> > ExtractCityStatePairsFromRequets(UpdateGeoTargetsRequest updateGeoTargetsRequest) { List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >(); foreach (GeoLocation pair in updateGeoTargetsRequest.GeoLocation) { pairs.Add(new KeyValuePair <string, string>(pair.City, pair.State)); } return(pairs); }