betfair-ng 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +43 -1
- data/lib/betfair/api/rest.rb +4 -4
- data/lib/betfair/api/rpc.rb +1 -1
- data/lib/betfair/client.rb +30 -13
- data/lib/betfair/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 243906d3090f8e342821089ba7a408f1e36508c296b3fb0c74b41775240134e6
|
4
|
+
data.tar.gz: ea2fcfda179921e4a551a8f62f7923ec2c70aeda194096888ce213e9901d47bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c53554ec09f61ff4a40fdfb8e618e6799f848f4e1dbdc780ffe03dd86e656dd7e57cd65f58a39ae7e54908940d3562719eba6347227bc0e97ba8ce051cd0c8b6
|
7
|
+
data.tar.gz: b4e7b3087e0373410485557fc233e741b79d2beb55b0060bda97c52bd0c02366a8c305e5d838fde25477f3a0c48879578bb5aab2b1f5472371aa83a6bdef7508
|
data/README.md
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
A lightweight ruby wrapper for the Betfair Exchange API (API-NG).
|
4
4
|
|
5
|
-
Full API description, including API parameters etc, is available from [Betfair's dedicated API site](
|
5
|
+
Full API description, including API parameters etc, is available from [Betfair's dedicated API site](http://docs.developer.betfair.com/docs/).
|
6
|
+
|
7
|
+
Oh, and always [bet responsibly](http://responsiblegambling.betfair.com/). Duh.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -91,6 +93,46 @@ client.place_orders({
|
|
91
93
|
client.logout
|
92
94
|
```
|
93
95
|
|
96
|
+
## Settings
|
97
|
+
|
98
|
+
You can change the endpoint from the default api.betfair.com .. to api-au.betfair.com by using the `endpoint` option when you initialize the Betfair client.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
client = Betfair::Client.new({ "X-Application" => "your_app_code" }, { endpoint: :aus })
|
102
|
+
```
|
103
|
+
|
104
|
+
The default is :default.
|
105
|
+
|
106
|
+
## Best practices
|
107
|
+
|
108
|
+
### Persistent HTTP connection
|
109
|
+
|
110
|
+
Betfair [recommends](http://docs.developer.betfair.com/docs/x/pAFD) that we pass the `Connection: keep-alive` header with each request in order to take advantage of HTTP 1.1's ability to have [persistent connections](https://en.wikipedia.org/wiki/HTTP_persistent_connection) which reduces latency for subsequent requests.
|
111
|
+
|
112
|
+
This library uses the [`httpi`](https://github.com/savonrb/httpi) gem, which supports a number of different ruby http client adapters. [`httpclient`](https://github.com/nahi/httpclient) and [`net-http-persistent`](https://github.com/drbrain/net-http-persistent) are two which utilise persistent connections by default. To use `net-http-persistent` you should ensure that the gem is installed and in your load path, then set the HTTPI adapter:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
require 'betfair'
|
116
|
+
HTTPI.adapter = :net_http_persistent
|
117
|
+
```
|
118
|
+
|
119
|
+
The same goes for `httpclient`, but it's not strictly necessary to explicitly set the adapter as it has a higher [load order precedence](https://github.com/savonrb/httpi/blob/master/lib/httpi/adapter.rb#L16) than the other adapters.
|
120
|
+
|
121
|
+
### Account security and authentication
|
122
|
+
|
123
|
+
Despite the example in this readme, you should definitely use the [non-interactive](http://docs.developer.betfair.com/docs/x/J4Q6) login for your bots; check the Betfair docs about how to set that up. To login this way, use the `non_interactive_login` method:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
# Performs the login procedure recommended for applications which run autonomously
|
127
|
+
# username: Betfair account username string
|
128
|
+
# password: Betfair account password string
|
129
|
+
# cert_key_file_path: Path to Betfair client certificate private key file
|
130
|
+
# cert_key_path: Path to Betfair client certificate public key file associated with Betfair account
|
131
|
+
client.non_interactive_login(username, password, cert_key_file_path, cert_file_path)
|
132
|
+
```
|
133
|
+
|
134
|
+
This also allows you to use 2-factor authentication for your online account access, which I'd also strongly recommend that you do. I'm pretty sure you wouldn't be comfortable if your bank accounts had nothing more advanced than credential access, and seeing as a lot of you will have a reasonable amount of money stored in your Betfair accounts, I don't think you should settle for that there, either.
|
135
|
+
|
94
136
|
## Todo
|
95
137
|
|
96
138
|
1. Error handling
|
data/lib/betfair/api/rest.rb
CHANGED
@@ -11,8 +11,8 @@ module Betfair
|
|
11
11
|
})
|
12
12
|
|
13
13
|
apis = {
|
14
|
-
betting: "https
|
15
|
-
account: "https
|
14
|
+
betting: "https://#{obj.endpoint}.betfair.com/exchange/betting/rest/v1.0",
|
15
|
+
account: "https://#{obj.endpoint}.betfair.com/exchange/account/rest/v1.0",
|
16
16
|
}
|
17
17
|
|
18
18
|
obj.class::OPERATIONS.each do |api, operations|
|
@@ -43,7 +43,7 @@ module Betfair
|
|
43
43
|
# cert_key_path: Path to Betfair client certificate public key file associated with Betfair account
|
44
44
|
def non_interactive_login(username, password, cert_key_file_path, cert_file_path)
|
45
45
|
json = post({
|
46
|
-
url: "https://identitysso.betfair.com/api/certlogin",
|
46
|
+
url: "https://identitysso-cert.betfair.com/api/certlogin",
|
47
47
|
body: { username: username, password: password },
|
48
48
|
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
|
49
49
|
cert_key_file_path: cert_key_file_path,
|
@@ -79,4 +79,4 @@ module Betfair
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
end
|
82
|
-
end
|
82
|
+
end
|
data/lib/betfair/api/rpc.rb
CHANGED
@@ -2,7 +2,7 @@ module Betfair
|
|
2
2
|
module API
|
3
3
|
module RPC
|
4
4
|
def self.extended(obj)
|
5
|
-
obj.endpoint = "https
|
5
|
+
obj.endpoint = "https://#{obj.endpoint}.betfair.com/exchange/betting/json-rpc/v1"
|
6
6
|
obj.persistent_headers.merge!({
|
7
7
|
"Accept" => "application/json",
|
8
8
|
"Content-Type" => "application/json"
|
data/lib/betfair/client.rb
CHANGED
@@ -2,6 +2,7 @@ require "betfair/api/rest"
|
|
2
2
|
require "betfair/api/rpc"
|
3
3
|
require "betfair/utils"
|
4
4
|
require "httpi"
|
5
|
+
require "active_support/core_ext/hash"
|
5
6
|
|
6
7
|
module Betfair
|
7
8
|
class Client
|
@@ -17,21 +18,24 @@ module Betfair
|
|
17
18
|
account: [:get_account_funds]
|
18
19
|
}
|
19
20
|
|
20
|
-
attr_accessor :settings, :persistent_headers
|
21
|
+
attr_accessor :settings, :request_opts, :persistent_headers, :endpoint
|
22
|
+
|
23
|
+
def initialize(headers = {}, opts = {}, api_type = :rest)
|
24
|
+
set_endpoint!(opts.delete(:endpoint))
|
25
|
+
|
26
|
+
setting_opts = opts.extract!(:retries, :adapter)
|
27
|
+
@settings = DEFAULT_SETTINGS.merge(setting_opts)
|
28
|
+
@persistent_headers = headers
|
29
|
+
@request_opts = opts
|
21
30
|
|
22
|
-
def initialize(headers = {}, api_type = :rest, settings = {})
|
23
|
-
@settings = DEFAULT_SETTINGS.merge(settings)
|
24
|
-
@persistent_headers = {}
|
25
|
-
@persistent_headers.merge!(headers)
|
26
31
|
extend_api(api_type)
|
27
32
|
end
|
28
33
|
|
29
34
|
private
|
30
|
-
|
31
35
|
[:get, :post].each do |verb|
|
32
36
|
define_method(verb) do |*args|
|
33
37
|
request = configure_request(*args)
|
34
|
-
response = attempt((settings[:retries]
|
38
|
+
response = attempt((settings[:retries]).times) do
|
35
39
|
HTTPI.request(verb, request, settings[:adapter])
|
36
40
|
end
|
37
41
|
response.body
|
@@ -40,14 +44,27 @@ module Betfair
|
|
40
44
|
|
41
45
|
def configure_request(opts = {})
|
42
46
|
opts[:headers] = persistent_headers.merge(opts[:headers] || {})
|
43
|
-
request = HTTPI::Request.new(opts)
|
44
47
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
48
|
+
HTTPI::Request.new(request_opts.merge(opts)).tap do |r|
|
49
|
+
# It would be nice to have HTTPI do this itself but
|
50
|
+
# HTTPI::Request#mass_assign doesn't merge auth fields
|
51
|
+
if opts[:cert_file_path] && opts[:cert_key_file_path]
|
52
|
+
r.auth.ssl.cert_key_file = opts[:cert_key_file_path]
|
53
|
+
r.auth.ssl.cert_file = opts[:cert_file_path]
|
54
|
+
end
|
49
55
|
end
|
50
|
-
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_endpoint!(endpoint)
|
59
|
+
@endpoint =
|
60
|
+
case endpoint
|
61
|
+
when :default, nil
|
62
|
+
"api"
|
63
|
+
when :aus
|
64
|
+
"api-au"
|
65
|
+
else
|
66
|
+
raise RuntimeError, "invalid endpoint, please choose :default or :aus"
|
67
|
+
end
|
51
68
|
end
|
52
69
|
|
53
70
|
def extend_api(type)
|
data/lib/betfair/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: betfair-ng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Campbell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.7.7
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: A lightweight wrapper for the Betfair Exchange API-NG
|