diff --git a/NewsAPI.Tests/App.config b/NewsAPI.Tests/App.config index 49cc43e..3dbff35 100644 --- a/NewsAPI.Tests/App.config +++ b/NewsAPI.Tests/App.config @@ -1,3 +1,3 @@ - + - \ No newline at end of file + diff --git a/NewsAPI.Tests/NewsAPI.Tests.csproj b/NewsAPI.Tests/NewsAPI.Tests.csproj index b38cd9a..b95942c 100644 --- a/NewsAPI.Tests/NewsAPI.Tests.csproj +++ b/NewsAPI.Tests/NewsAPI.Tests.csproj @@ -16,6 +16,7 @@ $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest + true @@ -36,7 +37,6 @@ - @@ -54,17 +54,17 @@ + + + Designer + + {86cdf033-3cae-4845-98bf-70575df27e2b} NewsAPI - - - Designer - - diff --git a/NewsAPI.Tests/Tests.cs b/NewsAPI.Tests/Tests.cs index 17c9076..6ec0412 100644 --- a/NewsAPI.Tests/Tests.cs +++ b/NewsAPI.Tests/Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using NewsAPI.Models; using System.Configuration; @@ -21,6 +22,20 @@ public void Init() NewsApiClient = new NewsApiClient(apiKey); } + [TestMethod] + public void EverythingRequestWithDomainsWorks() + { + var everythingRequest = new EverythingRequest + { + Domains = new List(new[] { "wsj.com", "nytimes.com" }) + }; + var result = NewsApiClient.GetEverything(everythingRequest); + Assert.AreEqual(Statuses.Ok, result.Status); + Assert.IsTrue(result.TotalResults > 0); + Assert.IsTrue(result.Articles.Count > 0); + Assert.IsNull(result.Error); + } + [TestMethod] public void BasicEverythingRequestWorks() { diff --git a/NewsAPI/Constants/Categories.cs b/NewsAPI/Constants/Categories.cs index 23d2d18..3eab2de 100644 --- a/NewsAPI/Constants/Categories.cs +++ b/NewsAPI/Constants/Categories.cs @@ -1,11 +1,5 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Text; -using System.Threading.Tasks; namespace NewsAPI.Constants { @@ -19,4 +13,4 @@ public enum Categories Sports, Technology } -} +} \ No newline at end of file diff --git a/NewsAPI/Constants/Countries.cs b/NewsAPI/Constants/Countries.cs index 3c06330..f206fbf 100644 --- a/NewsAPI/Constants/Countries.cs +++ b/NewsAPI/Constants/Countries.cs @@ -1,125 +1,143 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NewsAPI.Constants +namespace NewsAPI.Constants { public enum Countries { AE, + /// - /// Argentina + /// Argentina /// AR, + /// - /// Austria + /// Austria /// AT, + /// - /// Australia + /// Australia /// AU, + /// - /// Belgium + /// Belgium /// BE, BG, BR, + /// - /// Canada + /// Canada /// CA, CH, + /// - /// China + /// China /// CN, CO, CU, + /// - /// Czech Republic + /// Czech Republic /// CZ, + /// - /// Germany + /// Germany /// DE, + /// - /// Egypt + /// Egypt /// EG, + /// - /// France + /// France /// FR, + /// - /// United Kingdom + /// United Kingdom /// GB, + /// - /// Greece + /// Greece /// GR, + /// - /// Hong Kong + /// Hong Kong /// HK, + /// - /// Hungary + /// Hungary /// HU, ID, + /// - /// Ireland + /// Ireland /// IE, IL, IN, + /// - /// Italy + /// Italy /// IT, + /// - /// Japan + /// Japan /// JP, + /// - /// South Korea + /// South Korea /// KR, LT, LV, MA, + /// - /// Mexico + /// Mexico /// MX, MY, NG, + /// - /// Netherlands + /// Netherlands /// NL, + /// - /// Norway + /// Norway /// NO, + /// - /// New Zealand + /// New Zealand /// NZ, PH, PL, + /// - /// Portugal + /// Portugal /// PT, RO, RS, + /// - /// Russia + /// Russia /// RU, SA, @@ -131,11 +149,12 @@ public enum Countries TR, TW, UA, + /// - /// United States + /// United States /// US, VE, ZA } -} +} \ No newline at end of file diff --git a/NewsAPI/Constants/ErrorCodes.cs b/NewsAPI/Constants/ErrorCodes.cs index a2d20ed..61e275c 100644 --- a/NewsAPI/Constants/ErrorCodes.cs +++ b/NewsAPI/Constants/ErrorCodes.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NewsAPI.Constants +namespace NewsAPI.Constants { public enum ErrorCodes { @@ -24,4 +18,4 @@ public enum ErrorCodes UnexpectedError, UnknownError } -} +} \ No newline at end of file diff --git a/NewsAPI/Constants/Languages.cs b/NewsAPI/Constants/Languages.cs index ab43c02..5de1d04 100644 --- a/NewsAPI/Constants/Languages.cs +++ b/NewsAPI/Constants/Languages.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NewsAPI.Constants +namespace NewsAPI.Constants { public enum Languages { /// - /// Afrikaans (South Africa) + /// Afrikaans (South Africa) /// AF, AN, @@ -23,18 +17,21 @@ public enum Languages CS, CY, DA, + /// - /// German + /// German /// DE, EL, + /// - /// English + /// English /// EN, EO, + /// - /// Spanish + /// Spanish /// ES, ET, @@ -51,12 +48,14 @@ public enum Languages HY, ID, IS, + /// - /// Italian + /// Italian /// IT, + /// - /// Japanese + /// Japanese /// JP, JV, @@ -71,16 +70,18 @@ public enum Languages ML, MR, MS, + /// - /// Dutch + /// Dutch /// NL, NN, NO, OC, PL, + /// - /// Portuguese + /// Portuguese /// PT, RO, @@ -101,9 +102,10 @@ public enum Languages UR, VI, VO, + /// - /// Chinese + /// Chinese /// ZH } -} +} \ No newline at end of file diff --git a/NewsAPI/Constants/SortBys.cs b/NewsAPI/Constants/SortBys.cs index 18b8579..397fcd1 100644 --- a/NewsAPI/Constants/SortBys.cs +++ b/NewsAPI/Constants/SortBys.cs @@ -1,24 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NewsAPI.Constants +namespace NewsAPI.Constants { public enum SortBys { /// - /// Sort by publisher popularity + /// Sort by publisher popularity /// Popularity, + /// - /// Sort by article publish date (newest first) + /// Sort by article publish date (newest first) /// PublishedAt, + /// - /// Sort by relevancy to the Q param + /// Sort by relevancy to the Q param /// Relevancy } -} +} \ No newline at end of file diff --git a/NewsAPI/Constants/Statuses.cs b/NewsAPI/Constants/Statuses.cs index c9e6dd1..1aff89d 100644 --- a/NewsAPI/Constants/Statuses.cs +++ b/NewsAPI/Constants/Statuses.cs @@ -1,20 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NewsAPI.Constants +namespace NewsAPI.Constants { public enum Statuses { /// - /// Request was successful + /// Request was successful /// Ok, + /// - /// Request failed + /// Request failed /// Error } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/ApiResponse.cs b/NewsAPI/Models/ApiResponse.cs index 267b32a..7b32a46 100644 --- a/NewsAPI/Models/ApiResponse.cs +++ b/NewsAPI/Models/ApiResponse.cs @@ -1,18 +1,14 @@ -using NewsAPI.Constants; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; +using NewsAPI.Constants; namespace NewsAPI.Models { internal class ApiResponse { - public Statuses Status { get; set; } + public List
Articles { get; set; } public ErrorCodes? Code { get; set; } public string Message { get; set; } - public List
Articles { get; set; } + public Statuses Status { get; set; } public int TotalResults { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/Article.cs b/NewsAPI/Models/Article.cs index 9f26c60..ccebc8f 100644 --- a/NewsAPI/Models/Article.cs +++ b/NewsAPI/Models/Article.cs @@ -1,19 +1,15 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace NewsAPI.Models { public class Article { - public Source Source { get; set; } public string Author { get; set; } - public string Title { get; set; } public string Description { get; set; } + public DateTime? PublishedAt { get; set; } + public Source Source { get; set; } + public string Title { get; set; } public string Url { get; set; } public string UrlToImage { get; set; } - public DateTime? PublishedAt { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/ArticlesResult.cs b/NewsAPI/Models/ArticlesResult.cs index f7be632..ef46cc6 100644 --- a/NewsAPI/Models/ArticlesResult.cs +++ b/NewsAPI/Models/ArticlesResult.cs @@ -1,17 +1,13 @@ -using NewsAPI.Constants; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; +using NewsAPI.Constants; namespace NewsAPI.Models { public class ArticlesResult { - public Statuses Status { get; set; } + public List
Articles { get; set; } public Error Error { get; set; } + public Statuses Status { get; set; } public int TotalResults { get; set; } - public List
Articles { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/Error.cs b/NewsAPI/Models/Error.cs index 155be40..b79ca28 100644 --- a/NewsAPI/Models/Error.cs +++ b/NewsAPI/Models/Error.cs @@ -1,9 +1,4 @@ using NewsAPI.Constants; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace NewsAPI.Models { @@ -12,4 +7,4 @@ public class Error public ErrorCodes Code { get; set; } public string Message { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/EverythingRequest.cs b/NewsAPI/Models/EverythingRequest.cs index e8ab68a..534c794 100644 --- a/NewsAPI/Models/EverythingRequest.cs +++ b/NewsAPI/Models/EverythingRequest.cs @@ -1,52 +1,60 @@ -using NewsAPI.Constants; -using System; +using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using NewsAPI.Constants; namespace NewsAPI.Models { /// - /// Params for making a request to the /everything endpoint. + /// Params for making a request to the /everything endpoint. /// public class EverythingRequest { /// - /// The keyword or phrase to search for. Boolean operators are supported. + /// If you want to restrict the search to specific web domains, add these here. Example: nytimes.com. /// - public string Q { get; set; } + public List Domains = new List(); + /// - /// If you want to restrict the search to specific sources, add their Ids here. You can find source Ids with the /sources endpoint or on newsapi.org. + /// If you want to restrict the search to specific sources, add their Ids here. You can find source Ids with the + /// /sources endpoint or on newsapi.org. /// public List Sources = new List(); + /// - /// If you want to restrict the search to specific web domains, add these here. Example: nytimes.com. + /// The earliest date to retrieve articles from. Note that how far back you can go is constrained by your plan type. + /// See newsapi.org/pricing for plan details. /// - public List Domains = new List(); + public DateTime? From { get; set; } + /// - /// The earliest date to retrieve articles from. Note that how far back you can go is constrained by your plan type. See newsapi.org/pricing for plan details. + /// The language to restrict articles to. /// - public DateTime? From { get; set; } + public Languages? Language { get; set; } + /// - /// The latest date to retrieve articles from. + /// Each request returns a fixed amount of results. Page through them by increasing this. /// - public DateTime? To { get; set; } + public int Page { get; set; } + /// - /// The language to restrict articles to. + /// Set the max number of results to retrieve per request. The max is 100. /// - public Languages? Language { get; set; } + public int PageSize { get; set; } + /// - /// How should the results be sorted? Relevancy = articles relevant to the Q param come first. PublishedAt = most recent articles come first. Publisher = popular publishers come first. + /// The keyword or phrase to search for. Boolean operators are supported. /// - public SortBys? SortBy { get; set; } + public string Q { get; set; } + /// - /// Each request returns a fixed amount of results. Page through them by increasing this. + /// How should the results be sorted? Relevancy = articles relevant to the Q param come first. PublishedAt = most + /// recent articles come first. Publisher = popular publishers come first. /// - public int Page { get; set; } + public SortBys? SortBy { get; set; } + /// - /// Set the max number of results to retrieve per request. The max is 100. + /// The latest date to retrieve articles from. /// - public int PageSize { get; set; } + public DateTime? To { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/Source.cs b/NewsAPI/Models/Source.cs index 6ef25bd..b226b5f 100644 --- a/NewsAPI/Models/Source.cs +++ b/NewsAPI/Models/Source.cs @@ -1,14 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NewsAPI.Models +namespace NewsAPI.Models { public class Source { public string Id { get; set; } public string Name { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Models/TopHeadlinesRequest.cs b/NewsAPI/Models/TopHeadlinesRequest.cs index bf72c85..acb3c75 100644 --- a/NewsAPI/Models/TopHeadlinesRequest.cs +++ b/NewsAPI/Models/TopHeadlinesRequest.cs @@ -1,44 +1,47 @@ -using NewsAPI.Constants; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; +using NewsAPI.Constants; namespace NewsAPI.Models { /// - /// Params for making a request to the /top-headlines endpoint. + /// Params for making a request to the /top-headlines endpoint. /// public class TopHeadlinesRequest { /// - /// The keyword or phrase to search for. Boolean operators are supported. - /// - public string Q { get; set; } - /// - /// If you want to restrict the results to specific sources, add their Ids here. You can find source Ids with the /sources endpoint or on newsapi.org. + /// If you want to restrict the results to specific sources, add their Ids here. You can find source Ids with the + /// /sources endpoint or on newsapi.org. /// public List Sources = new List(); + /// - /// If you want to restrict the headlines to a specific news category, add these here. + /// If you want to restrict the headlines to a specific news category, add these here. /// public Categories? Category { get; set; } + /// - /// The language to restrict articles to. + /// The country of the source to restrict articles to. /// - public Languages? Language { get; set; } + public Countries? Country { get; set; } + /// - /// The country of the source to restrict articles to. + /// The language to restrict articles to. /// - public Countries? Country { get; set; } + public Languages? Language { get; set; } + /// - /// Each request returns a fixed amount of results. Page through them by increasing this. + /// Each request returns a fixed amount of results. Page through them by increasing this. /// public int Page { get; set; } + /// - /// Set the max number of results to retrieve per request. The max is 100. + /// Set the max number of results to retrieve per request. The max is 100. /// public int PageSize { get; set; } + + /// + /// The keyword or phrase to search for. Boolean operators are supported. + /// + public string Q { get; set; } } -} +} \ No newline at end of file diff --git a/NewsAPI/NewsAPI.csproj b/NewsAPI/NewsAPI.csproj index e2d60c2..dbb2ea8 100644 --- a/NewsAPI/NewsAPI.csproj +++ b/NewsAPI/NewsAPI.csproj @@ -1,72 +1,18 @@ - - - + - 11.0 - Debug - AnyCPU - {86CDF033-3CAE-4845-98BF-70575DF27E2B} - Library - Properties - NewsAPI - NewsAPI - en-US - 512 - {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Profile111 - v4.5 + netstandard2.0;net461 + Copyright 2018 + 0.5 + News API + News API + Official C# client library for News API (newsapi.org). Search for live news articles from all over the web. + https://github.com/News-API-gh/News-API-csharp/blob/master/LICENSE + https://github.com/News-API-gh/News-API-csharp + https://github.com/News-API-gh/News-API-csharp/raw/master/icon.png + https://github.com/News-API-gh/News-API-csharp - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - bin\Debug\NewsAPI.XML - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - bin\Release\NewsAPI.XML - - - - - - - - - - - - - - - - - - - - - ..\packages\Newtonsoft.Json.10.0.3\lib\portable-net45+win8+wp8+wpa81\Newtonsoft.Json.dll - True - - - + + - - - \ No newline at end of file + diff --git a/NewsAPI/NewsAPI.nuspec b/NewsAPI/NewsAPI.nuspec deleted file mode 100644 index ed9043b..0000000 --- a/NewsAPI/NewsAPI.nuspec +++ /dev/null @@ -1,17 +0,0 @@ - - - - NewsAPI - 0.4 - News API - News API - https://github.com/News-API-gh/News-API-csharp/blob/master/LICENSE - https://github.com/News-API-gh/News-API-csharp - https://github.com/News-API-gh/News-API-csharp/raw/master/icon.png - false - Official C# client library for News API (newsapi.org). Search for live news articles from all over the web. - Init. - Copyright 2018 - News, API, newsapi - - \ No newline at end of file diff --git a/NewsAPI/NewsApiClient.cs b/NewsAPI/NewsApiClient.cs index eb35eea..8411da7 100644 --- a/NewsAPI/NewsApiClient.cs +++ b/NewsAPI/NewsApiClient.cs @@ -1,49 +1,56 @@ -using NewsAPI.Constants; -using NewsAPI.Models; -using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Net; using System.Net.Http; -using System.Text; using System.Threading.Tasks; +using NewsAPI.Constants; +using NewsAPI.Models; +using Newtonsoft.Json; namespace NewsAPI { /// - /// Use this to get results from NewsAPI.org. + /// Use this to get results from NewsAPI.org. /// public class NewsApiClient { - private string BASE_URL = "https://newsapi.org/v2/"; - - private HttpClient HttpClient; + private const string BaseUrl = "https://newsapi.org/v2/"; - private string ApiKey; + private readonly HttpClient _httpClient; /// - /// Use this to get results from NewsAPI.org. + /// Use this to get results from NewsAPI.org. /// /// Your News API key. You can create one for free at https://newsapi.org. public NewsApiClient(string apiKey) { - ApiKey = apiKey; + _httpClient = new HttpClient(new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate + }); + _httpClient.DefaultRequestHeaders.Add("user-agent", "News-API-csharp/0.1"); + _httpClient.DefaultRequestHeaders.Add("x-api-key", apiKey); + } - HttpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }); - HttpClient.DefaultRequestHeaders.Add("user-agent", "News-API-csharp/0.1"); - HttpClient.DefaultRequestHeaders.Add("x-api-key", ApiKey); + /// + /// Query the /v2/everything endpoint for recent articles all over the web. + /// + /// The params and filters for the request. + /// + public ArticlesResult GetEverything(EverythingRequest request) + { + return GetEverythingAsync(request).Result; } /// - /// Query the /v2/top-headlines endpoint for live top news headlines. + /// Query the /v2/everything endpoint for recent articles all over the web. /// /// The params and filters for the request. /// - public async Task GetTopHeadlinesAsync(TopHeadlinesRequest request) + public async Task GetEverythingAsync(EverythingRequest request) { - // build the querystring + // build the queryString var queryParams = new List(); // q @@ -58,19 +65,34 @@ public async Task GetTopHeadlinesAsync(TopHeadlinesRequest reque queryParams.Add("sources=" + string.Join(",", request.Sources)); } - if (request.Category.HasValue) + // domains + if (request.Domains.Count > 0) { - queryParams.Add("category=" + request.Category.Value.ToString().ToLowerInvariant()); + queryParams.Add("domains=" + string.Join(",", request.Domains)); } + // from + if (request.From.HasValue) + { + queryParams.Add("from=" + string.Format("{0:s}", request.From.Value)); + } + + // to + if (request.To.HasValue) + { + queryParams.Add("to=" + string.Format("{0:s}", request.To.Value)); + } + + // language if (request.Language.HasValue) { queryParams.Add("language=" + request.Language.Value.ToString().ToLowerInvariant()); } - if (request.Country.HasValue) + // sortBy + if (request.SortBy.HasValue) { - queryParams.Add("country=" + request.Country.Value.ToString().ToLowerInvariant()); + queryParams.Add("sortBy=" + request.SortBy.Value); } // page @@ -86,13 +108,13 @@ public async Task GetTopHeadlinesAsync(TopHeadlinesRequest reque } // join them together - var querystring = string.Join("&", queryParams.ToArray()); + var queryString = string.Join("&", queryParams.ToArray()); - return await MakeRequest("top-headlines", querystring); + return await MakeRequest("everything", queryString).ConfigureAwait(false); } /// - /// Query the /v2/top-headlines endpoint for live top news headlines. + /// Query the /v2/top-headlines endpoint for live top news headlines. /// /// The params and filters for the request. /// @@ -102,13 +124,13 @@ public ArticlesResult GetTopHeadlines(TopHeadlinesRequest request) } /// - /// Query the /v2/everything endpoint for recent articles all over the web. + /// Query the /v2/top-headlines endpoint for live top news headlines. /// /// The params and filters for the request. /// - public async Task GetEverythingAsync(EverythingRequest request) + public async Task GetTopHeadlinesAsync(TopHeadlinesRequest request) { - // build the querystring + // build the queryString var queryParams = new List(); // q @@ -123,34 +145,19 @@ public async Task GetEverythingAsync(EverythingRequest request) queryParams.Add("sources=" + string.Join(",", request.Sources)); } - // domains - if (request.Domains.Count > 0) - { - queryParams.Add("domains=" + string.Join(",", request.Sources)); - } - - // from - if (request.From.HasValue) - { - queryParams.Add("from=" + string.Format("{0:s}", request.From.Value)); - } - - // to - if (request.To.HasValue) + if (request.Category.HasValue) { - queryParams.Add("to=" + string.Format("{0:s}", request.To.Value)); + queryParams.Add("category=" + request.Category.Value.ToString().ToLowerInvariant()); } - // language if (request.Language.HasValue) { queryParams.Add("language=" + request.Language.Value.ToString().ToLowerInvariant()); } - // sortBy - if (request.SortBy.HasValue) + if (request.Country.HasValue) { - queryParams.Add("sortBy=" + request.SortBy.Value.ToString()); + queryParams.Add("country=" + request.Country.Value.ToString().ToLowerInvariant()); } // page @@ -166,73 +173,72 @@ public async Task GetEverythingAsync(EverythingRequest request) } // join them together - var querystring = string.Join("&", queryParams.ToArray()); - - return await MakeRequest("everything", querystring); - } + var queryString = string.Join("&", queryParams.ToArray()); - /// - /// Query the /v2/everything endpoint for recent articles all over the web. - /// - /// The params and filters for the request. - /// - public ArticlesResult GetEverything(EverythingRequest request) - { - return GetEverythingAsync(request).Result; + return await MakeRequest("top-headlines", queryString).ConfigureAwait(false); } // *** - private async Task MakeRequest(string endpoint, string querystring) + private async Task MakeRequest(string endpoint, string queryString) { // here's the return obj var articlesResult = new ArticlesResult(); // make the http request - var httpRequest = new HttpRequestMessage(HttpMethod.Get, BASE_URL + endpoint + "?" + querystring); - var httpResponse = await HttpClient.SendAsync(httpRequest); + var httpRequest = new HttpRequestMessage(HttpMethod.Get, BaseUrl + endpoint + "?" + queryString); + var httpResponse = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false); - var json = await httpResponse.Content?.ReadAsStringAsync(); - if (!string.IsNullOrWhiteSpace(json)) + if (httpResponse.Content != null) { - // convert the json to an obj - var apiResponse = JsonConvert.DeserializeObject(json); - articlesResult.Status = apiResponse.Status; - if (articlesResult.Status == Statuses.Ok) - { - articlesResult.TotalResults = apiResponse.TotalResults; - articlesResult.Articles = apiResponse.Articles; - } - else + var json = await (httpResponse.Content.ReadAsStringAsync()).ConfigureAwait(false); + + if (!string.IsNullOrWhiteSpace(json)) { - ErrorCodes errorCode = ErrorCodes.UnknownError; - try + // convert the json to an obj + var apiResponse = JsonConvert.DeserializeObject(json); + articlesResult.Status = apiResponse.Status; + + if (articlesResult.Status == Statuses.Ok) { - errorCode = (ErrorCodes)apiResponse.Code; + articlesResult.TotalResults = apiResponse.TotalResults; + articlesResult.Articles = apiResponse.Articles; } - catch (Exception) + else { - Debug.WriteLine("The API returned an error code that wasn't expected: " + apiResponse.Code); + var errorCode = ErrorCodes.UnknownError; + + try + { + if (apiResponse.Code != null) + { + errorCode = (ErrorCodes) apiResponse.Code; + } + } + catch (Exception) + { + Debug.WriteLine("The API returned an error code that wasn't expected: " + apiResponse.Code); + } + + articlesResult.Error = new Error + { + Code = errorCode, + Message = apiResponse.Message + }; } - + } + else + { + articlesResult.Status = Statuses.Error; articlesResult.Error = new Error { - Code = errorCode, - Message = apiResponse.Message + Code = ErrorCodes.UnexpectedError, + Message = "The API returned an empty response. Are you connected to the internet?" }; } } - else - { - articlesResult.Status = Statuses.Error; - articlesResult.Error = new Error - { - Code = ErrorCodes.UnexpectedError, - Message = "The API returned an empty response. Are you connected to the internet?" - }; - } return articlesResult; } } -} +} \ No newline at end of file diff --git a/NewsAPI/Properties/AssemblyInfo.cs b/NewsAPI/Properties/AssemblyInfo.cs deleted file mode 100644 index f46c1e2..0000000 --- a/NewsAPI/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Resources; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("NewsAPI")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("NewsAPI")] -[assembly: AssemblyCopyright("Copyright © 2017")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] -[assembly: NeutralResourcesLanguage("en")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/NewsAPI/packages.config b/NewsAPI/packages.config deleted file mode 100644 index c2340da..0000000 --- a/NewsAPI/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file