Example #1
0
 def _create_client(settings):
     """Returns APIClient based on settings
     or looks for username in os.environ
     """
     if settings:
         return APIClient(**settings.get("betfairlightweight"))
     else:
         username = os.environ.get("username")
         return APIClient(username=username)
Example #2
0
    def test_session_timeout(self):
        client = APIClient("bf_username", "password", "app_key")
        assert client.session_timeout == 86400

        client = APIClient("bf_username",
                           "password",
                           "app_key",
                           locale="italy")
        assert client.session_timeout == 1200
def make_todays_feature_set(elo_ratings_dict, trueskill_ratings_dict,
                            ave_margin_dict):
    username = os.environ.get('BF_USERNAME_JAMES')
    pw = os.environ.get('BF_PW_JAMES')
    app_key = os.environ.get('BF_APP_KEY_JAMES')

    trading = APIClient(username, pw, app_key=app_key, lightweight=True)
    trading.login_interactive()

    nrl_competition_id = '10564377'

    # Define a market filter
    event_filter = betfairlightweight.filters.market_filter(
        market_type_codes=['MATCH_ODDS'], competition_ids=[nrl_competition_id])

    # Get upcoming events from bf api
    event_info = get_upcoming_event_info(trading, event_filter)

    ###########
    # Map elo
    ###########
    event_info['elo_1'] = event_info.team_1.map(elo_ratings_dict)
    event_info['elo_2'] = event_info.team_2.map(elo_ratings_dict)
    event_info['elo_prob_1'] = 1 / (1 + 10**(
        (event_info.elo_2 - event_info.elo_1) / 400))
    event_info['elo_prob_2'] = 1 - event_info['elo_prob_1']
    event_info['elo_odds_1'] = 1 / event_info['elo_prob_1']
    event_info['elo_odds_2'] = 1 / event_info['elo_prob_2']

    ##########
    # Map trueskill
    ##########
    event_info['trueskill_mu_1'] = event_info.team_1.map(
        lambda x: trueskill_ratings_dict[x].mu)
    event_info['trueskill_mu_2'] = event_info.team_2.map(
        lambda x: trueskill_ratings_dict[x].mu)
    event_info['trueskill_sigma_1'] = event_info.team_1.map(
        lambda x: trueskill_ratings_dict[x].sigma)
    event_info['trueskill_sigma_2'] = event_info.team_2.map(
        lambda x: trueskill_ratings_dict[x].sigma)

    ##########
    # Map margin
    ##########
    event_info['ave_margin_1'] = event_info.team_1.map(ave_margin_dict)
    event_info['ave_margin_2'] = event_info.team_2.map(ave_margin_dict)

    event_info = event_info.assign(
        localMarketStartTime=lambda df: pd.to_datetime(df.market_start_time).
        apply(lambda row: (row.replace(tzinfo=pytz.utc).astimezone(
            pytz.timezone('Australia/Melbourne')).strftime("%a %B %e, %I:%M%p")
                           )))
    return event_info
Example #4
0
def api_login(login_path = logins_dir):

    with open(logins_dir) as f:
        login_dict =  json.load(f)

    trading = APIClient(username=login_dict['my_username'],
                                           password=login_dict['my_password'],
                                           app_key=login_dict['my_app_key'],
                                           certs=login_dict['certs_path'])

    trading.login()
    print('Login Succesful.')

    return(trading)
class BaseClientTest(unittest.TestCase):

    def setUp(self):
        self.client = APIClient('username', 'password', 'app_key', '/fail/')

    def test_client_certs(self):
        with self.assertRaises(CertsError):
            print(self.client.cert)

    @mock.patch('betfairlightweight.baseclient.os.listdir')
    def test_client_certs_mocked(self, mock_listdir):
        mock_listdir.return_value = ['.DS_Store', 'client-2048.crt', 'client-2048.key']
        assert self.client.cert == ['/fail/client-2048.crt', '/fail/client-2048.key']

    def test_set_session_token(self):
        self.client.set_session_token('session_token')
        assert self.client.session_token == 'session_token'
        assert self.client._login_time is not None

    def test_get_app_key(self):
        self.client.app_key = None
        with self.assertRaises(AppKeyError):
            self.client.get_app_key()
        self.client.app_key = 'app_key'

    @mock.patch('betfairlightweight.baseclient.os.environ')
    def test_get_app_key_mocked(self, mocked_environ):
        self.client.app_key = None
        mocked_environ.__get__ = mock.Mock(return_value='app_key')
        assert self.client.get_app_key() is None

    def test_client_headers(self):
        assert self.client.login_headers == {'X-Application': '1',
                                             'content-type': 'application/x-www-form-urlencoded'}
        assert self.client.keep_alive_headers == {'Accept': 'application/json',
                                                  'X-Application': self.client.app_key,
                                                  'X-Authentication': self.client.session_token,
                                                  'content-type': 'application/x-www-form-urlencoded'}
        assert self.client.request_headers == {'X-Application': self.client.app_key,
                                               'X-Authentication': self.client.session_token,
                                               'content-type': 'application/json'}

    def test_client_logged_in_session(self):
        self.client.set_session_token('session_token')

        assert self.client.session_expired is None
        self.client._login_time = datetime.datetime(2003, 8, 4, 12, 30, 45)
        assert self.client.session_expired is True

    def test_client_logout(self):
        self.client.client_logout()
        assert self.client._login_time is None
        assert self.client.session_token is None
Example #6
0
 def setUp(self):
     self.client = APIClient(
         "bf_username",
         "password",
         "app_key",
         cert_files=os.path.normpath("/fail/client-2048.pem"),
     )
Example #7
0
    def test_base_endpoint_process_response(self):
        mock_resource = mock.Mock()

        response_list = [{}, {}]
        response = self.base_endpoint.process_response(response_list, mock_resource, None, False)
        assert type(response) == list
        assert response[0] == mock_resource()

        response_result_list = {'result': [{}, {}]}
        response = self.base_endpoint.process_response(response_result_list, mock_resource, None, False)
        assert type(response) == list
        assert response[0] == mock_resource()

        response_result = {'result': {}}
        response = self.base_endpoint.process_response(response_result, mock_resource, None, False)
        assert response == mock_resource()

        # lightweight tests
        response_list = [{}, {}]
        response = self.base_endpoint.process_response(response_list, mock_resource, None, True)
        assert response == response_list

        client = APIClient('username', 'password', 'app_key', lightweight=True)
        base_endpoint = BaseEndpoint(client)
        response_list = [{}, {}]
        response = base_endpoint.process_response(response_list, mock_resource, None, False)
        assert type(response) == list
        assert response[0] == mock_resource()
Example #8
0
 def test_base_endpoint_init(self):
     client = APIClient('username', 'password', 'app_key')
     navigation = Navigation(client)
     assert navigation.connect_timeout == 3.05
     assert navigation.read_timeout == 16
     assert navigation._error == APIError
     assert navigation.client == client
 def test_base_endpoint_init(self):
     client = APIClient('username', 'password', 'app_key')
     base_endpoint = BaseEndpoint(client)
     assert base_endpoint.connect_timeout == 3.05
     assert base_endpoint.read_timeout == 16
     assert base_endpoint._error == APIError
     assert base_endpoint.client == client
Example #10
0
 def test_base_endpoint_init(self):
     client = APIClient('username', 'password', 'app_key')
     scores = Scores(client)
     assert scores.connect_timeout == 3.05
     assert scores._error == APIError
     assert scores.client == client
     assert scores.URI == 'ScoresAPING/v1.0/'
Example #11
0
 def test_base_endpoint_init(self):
     client = APIClient("username", "password", "app_key")
     scores = Scores(client)
     assert scores.connect_timeout == 3.05
     assert scores._error == APIError
     assert scores.client == client
     assert scores.URI == "ScoresAPING/v1.0/"
Example #12
0
 def test_base_endpoint_init(self):
     client = APIClient('username', 'password', 'app_key')
     account = Account(client)
     assert account.connect_timeout == 6.05
     assert account.read_timeout == 16
     assert account._error == APIError
     assert account.client == client
     assert account.URI == 'AccountAPING/v1.0/'
Example #13
0
 def test_base_endpoint_init(self):
     client = APIClient("username", "password", "app_key")
     account = Account(client)
     assert account.connect_timeout == 6.05
     assert account.read_timeout == 16
     assert account._error == APIError
     assert account.client == client
     assert account.URI == "AccountAPING/v1.0/"
Example #14
0
 def test_base_endpoint_init(self):
     client = APIClient('username', 'password', 'app_key')
     betting = Betting(client)
     assert betting.connect_timeout == 3.05
     assert betting.read_timeout == 16
     assert betting._error == APIError
     assert betting.client == client
     assert betting.URI == 'SportsAPING/v1.0/'
Example #15
0
 def setUp(self):
     self.client = APIClient(
         "bf_username",
         "password",
         "app_key",
         cert_files=normpaths(
             ["/fail/client-2048.crt", "/fail/client-2048.key"]),
     )
Example #16
0
 def setUp(self):
     self.client = APIClient('bf_username',
                             'password',
                             'app_key',
                             cert_files=normpaths([
                                 '/fail/client-2048.crt',
                                 '/fail/client-2048.key'
                             ]))
Example #17
0
    def test_uri(self):
        client = APIClient('bf_username', 'password', 'app_key')
        assert client.locale is None
        assert client.identity_uri == 'https://identitysso.betfair.com/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='australia')
        assert client.locale == 'australia'
        assert client.identity_uri == 'https://identitysso.betfair.com/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='spain')
        assert client.locale == 'spain'
        assert client.identity_uri == 'https://identitysso.betfair.es/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.es/exchange/betting/rest/v1/en/navigation/menu.json'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='italy')
        assert client.locale == 'italy'
        assert client.identity_uri == 'https://identitysso.betfair.it/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.it/exchange/betting/rest/v1/en/navigation/menu.json'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='romania')
        assert client.locale == 'romania'
        assert client.identity_uri == 'https://idenititysso.betfair.ro'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='w_con')
        assert client.locale == 'w_con'
        assert client.identity_uri == 'https://identitysso.w-con.betfair.com'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='europe')
        assert client.locale == 'europe'
        assert client.identity_uri == 'https://identitysso.betfaironline.eu'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'
Example #18
0
 def test_base_client_init(self):
     client = APIClient('bf_username', 'password', 'app_key')
     assert client.username == 'bf_username'
     assert client.password == 'password'
     assert client.app_key == 'app_key'
     assert client.certs is None
     assert client.locale is None
     assert client._login_time is None
     assert client.session_token is None
Example #19
0
 def test_base_client_init(self):
     client = APIClient("bf_username",
                        "password",
                        "app_key",
                        lightweight=True)
     assert client.username == "bf_username"
     assert client.password == "password"
     assert client.app_key == "app_key"
     assert client.lightweight is True
     assert client.certs is None
     assert client.locale is None
     assert client._login_time is None
     assert client.session_token is None
Example #20
0
    def test_uri(self):
        client = APIClient('bf_username', 'password', 'app_key')
        assert client.locale is None
        assert client.identity_uri == 'https://identitysso.betfair.com/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'
        assert client.identity_cert_uri == 'https://identitysso-cert.betfair.com/api/'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='australia')
        assert client.locale == 'australia'
        assert client.identity_uri == 'https://identitysso.betfair.au/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'
        assert client.identity_cert_uri == 'https://identitysso-cert.betfair.com/api/'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='spain')
        assert client.locale == 'spain'
        assert client.identity_uri == 'https://identitysso.betfair.es/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.es/exchange/betting/rest/v1/en/navigation/menu.json'
        assert client.identity_cert_uri == 'https://identitysso-cert.betfair.es/api/'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='italy')
        assert client.locale == 'italy'
        assert client.identity_uri == 'https://identitysso.betfair.it/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.it/exchange/betting/rest/v1/en/navigation/menu.json'
        assert client.identity_cert_uri == 'https://identitysso-cert.betfair.it/api/'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='romania')
        assert client.locale == 'romania'
        assert client.identity_uri == 'https://identitysso.betfair.ro/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'
        assert client.identity_cert_uri == 'https://identitysso-cert.betfair.ro/api/'

        client = APIClient('bf_username',
                           'password',
                           'app_key',
                           locale='sweden')
        assert client.locale == 'sweden'
        assert client.identity_uri == 'https://identitysso.betfair.se/api/'
        assert client.api_uri == 'https://api.betfair.com/exchange/'
        assert client.navigation_uri == 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json'
        assert client.identity_cert_uri == 'https://identitysso-cert.betfair.se/api/'
