Add project files.
This commit is contained in:
@ -0,0 +1,31 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace OemanTrader.FinantialModelingPrepAPI
|
||||
{
|
||||
public class FinancialModelingPrepHttpClient : HttpClient
|
||||
{
|
||||
private readonly string _apiKey;
|
||||
|
||||
public FinancialModelingPrepHttpClient(string apiKey)
|
||||
{
|
||||
this.BaseAddress = new Uri("https://financialmodelingprep.com/api/v3/");
|
||||
_apiKey = apiKey;
|
||||
}
|
||||
|
||||
public async Task<T> GetAsync<T>(string uri)
|
||||
{
|
||||
// HttpResponseMessage response = await GetAsync(uri + "?apikey=2035d4934632e1d7c38f15982e39d3aa");
|
||||
//var apikey = "2035d4934632e1d7c38f15982e39d3aa";
|
||||
HttpResponseMessage response = await GetAsync($"{uri}?apikey={_apiKey}");
|
||||
string jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(jsonResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.FinantialModelingPrepAPI
|
||||
{
|
||||
public class FinancialModelingPrepHttpClientFactory
|
||||
{
|
||||
private readonly string _apiKey;
|
||||
|
||||
public FinancialModelingPrepHttpClientFactory(string apiKey)
|
||||
{
|
||||
_apiKey = apiKey;
|
||||
}
|
||||
|
||||
public FinancialModelingPrepHttpClient CreateHttpClient()
|
||||
{
|
||||
return new FinancialModelingPrepHttpClient(_apiKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OemanTrader.Domain\OemanTrader.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.FinantialModelingPrepAPI.Results
|
||||
{
|
||||
public class StockPriceResult
|
||||
{
|
||||
public double Price { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
using Newtonsoft.Json;
|
||||
using OemanTrader.Domain.Models;
|
||||
using OemanTrader.Domain.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.FinantialModelingPrepAPI.Services
|
||||
{
|
||||
public class MajorIndexService : IMajorIndexService
|
||||
{
|
||||
private readonly FinancialModelingPrepHttpClientFactory _httpClientFactory;
|
||||
|
||||
public MajorIndexService(FinancialModelingPrepHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public async Task<MajorIndex> GetMajorIndex(MajorIndexType indexType)
|
||||
{
|
||||
using (FinancialModelingPrepHttpClient client = _httpClientFactory.CreateHttpClient())
|
||||
{
|
||||
string uri = "majors-indexes/" + GetUriSuffix(indexType);
|
||||
|
||||
//HttpResponseMessage response = await client.GetAsync(uri + "?apikey=2035d4934632e1d7c38f15982e39d3aa");
|
||||
MajorIndex majorIndex = await client.GetAsync<MajorIndex>(uri);
|
||||
majorIndex.Type = indexType;
|
||||
|
||||
return majorIndex;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetUriSuffix(MajorIndexType indexType)
|
||||
{
|
||||
switch (indexType)
|
||||
{
|
||||
case MajorIndexType.DowJones:
|
||||
return ".DJI";
|
||||
case MajorIndexType.Nasdaq:
|
||||
return ".IXIC";
|
||||
case MajorIndexType.SP500:
|
||||
return ".INX";
|
||||
default:
|
||||
throw new Exception("MajorIndexType does not have a suffix defined.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
using OemanTrader.Domain.Exceptions;
|
||||
using OemanTrader.Domain.Services;
|
||||
using OemanTrader.FinantialModelingPrepAPI.Results;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OemanTrader.FinantialModelingPrepAPI.Services
|
||||
{
|
||||
public class StockPriceService : IStockPriceService
|
||||
{
|
||||
private readonly FinancialModelingPrepHttpClientFactory _httpClientFactory;
|
||||
|
||||
public StockPriceService(FinancialModelingPrepHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
public async Task<double> GetPrice(string symbol)
|
||||
{
|
||||
using (FinancialModelingPrepHttpClient client = _httpClientFactory.CreateHttpClient())
|
||||
{
|
||||
string uri = "stock/real-time-price/" + symbol;
|
||||
|
||||
//HttpResponseMessage response = await client.GetAsync(uri + "?apikey=2035d4934632e1d7c38f15982e39d3aa");
|
||||
StockPriceResult stockPriceResult = await client.GetAsync<StockPriceResult>(uri);
|
||||
|
||||
if (stockPriceResult.Price == 0)
|
||||
{
|
||||
throw new InvalidSymbolException(symbol);
|
||||
}
|
||||
|
||||
return stockPriceResult.Price;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user