Commit 82e71439 authored by Clint.Network's avatar Clint.Network

Adding Fixer API Service

parent 2872c941
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using RestSharp;
using Stratis.Guru.Settings;
namespace Stratis.Guru.Services
{
public class FixerService : IHostedService, IDisposable
{
private FixerApiSettings _options;
private IDistributedCache _distributedCache;
private Timer _timer;
public FixerService(IOptions<FixerApiSettings> options, IDistributedCache distributedCache)
{
_options = options.Value;
_distributedCache = distributedCache;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromHours(1));
return Task.CompletedTask;
}
private void DoWork(object state)
{
var rs = new RestClient($"{_options.Endpoint}?access_key={_options.ApiKey}&format=1");
var rq = new RestRequest(Method.GET);
rs.ExecuteAsync(rq, delegate(IRestResponse response)
{
_distributedCache.SetString("Fixer", response.Content);
});
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
}
\ No newline at end of file
...@@ -16,7 +16,7 @@ namespace Stratis.Guru.Services ...@@ -16,7 +16,7 @@ namespace Stratis.Guru.Services
private readonly IMemoryCache _memoryCache; private readonly IMemoryCache _memoryCache;
private readonly IHubContext<UpdateHub> _hubContext; private readonly IHubContext<UpdateHub> _hubContext;
private readonly UpdateHub _hub; private readonly UpdateHub _hub;
private readonly System.Timers.Timer updateTimer; private readonly System.Timers.Timer _updateTimer;
private readonly NakoApiSettings _nakoApiSettings; private readonly NakoApiSettings _nakoApiSettings;
public UpdateInfosService(IMemoryCache memoryCache, UpdateHub hub, IHubContext<UpdateHub> hubContext, IOptions<NakoApiSettings> nakoApiSettings) public UpdateInfosService(IMemoryCache memoryCache, UpdateHub hub, IHubContext<UpdateHub> hubContext, IOptions<NakoApiSettings> nakoApiSettings)
...@@ -24,17 +24,17 @@ namespace Stratis.Guru.Services ...@@ -24,17 +24,17 @@ namespace Stratis.Guru.Services
_memoryCache = memoryCache; _memoryCache = memoryCache;
_hub = hub; _hub = hub;
_hubContext = hubContext; _hubContext = hubContext;
updateTimer = new System.Timers.Timer(); _updateTimer = new System.Timers.Timer();
_nakoApiSettings = nakoApiSettings.Value; _nakoApiSettings = nakoApiSettings.Value;
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
{ {
updateTimer.Interval = 10; _updateTimer.Interval = 10;
updateTimer.Enabled = true; _updateTimer.Enabled = true;
updateTimer.Elapsed += async (sender, args) => _updateTimer.Elapsed += async (sender, args) =>
{ {
updateTimer.Interval = TimeSpan.FromMinutes(10).TotalMilliseconds; _updateTimer.Interval = TimeSpan.FromMinutes(10).TotalMilliseconds;
var coinmarketCapApiClient = new RestClient("https://api.coinmarketcap.com/v2/ticker/1343/"); var coinmarketCapApiClient = new RestClient("https://api.coinmarketcap.com/v2/ticker/1343/");
var coinmarketCapApiRequest = new RestRequest(Method.GET); var coinmarketCapApiRequest = new RestRequest(Method.GET);
var coinmarketcapApi = coinmarketCapApiClient.Execute(coinmarketCapApiRequest); var coinmarketcapApi = coinmarketCapApiClient.Execute(coinmarketCapApiRequest);
...@@ -46,7 +46,7 @@ namespace Stratis.Guru.Services ...@@ -46,7 +46,7 @@ namespace Stratis.Guru.Services
var blockchainStatsRequest = new RestRequest(Method.GET); var blockchainStatsRequest = new RestRequest(Method.GET);
_memoryCache.Set("BlockchainStats", blockchainStatsClient.Execute(blockchainStatsRequest).Content); _memoryCache.Set("BlockchainStats", blockchainStatsClient.Execute(blockchainStatsRequest).Content);
}; };
updateTimer.Start(); _updateTimer.Start();
return Task.CompletedTask; return Task.CompletedTask;
} }
...@@ -57,8 +57,8 @@ namespace Stratis.Guru.Services ...@@ -57,8 +57,8 @@ namespace Stratis.Guru.Services
public void Dispose() public void Dispose()
{ {
updateTimer?.Stop(); _updateTimer?.Stop();
updateTimer?.Dispose();; _updateTimer?.Dispose();
} }
} }
} }
\ No newline at end of file
namespace Stratis.Guru.Settings
{
public class FixerApiSettings
{
public string ApiKey { get; set; }
public string Endpoint { get; set; }
}
}
\ No newline at end of file
...@@ -14,6 +14,7 @@ using Microsoft.Extensions.Configuration; ...@@ -14,6 +14,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Stratis.Guru.Hubs; using Stratis.Guru.Hubs;
using Stratis.Guru.Models;
using Stratis.Guru.Modules; using Stratis.Guru.Modules;
using Stratis.Guru.Services; using Stratis.Guru.Services;
using Stratis.Guru.Settings; using Stratis.Guru.Settings;
...@@ -39,22 +40,19 @@ namespace Stratis.Guru ...@@ -39,22 +40,19 @@ namespace Stratis.Guru
options.MinimumSameSitePolicy = SameSiteMode.None; options.MinimumSameSitePolicy = SameSiteMode.None;
}); });
services.Configure<NakoApiSettings>(Configuration.GetSection("NakoApi")); services.Configure<NakoApiSettings>(Configuration.GetSection("NakoApi"));
services.Configure<FixerApiSettings>(Configuration.GetSection("FixerApi"));
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
services.AddMemoryCache(); services.AddMemoryCache();
services.AddHostedService<UpdateInfosService>();
//services.AddHostedService<VanityService>();
services.AddTransient<UpdateHub>(); services.AddTransient<UpdateHub>();
services.AddSingleton<IAsk, Ask>(); services.AddSingleton<IAsk, Ask>();
/*services.AddResponseCaching(options => services.AddHostedService<UpdateInfosService>();
{ services.AddHostedService<FixerService>();
options.UseCaseSensitivePaths = true; services.AddHostedService<VanityService>();
options.MaximumBodySize = 1024;
});*/
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment