Commit b5347a80 authored by Sergey Gernyak's avatar Sergey Gernyak

Completed request method integration

parent 4f4fe3bb
...@@ -3,9 +3,8 @@ require "betfair_api_ng_rails/errors" ...@@ -3,9 +3,8 @@ require "betfair_api_ng_rails/errors"
module BetfairApiNgRails module BetfairApiNgRails
module Api module Api
class Connection class Connection
def request(method, params = {}) def request(method, params = {})
provider.fetch method: method, params: hashing(params) provider.fetch method: Api::RequestMethod.new(method), params: hashing(params)
end end
include Api::ConnectionExt::Caching include Api::ConnectionExt::Caching
...@@ -19,7 +18,7 @@ module BetfairApiNgRails ...@@ -19,7 +18,7 @@ module BetfairApiNgRails
@_provider = nil @_provider = nil
end end
protected protected
def provider def provider
@_provider ||= BetfairApiNgRails::Api::Provider.new request_ssoid @_provider ||= BetfairApiNgRails::Api::Provider.new request_ssoid
...@@ -32,7 +31,6 @@ module BetfairApiNgRails ...@@ -32,7 +31,6 @@ module BetfairApiNgRails
def hashing(params) def hashing(params)
BetfairApiNgRails::Api::Hashalator.new(params).to_hash BetfairApiNgRails::Api::Hashalator.new(params).to_hash
end end
end end
end end
end end
\ No newline at end of file
...@@ -19,7 +19,7 @@ module BetfairApiNgRails ...@@ -19,7 +19,7 @@ module BetfairApiNgRails
CODE CODE
end end
private private
def write_income_request(method, params) def write_income_request(method, params)
BetfairApiNgRails.log.write("--->Income request for #{method} with parameters #{hashing(params)}") BetfairApiNgRails.log.write("--->Income request for #{method} with parameters #{hashing(params)}")
...@@ -28,8 +28,7 @@ module BetfairApiNgRails ...@@ -28,8 +28,7 @@ module BetfairApiNgRails
def write_result(res) def write_result(res)
BetfairApiNgRails.log.write(" Result of request: #{res.result}") BetfairApiNgRails.log.write(" Result of request: #{res.result}")
end end
end end
end end
end end
end end
\ No newline at end of file
...@@ -58,6 +58,11 @@ module BetfairApiNgRails ...@@ -58,6 +58,11 @@ module BetfairApiNgRails
:go_filename :go_filename
] ]
JSON_METHOD = {
betting: 'SportsAPING',
account: 'AccountAPING'
}
# URLs # URLs
LOGIN_URL = "https://identitysso.betfair.com/api/certlogin" LOGIN_URL = "https://identitysso.betfair.com/api/certlogin"
......
module BetfairApiNgRails module BetfairApiNgRails
module Api module Api
module Helper module Helper
def read_certificate_file(path) def read_certificate_file(path)
OpenSSL::X509::Certificate.new read_file(path) OpenSSL::X509::Certificate.new read_file(path)
end end
...@@ -19,11 +18,10 @@ module BetfairApiNgRails ...@@ -19,11 +18,10 @@ module BetfairApiNgRails
def prepare_api_req_json(method, params) def prepare_api_req_json(method, params)
{ {
"jsonrpc" => "2.0", "jsonrpc" => "2.0",
"method" => "SportsAPING/v1.0/#{method}", "method" => "#{method.json_method}/v1.0/#{method.name}",
"params" => params "params" => params
}.to_json }.to_json
end end
end end
end end
end end
\ No newline at end of file
...@@ -5,11 +5,11 @@ module BetfairApiNgRails ...@@ -5,11 +5,11 @@ module BetfairApiNgRails
class << self class << self
include Api::Constants include Api::Constants
def provider_requester(ssoid = nil) def provider_requester(api_url, ssoid = nil)
create_http_requester(Api::Config.api_url).tap do |req| req = create_http_requester(api_url)
req.set_request_headers API_REQUEST_HEADERS req.set_request_headers API_REQUEST_HEADERS
req.set_auth_headers Api::Config.application_key, ssoid req.set_auth_headers Api::Config.application_key, ssoid
end req
end end
def session_requester def session_requester
......
require "net/https" require "net/https"
require "uri" require 'uri'
require 'json' require 'json'
require 'betfair_api_ng_rails/api/helper' require 'betfair_api_ng_rails/api/helper'
...@@ -68,8 +68,7 @@ module BetfairApiNgRails ...@@ -68,8 +68,7 @@ module BetfairApiNgRails
def set_header(name, value) def set_header(name, value)
request[name] = value request[name] = value
end end
end end
end end
end end
end end
\ No newline at end of file
...@@ -14,25 +14,21 @@ module BetfairApiNgRails ...@@ -14,25 +14,21 @@ module BetfairApiNgRails
end end
def fetch(method: "", params: {}) def fetch(method: "", params: {})
raise "Not allowed method #{method.to_s}" unless is_method_allowed?(method) raise "Not allowed method #{method.name}" unless method.allowed?
run_request for_method: method, params: params run_request for_method: method, params: params
end end
private private
def run_request(for_method: "", params: {}) def run_request(for_method: "", params: {})
http_requester.set_api_req_body for_method, params requester = http_requester(for_method.api_url)
http_requester.do_request requester.set_api_req_body for_method, params
requester.do_request
end end
def is_method_allowed?(method) def http_requester(api_url)
ALLOWED_RESOURCES.include? method.to_s @_http_requester ||= Api::Http::Factory.provider_requester api_url, ssoid
end end
def http_requester
@_http_requester ||= Api::Http::Factory.provider_requester ssoid
end
end end
end end
end end
...@@ -23,6 +23,11 @@ module BetfairApiNgRails ...@@ -23,6 +23,11 @@ module BetfairApiNgRails
API_URL[type] API_URL[type]
end end
def json_method
return :no_method unless allowed?
JSON_METHOD[type]
end
private private
def allow_data def allow_data
......
...@@ -25,7 +25,7 @@ module BetfairApiNgRails ...@@ -25,7 +25,7 @@ module BetfairApiNgRails
ssoid ssoid
end end
private private
def fetch_ssoid def fetch_ssoid
get_login_response.session_token.tap { |sid| send_keep_alive(sid) } get_login_response.session_token.tap { |sid| send_keep_alive(sid) }
...@@ -47,8 +47,7 @@ module BetfairApiNgRails ...@@ -47,8 +47,7 @@ module BetfairApiNgRails
def keep_alive_requester(sid) def keep_alive_requester(sid)
Api::Http::Factory.keep_alive_requester sid Api::Http::Factory.keep_alive_requester sid
end end
end end
end end
end end
end end
\ No newline at end of file
require 'spec_helper' require 'spec_helper'
describe BetfairApiNgRails::Api::Connection do describe BetfairApiNgRails::Api::Connection do
subject(:connection) { described_class.new } subject(:connection) { described_class.new }
describe "#request" do describe "#request" do
let(:provider) { double(:provider) } let(:provider) { double(:provider) }
let(:parser) { double(:parser, process: true) } let(:parser) { double(:parser, process: true) }
let(:response) { double(:response, result: "") } let(:response) { double(:response, result: "") }
let(:logger) { double(:logger) } let(:logger) { double(:logger) }
before(:each) do before(:each) do
expect(subject).to receive(:init_parser).and_return parser expect(subject).to receive(:init_parser).and_return parser
BetfairApiNgRails.log = logger BetfairApiNgRails.log = logger
allow(logger).to receive(:write) allow(logger).to receive(:write)
...@@ -19,27 +17,22 @@ describe BetfairApiNgRails::Api::Connection do ...@@ -19,27 +17,22 @@ describe BetfairApiNgRails::Api::Connection do
it "calls provider's fetch method" do it "calls provider's fetch method" do
expect(subject).to receive(:provider).and_return provider expect(subject).to receive(:provider).and_return provider
expect(provider).to receive(:fetch).with(method: 'method', params: {}).and_return response expect(provider).to receive(:fetch).and_return response
expect(response).to receive(:has_error?).and_return false expect(response).to receive(:has_error?).and_return false
allow(subject).to receive(:hashing).with({}).and_return({}) allow(subject).to receive(:hashing).with({}).and_return({})
subject.request 'method' subject.request 'method'
end end
end end
describe "#expire_provider" do describe "#expire_provider" do
it "resets provider and ssoid" do it "resets provider and ssoid" do
connection.expire_provider connection.expire_provider
expect(connection.instance_variable_get(:@_provider)).to be_nil expect(connection.instance_variable_get(:@_provider)).to be_nil
end end
end end
describe "private method" do context "private method" do
describe "#provider" do describe "#provider" do
let(:ssoid) { double(:ssoid) } let(:ssoid) { double(:ssoid) }
it "inits new provider" do it "inits new provider" do
...@@ -47,20 +40,16 @@ describe BetfairApiNgRails::Api::Connection do ...@@ -47,20 +40,16 @@ describe BetfairApiNgRails::Api::Connection do
expect(subject).to receive(:request_ssoid).and_return ssoid expect(subject).to receive(:request_ssoid).and_return ssoid
subject.send(:provider) subject.send(:provider)
end end
end end
describe "#request_ssoid" do describe "#request_ssoid" do
it "requests new ssoid for new connection" do it "requests new ssoid for new connection" do
expect(BetfairApiNgRails::Api::SessionManager).to receive(:new_ssoid).at_least(:once) expect(BetfairApiNgRails::Api::SessionManager).to receive(:new_ssoid).at_least(:once)
subject.send(:request_ssoid) subject.send(:request_ssoid)
end end
end end
describe "#hashing" do describe "#hashing" do
let(:hashalator) { double(:hashalator) } let(:hashalator) { double(:hashalator) }
let(:params) { double(:params) } let(:params) { double(:params) }
...@@ -69,9 +58,6 @@ describe BetfairApiNgRails::Api::Connection do ...@@ -69,9 +58,6 @@ describe BetfairApiNgRails::Api::Connection do
expect(hashalator).to receive(:to_hash) expect(hashalator).to receive(:to_hash)
subject.send :hashing, params subject.send :hashing, params
end end
end end
end end
end
end
\ No newline at end of file
require 'spec_helper' require 'spec_helper'
describe BetfairApiNgRails::Api::Provider do describe BetfairApiNgRails::Api::Provider do
let(:ssoid) { double(:ssoid) } let(:ssoid) { double(:ssoid) }
subject { described_class.new(ssoid) } subject { described_class.new(ssoid) }
...@@ -9,83 +8,45 @@ describe BetfairApiNgRails::Api::Provider do ...@@ -9,83 +8,45 @@ describe BetfairApiNgRails::Api::Provider do
its(:ssoid) { is_expected.to eq ssoid } its(:ssoid) { is_expected.to eq ssoid }
describe "#fetch" do describe "#fetch" do
let(:method) { double(:method, allowed?: method_allowed, name: 'someMethod') }
before { expect(subject).to receive(:is_method_allowed?).with(:some_method).and_return(method_allowed) }
context 'when method isn\'t allowed' do context 'when method isn\'t allowed' do
let(:method_allowed) { false } let(:method_allowed) { false }
it "raises error" do it "raises error" do
expect{ subject.fetch(method: :some_method) }.to raise_error expect{ subject.fetch(method: method) }.to raise_error
end end
end end
context 'when requested method is allowed' do context 'when requested method is allowed' do
let(:method_allowed) { true } let(:method_allowed) { true }
it "calls run_request" do it "calls run_request" do
expect(subject).to receive(:run_request).with(for_method: :some_method, params: {}) expect(subject).to receive(:run_request).with(for_method: method, params: {})
subject.fetch method: :some_method subject.fetch method: method
end end
end end
end end
describe "private method" do describe "private method" do
describe "#run_request" do describe "#run_request" do
let(:http_requester) { double(:http_requester) } let(:http_requester) { double(:http_requester) }
let(:method) { double(:method, api_url: 'api_url') }
before { expect(subject).to receive(:http_requester).at_least(:once).and_return(http_requester) } before { expect(subject).to receive(:http_requester).at_least(:once).and_return(http_requester) }
it "runs http requster" do it "runs http requster" do
expect(http_requester).to receive(:set_api_req_body).with(:some_method, {}) expect(http_requester).to receive(:set_api_req_body).with(method, {})
expect(http_requester).to receive(:do_request) expect(http_requester).to receive(:do_request)
subject.send :run_request, for_method: :some_method, params: {} subject.send :run_request, for_method: method, params: {}
end
end
describe "#is_method_allowed?" do
before { stub_const("BetfairApiNgRails::Api::Constants::ALLOWED_RESOURCES", allowed_methods) }
context 'when requested method marked as allowed' do
let(:allowed_methods) { ['some_method'] }
it "returns true" do
expect(subject.send(:is_method_allowed?, :some_method)).to eq true
end
end
context 'when requested method isn\'t allowed' do
let(:allowed_methods) { ['some_another_method'] }
it "returns false" do
expect(subject.send(:is_method_allowed?, :some_method)).to eq false
end
end end
end end
describe "#http_requester" do describe "#http_requester" do
it "initalizes provider http request" do it "initalizes provider http request" do
expect(BetfairApiNgRails::Api::Http::Factory).to receive(:provider_requester).with ssoid expect(BetfairApiNgRails::Api::Http::Factory).to receive(:provider_requester).with 'api_url', ssoid
subject.send :http_requester subject.send :http_requester, 'api_url'
end end
end end
end end
end
end
\ No newline at end of file
...@@ -13,11 +13,13 @@ describe BetfairApiNgRails::Api::RequestMethod do ...@@ -13,11 +13,13 @@ describe BetfairApiNgRails::Api::RequestMethod do
before do before do
stub_const('BetfairApiNgRails::Api::Constants::ALLOWED_RESOURCES', {betting: [method_name]}) stub_const('BetfairApiNgRails::Api::Constants::ALLOWED_RESOURCES', {betting: [method_name]})
stub_const('BetfairApiNgRails::Api::Constants::API_URL', {betting: 'api_url'}) stub_const('BetfairApiNgRails::Api::Constants::API_URL', {betting: 'api_url'})
stub_const('BetfairApiNgRails::Api::Constants::JSON_METHOD', {betting: 'json_method'})
end end
its(:type) { is_expected.to eq(:betting) } its(:type) { is_expected.to eq(:betting) }
its(:allowed?) { is_expected.to be_truthy } its(:allowed?) { is_expected.to be_truthy }
its(:api_url) { is_expected.to eq('api_url') } its(:api_url) { is_expected.to eq('api_url') }
its(:json_method) { is_expected.to eq('json_method') }
end end
context 'when method is not exists in allowed' do context 'when method is not exists in allowed' do
...@@ -29,5 +31,6 @@ describe BetfairApiNgRails::Api::RequestMethod do ...@@ -29,5 +31,6 @@ describe BetfairApiNgRails::Api::RequestMethod do
its(:type) { is_expected.to eq(:no_type) } its(:type) { is_expected.to eq(:no_type) }
its(:allowed?) { is_expected.to be_falsey } its(:allowed?) { is_expected.to be_falsey }
its(:api_url) { is_expected.to eq(:no_api_url) } its(:api_url) { is_expected.to eq(:no_api_url) }
its(:json_method) { is_expected.to eq(:no_method) }
end end
end end
require 'spec_helper' require 'spec_helper'
require 'betfair_api_ng_rails/api/constants' require 'betfair_api_ng_rails/api/constants'
module TestModule
extend BetfairApiNgRails::Api::RequestMethods
end
describe BetfairApiNgRails::Api::RequestMethods do describe BetfairApiNgRails::Api::RequestMethods do
before { BetfairApiNgRails.config.locale = :en } before { BetfairApiNgRails.config.locale = :en }
BetfairApiNgRails::Api::Constants::SIMPLE_LISTING_FILTERED.each do |method|
BetfairApiNgRails::Api::Constants::SIMPLE_LISTING_FILTERED.each do |method|
describe "##{method}" do describe "##{method}" do
it "calls run_request with proper params" do it "calls run_request with proper params" do
expect(TestModule).to receive(:run_request).with(method.underscore.to_sym, {filter: :filter}, {locale: :en}) expect(TestModule).to receive(:run_request).with(method.underscore.to_sym, {filter: :filter}, {locale: :en})
TestModule.send method.underscore.to_sym, filter: :filter TestModule.send method.underscore.to_sym, filter: :filter
end end
end end
end end
describe "#list_market_catalogue" do describe "#list_market_catalogue" do
it "calls run_request with proper params" do it "calls run_request with proper params" do
expect(TestModule).to receive(:run_request).with(:list_market_catalogue, {filter: :filter, market_projection: [], sort: "", max_results: '1'}, {locale: :en}) expect(TestModule).to receive(:run_request).with(:list_market_catalogue, {filter: :filter, market_projection: [], sort: "", max_results: '1'}, {locale: :en})
TestModule.list_market_catalogue filter: :filter TestModule.list_market_catalogue filter: :filter
end end
end end
describe "#list_market_book" do describe "#list_market_book" do
it "calls run_request with proper params" do it "calls run_request with proper params" do
expect(TestModule).to receive(:run_request).with(:list_market_book, {market_ids: ['1'], price_projection: :priceProjection, order_projection: '', match_projection: ''}, {currency_code: 'USD', locale: :en}) expect(TestModule).to receive(:run_request).with(:list_market_book, {market_ids: ['1'], price_projection: :priceProjection, order_projection: '', match_projection: ''}, {currency_code: 'USD', locale: :en})
TestModule.list_market_book market_ids: ['1'], price_projection: :priceProjection TestModule.list_market_book market_ids: ['1'], price_projection: :priceProjection
end end
end end
describe "#place_orders" do describe "#place_orders" do
it "calls run_request with proper params" do it "calls run_request with proper params" do
expect(TestModule).to receive(:run_request).with(:place_orders, {market_id: "", instructions: [], customer_ref: ""}) expect(TestModule).to receive(:run_request).with(:place_orders, {market_id: "", instructions: [], customer_ref: ""})
TestModule.place_orders TestModule.place_orders
end end
end end