diff --git a/Hassie.API.NewsAPI/Client/ClientBuilder.cs b/Hassie.API.NewsAPI/Client/ClientBuilder.cs index 9985b74..147daba 100644 --- a/Hassie.API.NewsAPI/Client/ClientBuilder.cs +++ b/Hassie.API.NewsAPI/Client/ClientBuilder.cs @@ -1,21 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Hassie.NET.API.NewsAPI.Client +namespace Hassie.NET.API.NewsAPI.Client { - public class ClientBuilder - { - private string apiKey; - - public string ApiKey - { - set { apiKey = value; } - } + public class ClientOptions + { + public string ApiKey { get; set; } - public INewsClient Build() - { - return new NewsClient(apiKey); - } + public string UserAgent { get; set; } } } \ No newline at end of file diff --git a/Hassie.API.NewsAPI/Client/NewsClient.cs b/Hassie.API.NewsAPI/Client/NewsClient.cs index 1f8ade9..b131398 100644 --- a/Hassie.API.NewsAPI/Client/NewsClient.cs +++ b/Hassie.API.NewsAPI/Client/NewsClient.cs @@ -9,57 +9,60 @@ namespace Hassie.NET.API.NewsAPI.Client { - internal class NewsClient : INewsClient + public class NewsClient : INewsClient { - private readonly string apiKey; + private readonly ClientOptions _clientOptions; + private readonly HttpClient _httpClient; - public NewsClient(string apiKey) + public NewsClient(ClientOptions clientOptions, + HttpClient httpClient) { - this.apiKey = apiKey; + _clientOptions = clientOptions; + _httpClient = httpClient; + + httpClient.DefaultRequestHeaders.Add("User-Agent", _clientOptions.UserAgent); } public async Task GetEverything(EverythingBuilder query) { - return new NewsArticles(await GetResponse(String.Concat(query.ToString(), "&apiKey=", apiKey))); + return new NewsArticles(await GetResponse(String.Concat(query.ToString(), "&apiKey=", _clientOptions.ApiKey))); } public async Task GetTopHeadlines(TopHeadlinesBuilder query) { - return new NewsArticles(await GetResponse(String.Concat(query.ToString(), "&apiKey=", apiKey))); + return new NewsArticles(await GetResponse(String.Concat(query.ToString(), "&apiKey=", _clientOptions.ApiKey))); } public async Task GetNewsSources() { - return new NewsSources(await GetResponse(String.Concat(new NewsSourcesBuilder().Build().ToString(), "?apiKey=", apiKey))); + return new NewsSources(await GetResponse(String.Concat(new NewsSourcesBuilder().Build().ToString(), "?apiKey=", _clientOptions.ApiKey))); } public async Task GetNewsSources(NewsSourcesBuilder query) { - return new NewsSources(await GetResponse(String.Concat(query.ToString(), "&apiKey=", apiKey))); + return new NewsSources(await GetResponse(String.Concat(query.ToString(), "&apiKey=", _clientOptions.ApiKey))); } private async Task GetResponse(string query) { try { - using (HttpClient httpClient = new HttpClient()) - { - // Get response from API. - HttpResponseMessage response = await httpClient.GetAsync(query); + // Get response from API. + HttpResponseMessage response = await _httpClient.GetAsync(query); - // Check status code. - if (response.IsSuccessStatusCode) - { - // Success, parse json. - return JObject.Parse(await response.Content.ReadAsStringAsync()); - } - else - { - // Parse error json and throw exception. - JObject json = JObject.Parse(await response.Content.ReadAsStringAsync()); - throw new NewsHTTPException($"News API HTTP Exception - {json["code"]}: {json["message"]}"); - } + // Check status code. + if (response.IsSuccessStatusCode) + { + // Success, parse json. + return JObject.Parse(await response.Content.ReadAsStringAsync()); } + else + { + // Parse error json and throw exception. + JObject json = JObject.Parse(await response.Content.ReadAsStringAsync()); + throw new NewsHTTPException($"News API HTTP Exception - {json["code"]}: {json["message"]}"); + } + } catch (HttpRequestException e1) { diff --git a/Hassie.API.NewsAPI/Hassie.NET.API.NewsAPI.csproj b/Hassie.API.NewsAPI/Hassie.NET.API.NewsAPI.csproj deleted file mode 100644 index 6935d04..0000000 --- a/Hassie.API.NewsAPI/Hassie.NET.API.NewsAPI.csproj +++ /dev/null @@ -1,45 +0,0 @@ - - - - netstandard1.1 - Hassie - - NewsAPI.NET - ©2018 Hassie. - News API wrapper for .NET, written in .NET Standard 1.1 - https://github.com/hassie-dash/NewsAPI.NET - true - https://github.com/hassie-dash/NewsAPI.NET/blob/master/LICENSE - https://github.com/hassie-dash/NewsAPI.NET - git - Version 2.2.1 -* Fixed bug when adding a search query with a query builder. - -Version 2.2.0 -* Implemented Everything endpoint. Note that for the query parameter, you must provide the query in URL encoded format. - -Version 2.1.0 -* INewsArticles and INewsSources now return a read only list of INewsArticle and INewsSource respectively, you no longer have to use the Articles/Sources property within the interface to retrieve the list. - -Version 2.0.1 -* Bug fixes. - -Version 2.0.0 -* Rewrote API wraper to use builder pattern. -* Implemented Sources endpoint. -* Please refer to documentation to see the updated example usages. - news;api;hassie;hassie-dash - false - 2.2.1.0 - en-GB - NewsAPI.NET - A simple to use async library to retrieve news from News API; written in .NET Standard 1.1. - 2.2.1 - 2.2.1.0 - - - - - - - diff --git a/Hassie.API.NewsAPI/Moises.NET.API.NewsAPI.csproj b/Hassie.API.NewsAPI/Moises.NET.API.NewsAPI.csproj new file mode 100644 index 0000000..c804216 --- /dev/null +++ b/Hassie.API.NewsAPI/Moises.NET.API.NewsAPI.csproj @@ -0,0 +1,30 @@ + + + + netstandard2.1 + Hassie;T. Moises + + NewsAPI.NET + ©2023 T. Moises. + News API wrapper for .NET, written in .NET Standard 2.1 + https://github.com/thiagomoises/News-API-csharp + true + https://github.com/thiagomoises/News-API-csharp/blob/master/LICENSE + https://github.com/thiagomoises/News-API-csharp + git + + news;api;hassie;hassie-dash;tmoises + false + 3.0.0.0 + en-GB + NewsAPI.NET + A simple to use async library to retrieve news from News API; written in .NET Standard 1.1. + 3.0.0 + 3.0.0 + + + + + + + diff --git a/Hassie.NET.API.NewsAPI.DependencyInjection/Moises.NET.API.NewsAPI.DependencyInjection.csproj b/Hassie.NET.API.NewsAPI.DependencyInjection/Moises.NET.API.NewsAPI.DependencyInjection.csproj new file mode 100644 index 0000000..07b9f2f --- /dev/null +++ b/Hassie.NET.API.NewsAPI.DependencyInjection/Moises.NET.API.NewsAPI.DependencyInjection.csproj @@ -0,0 +1,26 @@ + + + + netstandard2.1 + enable + true + news;api;hassie;hassie-dash;tmoises + false + 3.0.0.0 + en-GB + NewsAPI.NET + A simple to use async library to retrieve news from News API; written in .NET Standard 1.1. + 3.0.0 + 3.0.0 + + + + + + + + + + + + diff --git a/Hassie.NET.API.NewsAPI.DependencyInjection/ServiceCollectionExtensions.cs b/Hassie.NET.API.NewsAPI.DependencyInjection/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..0ac303d --- /dev/null +++ b/Hassie.NET.API.NewsAPI.DependencyInjection/ServiceCollectionExtensions.cs @@ -0,0 +1,15 @@ +using Hassie.NET.API.NewsAPI.Client; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Hassie.NET.API.NewsAPI.DependencyInjection +{ + public static class ServiceCollectionExtensions + { + public static IHttpClientBuilder AddNewsApi(this IServiceCollection services, Func options) + { + services.AddSingleton(x => options(new ClientOptions())); + return services.AddHttpClient(); + } + } +} diff --git a/NewsAPI.NET.sln b/NewsAPI.NET.sln index a71bb5d..3a2dd6d 100644 --- a/NewsAPI.NET.sln +++ b/NewsAPI.NET.sln @@ -1,11 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27004.2009 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{1EC615E4-8E49-4DAF-BF8E-3F82C3A583AB}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hassie.NET.API.NewsAPI", "Hassie.API.NewsAPI\Hassie.NET.API.NewsAPI.csproj", "{06280E10-1047-44B7-994E-F68481B044A6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moises.NET.API.NewsAPI", "Hassie.API.NewsAPI\Moises.NET.API.NewsAPI.csproj", "{06280E10-1047-44B7-994E-F68481B044A6}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{15667796-E30F-4563-9A82-939EA7926682}" ProjectSection(SolutionItems) = preProject @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moises.NET.API.NewsAPI.DependencyInjection", "Hassie.NET.API.NewsAPI.DependencyInjection\Moises.NET.API.NewsAPI.DependencyInjection.csproj", "{A9457174-917F-4C4E-94D6-0C91FABAD477}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,12 +25,17 @@ Global {06280E10-1047-44B7-994E-F68481B044A6}.Debug|Any CPU.Build.0 = Debug|Any CPU {06280E10-1047-44B7-994E-F68481B044A6}.Release|Any CPU.ActiveCfg = Release|Any CPU {06280E10-1047-44B7-994E-F68481B044A6}.Release|Any CPU.Build.0 = Release|Any CPU + {A9457174-917F-4C4E-94D6-0C91FABAD477}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9457174-917F-4C4E-94D6-0C91FABAD477}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9457174-917F-4C4E-94D6-0C91FABAD477}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9457174-917F-4C4E-94D6-0C91FABAD477}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {06280E10-1047-44B7-994E-F68481B044A6} = {1EC615E4-8E49-4DAF-BF8E-3F82C3A583AB} + {A9457174-917F-4C4E-94D6-0C91FABAD477} = {1EC615E4-8E49-4DAF-BF8E-3F82C3A583AB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {1F1DD039-22E9-4458-A1AE-39DC77D4C14F}