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"
module BetfairApiNgRails
module Api
class Connection
def request(method, params = {})
provider.fetch method: method, params: hashing(params)
provider.fetch method: Api::RequestMethod.new(method), params: hashing(params)
end
include Api::ConnectionExt::Caching
......@@ -32,7 +31,6 @@ module BetfairApiNgRails
def hashing(params)
BetfairApiNgRails::Api::Hashalator.new(params).to_hash
end
end
end
end
......@@ -28,7 +28,6 @@ module BetfairApiNgRails
def write_result(res)
BetfairApiNgRails.log.write(" Result of request: #{res.result}")
end
end
end
end
......
......@@ -58,6 +58,11 @@ module BetfairApiNgRails
:go_filename
]
JSON_METHOD = {
betting: 'SportsAPING',
account: 'AccountAPING'
}
# URLs
LOGIN_URL = "https://identitysso.betfair.com/api/certlogin"
......
module BetfairApiNgRails
module Api
module Helper
def read_certificate_file(path)
OpenSSL::X509::Certificate.new read_file(path)
end
......@@ -19,11 +18,10 @@ module BetfairApiNgRails
def prepare_api_req_json(method, params)
{
"jsonrpc" => "2.0",
"method" => "SportsAPING/v1.0/#{method}",
"method" => "#{method.json_method}/v1.0/#{method.name}",
"params" => params
}.to_json
end
end
end
end
......@@ -5,11 +5,11 @@ module BetfairApiNgRails
class << self
include Api::Constants
def provider_requester(ssoid = nil)
create_http_requester(Api::Config.api_url).tap do |req|
def provider_requester(api_url, ssoid = nil)
req = create_http_requester(api_url)
req.set_request_headers API_REQUEST_HEADERS
req.set_auth_headers Api::Config.application_key, ssoid
end
req
end
def session_requester
......
require "net/https"
require "uri"
require 'uri'
require 'json'
require 'betfair_api_ng_rails/api/helper'
......@@ -68,7 +68,6 @@ module BetfairApiNgRails
def set_header(name, value)
request[name] = value
end
end
end
end
......
......@@ -14,25 +14,21 @@ module BetfairApiNgRails
end
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
end
private
def run_request(for_method: "", params: {})
http_requester.set_api_req_body for_method, params
http_requester.do_request
requester = http_requester(for_method.api_url)
requester.set_api_req_body for_method, params
requester.do_request
end
def is_method_allowed?(method)
ALLOWED_RESOURCES.include? method.to_s
def http_requester(api_url)
@_http_requester ||= Api::Http::Factory.provider_requester api_url, ssoid
end
def http_requester
@_http_requester ||= Api::Http::Factory.provider_requester ssoid
end
end
end
end
......@@ -23,6 +23,11 @@ module BetfairApiNgRails
API_URL[type]
end
def json_method
return :no_method unless allowed?
JSON_METHOD[type]
end
private
def allow_data
......
......@@ -47,7 +47,6 @@ module BetfairApiNgRails
def keep_alive_requester(sid)
Api::Http::Factory.keep_alive_requester sid
end
end
end
end
......
require 'spec_helper'
describe BetfairApiNgRails::Api::Connection do
subject(:connection) { described_class.new }
describe "#request" do
let(:provider) { double(:provider) }
let(:parser) { double(:parser, process: true) }
let(:response) { double(:response, result: "") }
......@@ -19,27 +17,22 @@ describe BetfairApiNgRails::Api::Connection do
it "calls provider's fetch method" do
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
allow(subject).to receive(:hashing).with({}).and_return({})
subject.request 'method'
end
end
describe "#expire_provider" do
it "resets provider and ssoid" do
connection.expire_provider
expect(connection.instance_variable_get(:@_provider)).to be_nil
end
end
describe "private method" do
context "private method" do
describe "#provider" do
let(:ssoid) { double(:ssoid) }
it "inits new provider" do
......@@ -47,20 +40,16 @@ describe BetfairApiNgRails::Api::Connection do
expect(subject).to receive(:request_ssoid).and_return ssoid
subject.send(:provider)
end
end
describe "#request_ssoid" do
it "requests new ssoid for new connection" do
expect(BetfairApiNgRails::Api::SessionManager).to receive(:new_ssoid).at_least(:once)
subject.send(:request_ssoid)
end
end
describe "#hashing" do
let(:hashalator) { double(:hashalator) }
let(:params) { double(:params) }
......@@ -69,9 +58,6 @@ describe BetfairApiNgRails::Api::Connection do
expect(hashalator).to receive(:to_hash)
subject.send :hashing, params
end
end
end
end
require 'spec_helper'
describe BetfairApiNgRails::Api::Provider do
let(:ssoid) { double(:ssoid) }
subject { described_class.new(ssoid) }
......@@ -9,83 +8,45 @@ describe BetfairApiNgRails::Api::Provider do
its(:ssoid) { is_expected.to eq ssoid }
describe "#fetch" do
before { expect(subject).to receive(:is_method_allowed?).with(:some_method).and_return(method_allowed) }
let(:method) { double(:method, allowed?: method_allowed, name: 'someMethod') }
context 'when method isn\'t allowed' do
let(:method_allowed) { false }
it "raises error" do
expect{ subject.fetch(method: :some_method) }.to raise_error
expect{ subject.fetch(method: method) }.to raise_error
end
end
context 'when requested method is allowed' do
let(:method_allowed) { true }
it "calls run_request" do
expect(subject).to receive(:run_request).with(for_method: :some_method, params: {})
subject.fetch method: :some_method
expect(subject).to receive(:run_request).with(for_method: method, params: {})
subject.fetch method: method
end
end
end
describe "private method" do
describe "#run_request" do
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) }
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)
subject.send :run_request, for_method: :some_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
subject.send :run_request, for_method: method, params: {}
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
describe "#http_requester" do
it "initalizes provider http request" do
expect(BetfairApiNgRails::Api::Http::Factory).to receive(:provider_requester).with ssoid
subject.send :http_requester
expect(BetfairApiNgRails::Api::Http::Factory).to receive(:provider_requester).with 'api_url', ssoid
subject.send :http_requester, 'api_url'
end
end
end
end
......@@ -13,11 +13,13 @@ describe BetfairApiNgRails::Api::RequestMethod do
before do
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::JSON_METHOD', {betting: 'json_method'})
end
its(:type) { is_expected.to eq(:betting) }
its(:allowed?) { is_expected.to be_truthy }
its(:api_url) { is_expected.to eq('api_url') }
its(:json_method) { is_expected.to eq('json_method') }
end
context 'when method is not exists in allowed' do
......@@ -29,5 +31,6 @@ describe BetfairApiNgRails::Api::RequestMethod do
its(:type) { is_expected.to eq(:no_type) }
its(:allowed?) { is_expected.to be_falsey }
its(:api_url) { is_expected.to eq(:no_api_url) }
its(:json_method) { is_expected.to eq(:no_method) }
end
end
require 'spec_helper'
require 'betfair_api_ng_rails/api/constants'
module TestModule
extend BetfairApiNgRails::Api::RequestMethods
end
describe BetfairApiNgRails::Api::RequestMethods do
before { BetfairApiNgRails.config.locale = :en }
BetfairApiNgRails::Api::Constants::SIMPLE_LISTING_FILTERED.each do |method|
describe "##{method}" do
it "calls run_request with proper params" do
expect(TestModule).to receive(:run_request).with(method.underscore.to_sym, {filter: :filter}, {locale: :en})
TestModule.send method.underscore.to_sym, filter: :filter
end
end
end
describe "#list_market_catalogue" 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})
TestModule.list_market_catalogue filter: :filter
end
end
describe "#list_market_book" 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})
TestModule.list_market_book market_ids: ['1'], price_projection: :priceProjection
end
end
describe "#place_orders" do
it "calls run_request with proper params" do
expect(TestModule).to receive(:run_request).with(:place_orders, {market_id: "", instructions: [], customer_ref: ""})
TestModule.place_orders
end
end
describe "private method" do
describe "#build_request_type" do
it "returns proper api method name" do
expect(TestModule.send(:build_request_type, 'some_value')).to eq 'someValue'
end
end
describe "#run_request" do
let(:params) { {key1: 'value'} }
let(:data) { {key2: 'value'} }
let(:connection) { double(:connection) }
......@@ -72,9 +53,6 @@ describe BetfairApiNgRails::Api::RequestMethods do
it "calls request method with proper params" do
expect(connection).to receive(:request).with('someMethod', params.merge(data))
end
end
end
end
......@@ -4,11 +4,12 @@ shared_examples 'request method' do
let(:http_response) { double(:http_response, code: '200', body: result_hash) }
let(:result) { BetfairApiNgRails::Api::Http::Responser.new(http_response) }
let(:filter) { BetfairApiNgRails::MarketFilter.new }
let(:api_http_requester) { double(:api_http_requester, do_request: result, set_api_req_body: true) }
let(:api_http_requester) { double(:api_http_requester, do_request: result, set_api_req_body: true, result: '') }
before(:each) do
allow(api_http_requester).to receive(:do_request).and_return result
expect(BetfairApiNgRails::Api::SessionManager).to receive(:new_ssoid).and_return ssoid
expect(BetfairApiNgRails::Api::Http::Factory).to receive(:provider_requester).with(ssoid).and_return api_http_requester
expect(BetfairApiNgRails::Api::Http::Factory).to receive(:provider_requester).and_return api_http_requester
BetfairApiNgRails.connection = connection
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment