OddsService WIP

This commit is contained in:
Thomas Fransolet 2020-01-13 20:22:06 +01:00
parent e49daefae3
commit 8ec099f688
8 changed files with 176 additions and 1 deletions

Binary file not shown.

View File

@ -8,6 +8,8 @@ using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;
using MyCore.Models;
using MyCore.Services;
using static MyCore.Services.OddService;
namespace MyCore.Controllers
{
@ -16,6 +18,9 @@ namespace MyCore.Controllers
[ApiController]
public class ValuesController : ControllerBase
{
private OddService oddService = new OddService(OddService.RegionOdd.UK);
// GET api/values
/// <summary>
/// It's a test ! :)
@ -27,6 +32,28 @@ namespace MyCore.Controllers
{
WidgetWeather widgetWeather = new WidgetWeather();
try
{
League league = new League("BE");
oddService.GetOddsForLeague(league).ContinueWith(res =>
{
if (res.Status == TaskStatus.RanToCompletion)
{
var test = res.Result;
}
else
{
}
}); ;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return new string[] { "value1", "value2" };
}

32
MyCore/DTO/Odd.cs Normal file
View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO
{
public class Odd
{
public string Sport_nice;
public List<string> Teams;
public int Commence_time;
public string Home_team;
public List<OddSite> Sites;
}
public class OddSite
{
public string Site_key;
public string Site_nice;
public int Last_update;
public OddMatch OddMatch;
}
public class OddMatch
{
public double HomeOdd;
public double DrawOdd;
public double VisitOdd;
}
}

View File

@ -12,7 +12,6 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="DTO\" />
<Folder Include="wwwroot\" />
</ItemGroup>

View File

@ -0,0 +1,117 @@
using MyCore.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
namespace MyCore.Services
{
public class OddService
{
private HttpClient _client = new HttpClient();
private string _servicePoint = "https://api.the-odds-api.com/v3/odds/?";
private string _apiKey = "285d0ebdb4b29415f6fef8b83729f655";
private string _url;
private string _region;
public List<League> allLeagues = new List<League>();
public class League
{
public string ApiIdentifiant { get; set; }
public League(string league) {
switch (league)
{
case "BE":
ApiIdentifiant = "soccer_belgium_first_div";
break;
case "UK":
ApiIdentifiant = "soccer_epl";
break;
case "DK":
ApiIdentifiant = "soccer_denmark_superliga";
break;
case "FR":
ApiIdentifiant = "soccer_france_ligue_one";
break;
case "DE":
ApiIdentifiant = "soccer_germany_bundesliga";
break;
case "IT":
ApiIdentifiant = "soccer_italy_serie_a";
break;
case "NE":
ApiIdentifiant = "soccer_netherlands_eredivisie";
break;
case "PT":
ApiIdentifiant = "soccer_portugal_primeira_liga";
break;
case "RU":
ApiIdentifiant = "soccer_russia_premier_league";
break;
case "ES":
ApiIdentifiant = "soccer_spain_la_liga";
break;
case "CH":
ApiIdentifiant = "soccer_switzerland_superleague";
break;
case "TR":
ApiIdentifiant = "soccer_turkey_super_league";
break;
default:
ApiIdentifiant = "soccer_epl";
break;
}
}
}
public class RegionOdd
{
public static string UK { get; set; } = "uk";
public static string US { get; set; } = "us";
public static string AU { get; set; } = "au";
}
public OddService(string region)
{
_region = region;
_url = $"{_servicePoint}apiKey={_apiKey}&region={_region}";
_client.BaseAddress = new Uri(_url);
}
public async Task<List<Odd>> GetOddsForLeague(League league)
{
List<Odd> oddsToReturn = new List<Odd>();
var builder = new UriBuilder(_servicePoint);
builder.Port = -1;
var query = HttpUtility.ParseQueryString(builder.Query);
query["apiKey"] = _apiKey;
query["region"] = _region;
query["sport"] = league.ApiIdentifiant;
builder.Query = query.ToString();
string url = builder.ToString();
_client.BaseAddress = new Uri(url);
var result = await _client.GetAsync(url);
Console.WriteLine(result.StatusCode);
return oddsToReturn;
}
public List<Odd> GetOddsForAllLeagues()
{
List<Odd> oddsToReturn = new List<Odd>();
return oddsToReturn;
}
}
}