2020-02-22 22:56:06 +00:00
|
|
|
using SuccessfulCodeTools;
|
|
|
|
|
using SuccessfulCodeTools.Extensions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2020-02-22 23:56:45 +00:00
|
|
|
namespace SuccessfulCode.BetfairApi
|
2020-02-22 22:56:06 +00:00
|
|
|
{
|
|
|
|
|
public class Authentication
|
|
|
|
|
{
|
|
|
|
|
private readonly string _username;
|
|
|
|
|
private readonly string _password;
|
|
|
|
|
private readonly string _applicationKey;
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
private Login _loginResponse;
|
|
|
|
|
|
|
|
|
|
public Authentication(string username, string password, string applicationKey)
|
|
|
|
|
{
|
|
|
|
|
_username = username;
|
|
|
|
|
_password = password;
|
|
|
|
|
_applicationKey = applicationKey;
|
|
|
|
|
_client = new HttpClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Authenticated()
|
|
|
|
|
{
|
|
|
|
|
return _loginResponse != null && String.IsNullOrWhiteSpace(_loginResponse.Token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Login> Authenticate()
|
|
|
|
|
{
|
|
|
|
|
var message = new HttpRequestMessage(HttpMethod.Post, "https://identitysso.betfair.com/api/login")
|
|
|
|
|
{
|
|
|
|
|
Content = new FormUrlEncodedContent(new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{ "username", _username },
|
|
|
|
|
{ "password", _password }
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
message.Headers.Add("X-Application", _applicationKey);
|
|
|
|
|
|
|
|
|
|
var requestResponse = await _client.SendAsync(message);
|
|
|
|
|
var content = await requestResponse.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
if (!requestResponse.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_loginResponse = content.DeserializeJson<Login>();
|
|
|
|
|
return _loginResponse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<T> ExecuteRequest<T>(string endpoint, object objectToSend = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Authenticated())
|
|
|
|
|
{
|
|
|
|
|
await Authenticate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_loginResponse == null || String.IsNullOrWhiteSpace(_loginResponse.Token))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Login succeeded but login credentials empty");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var objectJson = objectToSend == null ? String.Empty : objectToSend.ToJson();
|
|
|
|
|
|
|
|
|
|
var message = new HttpRequestMessage(HttpMethod.Post, endpoint)
|
|
|
|
|
{
|
|
|
|
|
Content = new StringContent(objectJson, Encoding.UTF8, "application/json")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
message.Headers.Add("X-Application", _applicationKey);
|
|
|
|
|
message.Headers.Add("X-Authentication", _loginResponse.Token);
|
|
|
|
|
|
|
|
|
|
var requestResponse = await _client.SendAsync(message);
|
|
|
|
|
var content = await requestResponse.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
if (!requestResponse.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = content.DeserializeJson<T>();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Login
|
|
|
|
|
{
|
|
|
|
|
public string Token { get; set; }
|
|
|
|
|
public string Product { get; set; }
|
|
|
|
|
public LoginStatus Status { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum LoginStatus
|
|
|
|
|
{
|
|
|
|
|
[EnumMember(Value = "SUCCESS")]
|
|
|
|
|
Success,
|
|
|
|
|
[EnumMember(Value = "LIMITED_ACCESS")]
|
|
|
|
|
LimitedAccess,
|
|
|
|
|
[EnumMember(Value = "LOGIN_RESTRICTED")]
|
|
|
|
|
LoginRestricted,
|
|
|
|
|
[EnumMember(Value = "FAIL")]
|
|
|
|
|
Fail
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|