public IActionResult CreateCustomAudienceIntegration([FromBody] FacebookAudienceManager facebookAudienceManager) { FacebookAudienceResponse result = new FacebookAudienceResponse(); SiteAudience siteAudience = new SiteAudience(); try { result = _audienceManagerService.CreateCustomAudienceIntegration(facebookAudienceManager); if (result.Error == null) { siteAudience.SiteId = facebookAudienceManager.SiteId; siteAudience.AudienceId = result.AudienceId; siteAudience.AudienceName = result.AudienceName; siteAudience.RetentionDays = result.RetentionDays; siteAudience.CreatedDate = DateTimeExtensions.ToDateTimeFromEpoch(result.CreatedDate); siteAudience.UpdatedDate = DateTimeExtensions.ToDateTimeFromEpoch(result.UpdatedDate); _repoWrapper.SiteAudience.Create(siteAudience); _repoWrapper.Save(); } else { throw new Exception(result.Error.Type + "|message-" + result.Error.Message + "|fbtrace_id-" + result.Error.Type); } } catch (Exception ex) { this._logger.LogError(ex.Message); } return(new JsonResult(siteAudience)); }
public FacebookAudienceResponse CreateCustomAudienceIntegration(FacebookAudienceManager audienceManager) { if (audienceManager.Account == null) { throw new Exception("null account"); } if (audienceManager.AddressId < 1) { throw new Exception("invalid addressId"); } if (audienceManager.SiteId < 1) { throw new Exception("invalid siteId"); } if (string.IsNullOrWhiteSpace(audienceManager.Description)) { throw new Exception("invalid Description"); } if (string.IsNullOrWhiteSpace(audienceManager.Name)) { throw new Exception("invalid Name"); } if (string.IsNullOrWhiteSpace(audienceManager.RetentionDays)) { throw new Exception("invalid RetentionDays"); } if (string.IsNullOrWhiteSpace(audienceManager.Account.Id)) { throw new Exception("invalid Id (ad account id)"); } return(_audienceManagerThirdParty.CreateCustomAudienceIntegration(audienceManager).Result); }
public async Task <FacebookAudienceResponse> CreateCustomAudienceIntegration(FacebookAudienceManager audienceManager) { if (audienceManager?.Account == null) { throw new Exception("null audienceManager or audienceManager.Account"); } var facebookAuthenticaionHelper = new FacebookAuthenticationHelper(_configuration); var token = await facebookAuthenticaionHelper.GetAccessTokenForFacebookMilestonesAsync() .ConfigureAwait(false); HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var stringContent = new StringContent(JsonConvert.SerializeObject(audienceManager), Encoding.UTF8, "application/json"); var response = await HttpClient.PostAsync("https://iapi-teamtardis.dealersocket.engineering/webapi/facebookAudienceManager", stringContent).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var stringToParse = JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); var customAudience = JsonConvert.DeserializeObject <FacebookAudienceResponse>(stringToParse); return(customAudience); } throw new Exception( $"Reason: {response.ReasonPhrase}, Content: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)}"); }