Example #21
0
 def get_bet_set(self):
     uname = os.environ['BETFAIR_USERNAME']
     pword = os.environ['BETFAIR_PASSWORD']
     key = os.environ['BETFAIR_API_KEY']
     certs_dir = os.environ['BETFAIR_API_CERTS_DIR']
     return BetSet(APIClient(uname,
                             pword,
                             app_key=key,
                             certs=certs_dir,
                             locale='spain',
                             lightweight=True),
                   league_name='NCAAB',
                   bet_type='MATCH_ODDS',
                   currency='EUR',
                   request_refresh=True)
Example #22
0
 def setUp(self):
     client = APIClient("username", "password", "app_key", "UK")
     self.scores = Scores(client)
Example #23
0
 def setUp(self):
     client = APIClient('username', 'password', 'app_key', 'UK')
     self.keep_alive = KeepAlive(client)
Example #24
0
 def setUp(self):
     client = APIClient('username', 'password', 'app_key', 'UK')
     self.scores = Scores(client)
Example #25
0
 def setUp(self):
     client = APIClient("username", "password", "app_key", "UK")
     self.login = Login(client)
Example #26
0
 def setUp(self):
     self.client = APIClient('username', 'password', 'app_key', 'UK')
     self.in_play_service = InPlayService(self.client)
Example #27
0
 def setUp(self):
     self.client = APIClient('bf_username', 'password', 'app_key',
                             os.path.normpath('fail/'))
Example #28
0
 def setUp(self):
     self.client = APIClient('bf_username', 'password', 'app_key', '/fail/')
Example #29
0
class BaseClientTest(unittest.TestCase):
    def setUp(self):
        self.client = APIClient('bf_username', 'password', 'app_key', '/fail/')

    def test_client_certs(self):
        with self.assertRaises(CertsError):
            print(self.client.cert)

    @mock.patch('betfairlightweight.baseclient.os.listdir')
    def test_client_certs_mocked(self, mock_listdir):
        mock_listdir.return_value = [
            '.DS_Store', 'client-2048.crt', 'client-2048.key'
        ]
        assert self.client.cert == [
            '/fail/client-2048.crt', '/fail/client-2048.key'
        ]

    @mock.patch('betfairlightweight.baseclient.os.listdir')
    def test_client_certs_missing(self, mock_listdir):
        mock_listdir.return_value = ['.DS_Store', 'client-2048.key']
        with self.assertRaises(CertsError):
            print(self.client.cert)

    def test_set_session_token(self):
        self.client.set_session_token('session_token')
        assert self.client.session_token == 'session_token'
        assert self.client._login_time is not None

    def test_get_password(self):
        self.client.password = None
        with self.assertRaises(PasswordError):
            self.client.get_password()

    def test_get_app_key(self):
        self.client.app_key = None
        with self.assertRaises(AppKeyError):
            self.client.get_app_key()
        self.client.app_key = 'app_key'

    @mock.patch('betfairlightweight.baseclient.os.environ')
    def test_get_app_key_mocked(self, mocked_environ):
        self.client.app_key = None
        mocked_environ.__get__ = mock.Mock(return_value='app_key')
        assert self.client.get_app_key() is None

    def test_client_headers(self):
        assert self.client.login_headers == {
            'X-Application': '1',
            'content-type': 'application/x-www-form-urlencoded'
        }
        assert self.client.keep_alive_headers == {
            'Accept': 'application/json',
            'X-Application': self.client.app_key,
            'X-Authentication': self.client.session_token,
            'content-type': 'application/x-www-form-urlencoded'
        }
        assert self.client.request_headers == {
            'X-Application': self.client.app_key,
            'X-Authentication': self.client.session_token,
            'content-type': 'application/json',
            'Accept-Encoding': 'gzip, deflate',
            'Connection': 'keep-alive'
        }

    def test_client_logged_in_session(self):
        self.client.set_session_token('session_token')

        assert self.client.session_expired is None
        self.client._login_time = datetime.datetime(2003, 8, 4, 12, 30, 45)
        assert self.client.session_expired is True

    def test_client_logout(self):
        self.client.client_logout()
        assert self.client._login_time is None
        assert self.client.session_token is None
 def setUp(self):
     self.client = APIClient('username', 'password', 'app_key', '/fail/')
Example #31
0
 def setUp(self):
     client = APIClient('username', 'password', 'app_key', 'UK')
     self.betting = Betting(client)
Example #32
0
 def setUp(self):
     client = APIClient('username', 'password', 'app_key', 'UK')
     self.navigation = Navigation(client)