From ce2d9f787f5d45f72faf1fcf66bca172447b68e1 Mon Sep 17 00:00:00 2001 From: CircuitRCAY Date: Fri, 5 Apr 2019 09:06:24 +1100 Subject: [PATCH] 2.2.0 --- NewsAPI.Net.Tests/EndpointTest.cs | 17 ++++++------ NewsAPI.Net/Extensions/HttpExtensions.cs | 2 ++ NewsAPI.Net/Models/Language.cs | 35 ++++++++++++++++++++++++ NewsAPI.Net/NewsAPI.Net.csproj | 24 ++++++---------- NewsAPI.Net/NewsApiClient.cs | 26 ++++++++---------- README.md | 14 +++++++--- 6 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 NewsAPI.Net/Models/Language.cs diff --git a/NewsAPI.Net.Tests/EndpointTest.cs b/NewsAPI.Net.Tests/EndpointTest.cs index 4349dea..344c8e8 100644 --- a/NewsAPI.Net.Tests/EndpointTest.cs +++ b/NewsAPI.Net.Tests/EndpointTest.cs @@ -1,3 +1,4 @@ +using NewsAPI.Net.Models; using Newtonsoft.Json; using Xunit; using Xunit.Abstractions; @@ -14,26 +15,26 @@ public EndpointTest(ITestOutputHelper output) } [Fact] - public void Test1() + public void SourcesTest() { - NewsClient client = new NewsClient("550d4e974dcc4cf1b4675a5ce91b202e"); + NewsClient client = new NewsClient("bb554838724b452fb14adf8661be7646"); Models.NewsSourcesModel s = client.GetSourcesAsync().GetAwaiter().GetResult(); output.WriteLine(JsonConvert.SerializeObject(s)); } [Fact] - public void Test2() + public void EverythingTest() { - NewsClient client = new NewsClient("550d4e974dcc4cf1b4675a5ce91b202e"); - Models.NewsModel s = client.GetEverythingAsync("bitcoin").GetAwaiter().GetResult(); + NewsClient client = new NewsClient("bb554838724b452fb14adf8661be7646"); + Models.NewsModel s = client.GetEverythingAsync("bitcoin", lang: Language.ENGLISH).GetAwaiter().GetResult(); output.WriteLine(JsonConvert.SerializeObject(s)); } [Fact] - public void Test3() + public void TopHeadlinesTest() { - NewsClient client = new NewsClient("550d4e974dcc4cf1b4675a5ce91b202e"); - Models.NewsModel s = client.GetTopHeadlinesAsync("bitcoin").GetAwaiter().GetResult(); + NewsClient client = new NewsClient("bb554838724b452fb14adf8661be7646"); + Models.NewsModel s = client.GetTopHeadlinesAsync("abc-news", lang:Language.ENGLISH).GetAwaiter().GetResult(); output.WriteLine(JsonConvert.SerializeObject(s)); } } diff --git a/NewsAPI.Net/Extensions/HttpExtensions.cs b/NewsAPI.Net/Extensions/HttpExtensions.cs index d873a32..4a94cab 100644 --- a/NewsAPI.Net/Extensions/HttpExtensions.cs +++ b/NewsAPI.Net/Extensions/HttpExtensions.cs @@ -1,10 +1,12 @@ using System; +using System.Runtime.InteropServices; using System.Web; namespace NewsAPI.Net.Extensions { internal static class HttpExtensions { + internal static Uri AddQuery(this Uri url, string paramName, string paramValue) { UriBuilder uriBuilder = new UriBuilder(url); diff --git a/NewsAPI.Net/Models/Language.cs b/NewsAPI.Net/Models/Language.cs new file mode 100644 index 0000000..c1e9a47 --- /dev/null +++ b/NewsAPI.Net/Models/Language.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NewsAPI.Net.Models +{ + public class Language + { + public static string ARABIC { get { return "ar"; } } + + public static string GERMAN { get { return "de"; } } + + public static string ENGLISH { get { return "en"; } } + + public static string SPANISH { get { return "es"; } } + + public static string FRENCH { get { return "fr"; } } + + public static string HEBREW { get { return "he"; } } + + public static string ITALIAN { get { return "it"; } } + + public static string GEKOLONISEERD { get { return "nl"; } } + + public static string NORSE { get { return "no"; } } + + public static string PORTUGUESE { get { return "pt"; } } + + public static string RUSSIAN { get { return "ru"; } } + + public static string SWEDISH { get { return "se"; } } + + public static string CHINESE { get { return "zh"; } } + } +} diff --git a/NewsAPI.Net/NewsAPI.Net.csproj b/NewsAPI.Net/NewsAPI.Net.csproj index ae668e3..a5f0350 100644 --- a/NewsAPI.Net/NewsAPI.Net.csproj +++ b/NewsAPI.Net/NewsAPI.Net.csproj @@ -1,27 +1,19 @@  - netcoreapp2.1;netstandard2.0;net472 - - 2.0.0 - https://github.com/The-Grape-Vine/NewsAPI.Net/blob/master/LICENSE - CircuitRCAY - Copyright 2019-present The Grape Vine + netstandard2.0 + 2.2.0 https://github.com/The-Grape-Vine/NewsAPI.Net/ - https://circuitrcay.com - news + circuitrcay + https://github.com/The-Grape-Vine/NewsAPI.Net/blob/master/LICENSE A C# library wrapping https://newsapi.org - Released 2.0.0, a major refactor in the core implementation. + Released 2.2.0, added support for specific languages. + 2019-present The Grape Vine. - - - - ..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.1.0\ref\netcoreapp2.1\System.Web.HttpUtility.dll - - - + + diff --git a/NewsAPI.Net/NewsApiClient.cs b/NewsAPI.Net/NewsApiClient.cs index dbf8261..d7bdcb8 100644 --- a/NewsAPI.Net/NewsApiClient.cs +++ b/NewsAPI.Net/NewsApiClient.cs @@ -62,23 +62,22 @@ public async Task GetSourcesAsync() } /// - /// Returns a containing a list of articles relating to . + /// Returns a containing a list of articles relating to . /// /// Keywords or phrases to search for. /// A comma-separated string of identifiers (maximum 20) for the news sources or blogs you want headlines from. + /// The language you want to get headlines for. Default being all languages returned. /// A comma-separated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to. - /// - public async Task GetEverythingAsync(string query = null, string sources = null, string domains = null) + /// Task{TResult} + public async Task GetEverythingAsync(string query = null, string sources = null, string domains = null, string lang = null) { - if (q == null) - { - throw new ArgumentNullException(nameof(query)); - } + Uri uri = new Uri(_baseUrl + "everything") .AddQuery("apiKey", _key) .AddQuery("q", query) .AddQuery("sources", sources) + .AddQuery("language", lang.ToString()) .AddQuery("domains", domains); HttpResponseMessage response = await _client.GetAsync(uri); response.EnsureSuccessStatusCode(); @@ -88,23 +87,22 @@ public async Task GetEverythingAsync(string query = null, string sour } /// - /// Returns a containing a list of top articles relating to . + /// Returns a containing a list of top articles relating to . /// /// Keywords or phrases to search for. /// A comma-separated string of identifiers (maximum 20) for the news sources or blogs you want headlines from. + /// The language you want to return top /// A comma-separated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to. - /// - public async Task GetTopHeadlinesAsync(string query, string sources = null, string domains = null) + /// Task{TResult} + public async Task GetTopHeadlinesAsync(string query = null, string sources = null, string domains = null, string lang = null) { - if (q == null) - { - throw new ArgumentNullException(nameof(query)); - } + Uri uri = new Uri(_baseUrl + "top-headlines") .AddQuery("apiKey", _key) .AddQuery("q", query) .AddQuery("sources", sources) + .AddQuery("language", lang.ToString()) .AddQuery("domains", domains); HttpResponseMessage response = await _client.GetAsync(uri); response.EnsureSuccessStatusCode(); diff --git a/README.md b/README.md index 0c0d4ee..69987f9 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,16 @@ var api = new NewsAPIClient("api-key"); var sources = await api.GetSourcesAsync(); -var everything = await api.GetEverythingAsync("bitcoin", "sources", "domains"); +var everything = await api.GetEverythingAsync("bitcoin", "sources", "domains", Language.ENGLISH); -var topHeadlines = await api.GetTopHeadlinesAsync("dotnet", "sources", "domains"); +var topHeadlines = await api.GetTopHeadlinesAsync("dotnet", "sources", "domains", Language.ENGLISH); ``` -# Documentation - - under construction \ No newline at end of file +## Documentation + - under construction + + +## License +We abide by the MIT License, provided [here](https://github.com/The-Grape-Vine/NewsAPI.Net/blob/master/LICENSE). + + \ No newline at end of file