using MyCore.DTO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; 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 allLeagues = new List(); public class League { public string ApiIdentifiant { get; set; } public string Identifiant { get; set; } public League(string league) { Identifiant = 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}®ion={_region}"; _client.BaseAddress = new Uri(_url); // TO REVIEW ! League leagueBE = new League("BE"); League leagueUK = new League("UK"); League leagueDK = new League("DK"); League leagueFR = new League("FR"); League leagueDE = new League("DE"); League leagueIT = new League("IT"); League leagueNE = new League("NE"); League leaguePT = new League("PT"); League leagueRU = new League("RU"); League leagueES = new League("ES"); League leagueCH = new League("CH"); League leagueTR = new League("TR"); allLeagues.Add(leagueBE); allLeagues.Add(leagueUK); allLeagues.Add(leagueDK); allLeagues.Add(leagueFR); allLeagues.Add(leagueDE); allLeagues.Add(leagueIT); allLeagues.Add(leagueNE); allLeagues.Add(leaguePT); allLeagues.Add(leagueRU); allLeagues.Add(leagueES); allLeagues.Add(leagueCH); allLeagues.Add(leagueTR); } public async Task> GetOddsForLeague(League league) { try { using (HttpClient client = new HttpClient()) { 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 response = await client.GetAsync(url); if (response != null) { var jsonString = await response.Content.ReadAsStringAsync(); var data = ((JObject)JsonConvert.DeserializeObject(jsonString))["data"]; return JsonConvert.DeserializeObject>(data.ToString()); } } } catch (Exception ex) { Console.WriteLine(ex); } return null; } public List GetOddsForAllLeagues() { List oddsToReturn = new List(); return oddsToReturn; } } }