Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions Hassie.API.NewsAPI/Client/ClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
51 changes: 27 additions & 24 deletions Hassie.API.NewsAPI/Client/NewsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<INewsArticles> 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<INewsArticles> 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<INewsSources> 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<INewsSources> 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<JObject> 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)
{
Expand Down
45 changes: 0 additions & 45 deletions Hassie.API.NewsAPI/Hassie.NET.API.NewsAPI.csproj

This file was deleted.

30 changes: 30 additions & 0 deletions Hassie.API.NewsAPI/Moises.NET.API.NewsAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Authors>Hassie;T. Moises</Authors>
<Company />
<Product>NewsAPI.NET</Product>
<Copyright>©2023 T. Moises.</Copyright>
<Description>News API wrapper for .NET, written in .NET Standard 2.1</Description>
<PackageProjectUrl>https://github.com/thiagomoises/News-API-csharp</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl>https://github.com/thiagomoises/News-API-csharp/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/thiagomoises/News-API-csharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>

<PackageTags>news;api;hassie;hassie-dash;tmoises</PackageTags>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<NeutralLanguage>en-GB</NeutralLanguage>
<Title>NewsAPI.NET</Title>
<Description>A simple to use async library to retrieve news from News API; written in .NET Standard 1.1.</Description>
<Version>3.0.0</Version>
<FileVersion>3.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageTags>news;api;hassie;hassie-dash;tmoises</PackageTags>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<NeutralLanguage>en-GB</NeutralLanguage>
<Title>NewsAPI.NET</Title>
<Description>A simple to use async library to retrieve news from News API; written in .NET Standard 1.1.</Description>
<Version>3.0.0</Version>
<FileVersion>3.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hassie.API.NewsAPI\Moises.NET.API.NewsAPI.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<ClientOptions, ClientOptions> options)
{
services.AddSingleton(x => options(new ClientOptions()));
return services.AddHttpClient<INewsClient, NewsClient>();
}
}
}
13 changes: 10 additions & 3 deletions NewsAPI.NET.sln
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@

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
.travis.yml = .travis.yml
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
Expand All @@ -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}
Expand Down