public static async Task <dynamic> ReturnFacebookAdsDTO(AllCampaignsRoot allCampaigns, HttpClient client) { List <CampaignDTO> campaignDTOList = new List <CampaignDTO>(); List <string> errors = new List <string>(); for (int i = 0; i < allCampaigns.Data.Count; i++) { try { InsightsRoot insightsRoot = await GetCampaignInsights(allCampaigns.Data[i].Id, client); if (insightsRoot.Data.Any()) { campaignDTOList.Add(new CampaignDTO(insightsRoot)); } } catch (Exception ex) { errors.Add(ex.Message); } } return(new FacebookAdsDTO(campaignDTOList, errors)); }
public static async Task <dynamic> GetAllCampaigns(string companyName, Entities db, HttpClient client) { string alias; try { alias = AliasMethods.GetAlias(companyName, db, "FacebookAdAccountID"); } catch { return(new ArgumentException($"No HubSpot alias found for the company name {companyName}")); } string url = $"{ConfigDictionary.Config()["FacebookAPIURLRoot"]}/{ConfigDictionary.Config()["FacebookAPIVersion"]}/act_{alias}/campaigns?access_token={ConfigDictionary.Config()["FacebookAccessToken"]}"; try { HttpResponseMessage response = await client.GetAsync(url); string responseData = await response.Content.ReadAsStringAsync(); AllCampaignsRoot allCampaignsRoot = JsonConvert.DeserializeObject <AllCampaignsRoot>(responseData); return(allCampaignsRoot); } catch (HttpRequestException ex) { return(ex); } }
public async Task <dynamic> Get(string companyName) { if (CheckClientSecret()) { using (HttpClient client = new HttpClient()) using (Entities db = new Entities()) { try { AllCampaignsRoot allCampaignsRoot = await CampaignsMethods.GetAllCampaigns(companyName, db, client); return(await InsightsMethods.ReturnFacebookAdsDTO(allCampaignsRoot, client)); } catch (Exception ex) { return(ex); } } } else { return(new HttpResponseMessage(HttpStatusCode.Forbidden)); } }