Skip to content

Commit

Permalink
Added AspNetCore DI setup method, fixes BenFradet#381 (BenFradet#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Ouborny authored May 31, 2017
1 parent d67b8cc commit 1aad4d5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
9 changes: 9 additions & 0 deletions RiotSharp.AspNetCore/ApiKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RiotSharp.AspNetCore
{
public class ApiKey
{
public string Key { get; set; }
public int RateLimitPer10S { get; set; }
public int RateLimitPer10M { get; set; }
}
}
49 changes: 49 additions & 0 deletions RiotSharp.AspNetCore/IServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.Extensions.DependencyInjection;
using RiotSharp.Http;
using RiotSharp.Interfaces;
using System;

namespace RiotSharp.AspNetCore
{
public static class IServiceCollectionExtension
{
/// <summary>
/// Adds and configures RiotSharp's services
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection AddRiotSharp(this IServiceCollection serviceCollection, Action<RiotSharpOptions> options)
{
var riotSharpOptions = new RiotSharpOptions();
options(riotSharpOptions);

if (riotSharpOptions.TournamentApiKey.Key == null && riotSharpOptions.ApiKey.Key == null)
throw new ArgumentNullException("No api key provided.", innerException: null);

if (riotSharpOptions.ApiKey.Key != null)
{
var rateLimitedRequester = new RateLimitedRequester(riotSharpOptions.ApiKey.Key,
riotSharpOptions.ApiKey.RateLimitPer10S, riotSharpOptions.ApiKey.RateLimitPer10M);
serviceCollection.AddSingleton<ITournamentRiotApi>(serviceProvider =>
new TournamentRiotApi(rateLimitedRequester));
serviceCollection.AddSingleton<IRiotApi>(serviceProvider => new RiotApi(rateLimitedRequester));

var requester = new Requester(riotSharpOptions.ApiKey.Key);
serviceCollection.AddSingleton<ICache, Cache>();
serviceCollection.AddSingleton<IStaticRiotApi>(serviceProvider =>
new StaticRiotApi(requester, serviceProvider.GetRequiredService<ICache>()));
}

if (riotSharpOptions.TournamentApiKey.Key != null)
{
var rateLimitedRequester = new RateLimitedRequester(riotSharpOptions.TournamentApiKey.Key,
riotSharpOptions.TournamentApiKey.RateLimitPer10S, riotSharpOptions.TournamentApiKey.RateLimitPer10M);
serviceCollection.AddSingleton<ITournamentRiotApi>(serviceProvider =>
new TournamentRiotApi(rateLimitedRequester));
}

return serviceCollection;
}
}
}
15 changes: 15 additions & 0 deletions RiotSharp.AspNetCore/RiotSharp.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\RiotSharp\RiotSharp.csproj" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions RiotSharp.AspNetCore/RiotSharpOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace RiotSharp.AspNetCore
{
public class RiotSharpOptions
{
public RiotSharpOptions()
{
ApiKey = new ApiKey();
ApiKey.RateLimitPer10S = 10;
ApiKey.RateLimitPer10M = 500;
TournamentApiKey = new ApiKey();
TournamentApiKey.RateLimitPer10S = 10;
TournamentApiKey.RateLimitPer10M = 500;
}

public ApiKey ApiKey { get; set; }
public ApiKey TournamentApiKey { get; set; }
}
}
10 changes: 8 additions & 2 deletions RiotSharp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2015
VisualStudioVersion = 14.0.23107.0
# Visual Studio 15
VisualStudioVersion = 15.0.26430.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RiotSharp", "RiotSharp\RiotSharp.csproj", "{3815E113-140C-4EE7-B0B9-7797C9352311}"
EndProject
Expand All @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{E359C2
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RiotSharp.AspNetCore", "RiotSharp.AspNetCore\RiotSharp.AspNetCore.csproj", "{3701FA9B-3D58-4266-A3E8-4BA1167BF3D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -34,6 +36,10 @@ Global
{2A150AA0-91AE-4CAC-9524-C67161D6E480}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A150AA0-91AE-4CAC-9524-C67161D6E480}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A150AA0-91AE-4CAC-9524-C67161D6E480}.Release|Any CPU.Build.0 = Release|Any CPU
{3701FA9B-3D58-4266-A3E8-4BA1167BF3D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3701FA9B-3D58-4266-A3E8-4BA1167BF3D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3701FA9B-3D58-4266-A3E8-4BA1167BF3D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3701FA9B-3D58-4266-A3E8-4BA1167BF3D6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ skip_tags: true
#---------------------------------#

# Operating system (build VM template)
os: Visual Studio 2015
os: Visual Studio 2017

# clone directory
clone_folder: c:\projects\RiotSharp
Expand Down

0 comments on commit 1aad4d5

Please sign in to comment.