2022-11-12 02:27:46 +01:00

40 lines
1.2 KiB
Ruby

module Integrations
module Betfair
class Connection
include HTTParty
def initialize(account)
@account = account
self.class.pem @account.ssl_pem
end
def api_headers
{ 'X-Application' => @account.apikey, 'X-Authentication' => session_token, 'content-type' => 'application/json', 'accept' => 'application/json' }
end
private
def session_token
# if
puts 'Checking if session still fresh'
if @account.last_session_token_saved_at && @account.last_session_token_saved_at > 10.hours.ago
puts 'Returning cached session token'
return @account.last_session_token
end
puts 'Cache is stale or non-existent - getting fresh session key'
url = 'https://identitysso-cert.betfair.com/api/certlogin'
r = self.class.post(url, headers: { 'X-Application' => @account.apikey }, body: { username: @account.login_uid, password: @account.login_pass })
resp = JSON.parse(r)
if resp['loginStatus'] == 'SUCCESS'
@account.update(last_session_token: resp['sessionToken'], last_session_token_saved_at: Time.now)
return resp['sessionToken']
end
raise '[Betfair Session token] Cannot get session to Betfair'
end
end
end
end