From 030254b58e0f1c5fa35eaae6382a4a01ba0bccf1 Mon Sep 17 00:00:00 2001 From: Peter Marsh Date: Fri, 10 Jul 2015 00:09:56 +0100 Subject: [PATCH] Stop inflecting API method parameters on each call Previously, every parameter of an API call was converted to camel case before being serialized to JSON - even if it had been converted before. This added unnecessary overhead to every API call.This avoids these conversions by manually constructing dicts with the correct keys from the parameters passed to the method. --- betfair/betfair.py | 53 ++++++++++++++++++++++++++----------------- betfair/meta/utils.py | 21 ----------------- betfair/utils.py | 15 +----------- tests/test_utils.py | 2 +- 4 files changed, 34 insertions(+), 57 deletions(-) delete mode 100644 betfair/meta/utils.py diff --git a/betfair/betfair.py b/betfair/betfair.py index c93f2aa..deafff1 100644 --- a/betfair/betfair.py +++ b/betfair/betfair.py @@ -146,7 +146,7 @@ def list_event_types(self, filter=None, locale=None): return self.make_api_request( 'Sports', 'listEventTypes', - utils.get_kwargs(locals()), + {'filter': filter, 'locale': locale}, model=models.EventTypeResult, ) @@ -161,7 +161,7 @@ def list_competitions(self, filter=None, locale=None): return self.make_api_request( 'Sports', 'listCompetitions', - utils.get_kwargs(locals()), + {'filter': filter, 'locale': locale}, model=models.CompetitionResult, ) @@ -176,7 +176,7 @@ def list_time_ranges(self, granularity, filter=None): return self.make_api_request( 'Sports', 'listTimeRanges', - utils.get_kwargs(locals()), + {'granularity': granularity, 'filter': filter}, model=models.TimeRangeResult, ) @@ -191,7 +191,7 @@ def list_events(self, filter=None, locale=None): return self.make_api_request( 'Sports', 'listEvents', - utils.get_kwargs(locals()), + {'filter': filter, 'locale': locale}, model=models.EventResult, ) @@ -206,7 +206,7 @@ def list_market_types(self, filter=None, locale=None): return self.make_api_request( 'Sports', 'listMarketTypes', - utils.get_kwargs(locals()), + {'filter': filter, 'locale': locale}, model=models.MarketTypeResult, ) @@ -221,7 +221,7 @@ def list_countries(self, filter=None, locale=None): return self.make_api_request( 'Sports', 'listCountries', - utils.get_kwargs(locals()), + {'filter': filter, 'locale': locale}, model=models.CountryCodeResult, ) @@ -236,7 +236,7 @@ def list_venues(self, filter=None, locale=None): return self.make_api_request( 'Sports', 'listCountries', - utils.get_kwargs(locals()), + {'filter': filter, 'locale': locale}, model=models.VenueResult, ) @@ -256,7 +256,8 @@ def list_market_catalogue( return self.make_api_request( 'Sports', 'listMarketCatalogue', - utils.get_kwargs(locals()), + {'filter': filter, 'maxResults': max_results, + 'marketProjection': market_projection, 'locale': locale}, model=models.MarketCatalogue, ) @@ -276,7 +277,9 @@ def list_market_book( return self.make_api_request( 'Sports', 'listMarketBook', - utils.get_kwargs(locals()), + {'marketIds': market_ids, 'priceProjection': price_projection, + 'orderProjection': order_projection, 'matchProjection': match_projection, + 'currencyCode': currency_code, 'locale': locale}, model=models.MarketBook, ) @@ -296,7 +299,8 @@ def list_market_profit_and_loss( return self.make_api_request( 'Sports', 'listMarketProfitAndLoss', - utils.get_kwargs(locals()), + {'marketIds': market_ids, 'includeSettledBets': include_settled_bets, + 'includeBspBets': include_bsp_bets, 'netOfCommission': net_of_commission}, model=models.MarketProfitAndLoss, ) @@ -348,7 +352,9 @@ def list_current_orders( return self.make_api_request( 'Sports', 'listCurrentOrders', - utils.get_kwargs(locals()), + {'betIds': bet_ids, 'marketIds': market_ids, 'orderProjection': order_projection, + 'dateRage': date_range, 'orderBy': order_by, 'sortDir': sort_dir, + 'fromRecord': from_record, 'recordCount': record_count}, model=models.CurrentOrderSummaryReport, ) @@ -376,7 +382,11 @@ def list_cleared_orders( return self.make_api_request( 'Sports', 'listClearedOrders', - utils.get_kwargs(locals()), + {'betStatus': bet_status, 'eventTypeIds': event_type_ids, 'eventIds': event_ids, + 'marketIds': market_ids, 'runnerIds': runner_ids, 'betIds': bet_ids, + 'side': side, 'settledDateRange': settled_date_range, 'groupBy': group_by, + 'includeItemDescription': include_item_description, 'locale': locale, + 'fromRecord': from_record, 'recordCount': record_count}, model=models.ClearedOrderSummaryReport, ) @@ -392,7 +402,7 @@ def place_orders(self, market_id, instructions, customer_ref=None): return self.make_api_request( 'Sports', 'placeOrders', - utils.get_kwargs(locals()), + {'marketId': market_id, 'instructions': instructions, 'customerRef': customer_ref}, model=models.PlaceExecutionReport, ) @@ -408,7 +418,7 @@ def cancel_orders(self, market_id, instructions, customer_ref=None): return self.make_api_request( 'Sports', 'cancelOrders', - utils.get_kwargs(locals()), + {'marketId': market_id, 'instructions': instructions, 'customerRef': customer_ref}, model=models.CancelInstructionReport, ) @@ -424,7 +434,7 @@ def replace_orders(self, market_id, instructions, customer_ref=None): return self.make_api_request( 'Sports', 'replaceOrders', - utils.get_kwargs(locals()), + {'marketId': market_id, 'instructions': instructions, 'customerRef': customer_ref}, model=models.ReplaceExecutionReport, ) @@ -439,7 +449,7 @@ def update_orders(self, market_id, instructions, customer_ref=None): return self.make_api_request( 'Sports', 'updateOrders', - utils.get_kwargs(locals()), + {'marketId': market_id, 'instructions': instructions, 'customerRef': customer_ref}, model=models.UpdateExecutionReport, ) @@ -452,7 +462,7 @@ def get_account_funds(self, wallet=None): result = self.make_api_request( 'Account', 'getAccountFunds', - utils.get_kwargs(locals()), + {'wallet': wallet}, model=models.AccountFundsResponse, ) @@ -472,7 +482,8 @@ def get_account_statement( result = self.make_api_request( 'Account', 'getAccountStatement', - utils.get_kwargs(locals()), + {'locale': locale, 'fromRecord': from_record, 'recordCount': record_count, + 'itemDateRange': item_date_range, 'includeItem': include_item, 'wallet': wallet}, model=models.AccountStatementReport, ) @@ -484,7 +495,7 @@ def get_account_details(self): result = self.make_api_request( 'Account', 'getAccountDetails', - utils.get_kwargs(locals()), + {}, model=models.AccountDetailsResponse, ) @@ -497,7 +508,7 @@ def list_currency_rates(self, from_currency=None): result = self.make_api_request( 'Account', 'listCurrencyRates', - utils.get_kwargs(locals()), + {'fromCurrency': from_currency}, model=models.CurrencyRate, ) @@ -512,6 +523,6 @@ def transfer_funds(self, from_, to, amount): result = self.make_api_request( 'Account', 'transferFunds', - utils.get_kwargs(locals()), + {'from': from_, 'to': to, 'amount': amount}, model=models.TransferResponse, ) diff --git a/betfair/meta/utils.py b/betfair/meta/utils.py deleted file mode 100644 index b42f0eb..0000000 --- a/betfair/meta/utils.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- - -import functools - -import six -import inflection - - -def convert_value(value, converter=None): - return converter(value) if converter else value - - -def convert_dict(data, key_converter=None, value_converter=None): - return { - convert_value(key, key_converter): convert_value(value, value_converter) - for key, value in six.iteritems(data) - } - - -camelize = functools.partial(inflection.camelize, uppercase_first_letter=False) -serialize_dict = functools.partial(convert_dict, key_converter=camelize) diff --git a/betfair/utils.py b/betfair/utils.py index c32d427..61163b4 100644 --- a/betfair/utils.py +++ b/betfair/utils.py @@ -2,7 +2,6 @@ from __future__ import absolute_import -import six import json import datetime import collections @@ -12,7 +11,6 @@ from six.moves import http_client as httplib from betfair import exceptions -from betfair.meta import utils from betfair.meta.models import BetfairModel @@ -28,17 +26,6 @@ def get_chunks(sequence, chunk_size): ] -def get_kwargs(kwargs): - """Get all keys and values from dictionary where key is not `self`. - - :param dict kwargs: Input parameters - """ - return { - key: value for key, value in six.iteritems(kwargs) - if key != 'self' - } - - def check_status_code(response, codes=None): """Check HTTP status code and raise exception if incorrect. @@ -107,7 +94,7 @@ def make_payload(base, method, params): payload = { 'jsonrpc': '2.0', 'method': '{base}APING/v1.0/{method}'.format(**locals()), - 'params': utils.serialize_dict(params), + 'params': params, 'id': 1, } return payload diff --git a/tests/test_utils.py b/tests/test_utils.py index e78a4be..bad9fb8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -38,7 +38,7 @@ def test_encode_model_invalid(): def test_make_payload(): - result = make_payload('Sports', 'listMarketBook', {'some_param': 123}) + result = make_payload('Sports', 'listMarketBook', {'someParam': 123}) assert result == { 'jsonrpc': '2.0', 'method': 'SportsAPING/v1.0/listMarketBook',