Skip to content

Commit

Permalink
Add krakenex.API.set_connection() method to set default connection.
Browse files Browse the repository at this point in the history
Otherwise, setting a connection within a function won't work once the
function has returned (lost scope).
  • Loading branch information
veox committed May 1, 2014
1 parent 108fa31 commit af23823
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions krakenex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ class API:
query_private
"""
def __init__(self, key = '', secret = ''):
def __init__(self, key = '', secret = '', conn = None):
"""Create an object with authentication information.
Arguments:
key -- key required to make queries to the API (default: '')
secret -- private key used to sign API messages (default: '')
conn -- krakenex.Connection object for connection reuse
(default: None)
"""
self.key = key
self.secret = secret
self.uri = 'https://api.kraken.com'
self.apiversion = '0'
self.conn = conn


def load_key(self, path):
Expand All @@ -64,6 +67,16 @@ def load_key(self, path):
self.secret = f.readline().strip()


def set_connection(self, conn):
"""Set an existing connection to be used as a default in queries.
Argument:
conn -- connection (krakenex.Connection object, no default)
"""
self.conn = conn


def _query(self, urlpath, req = {}, conn = None, headers = {}):
"""Low-level query handling.
Expand All @@ -77,7 +90,10 @@ def _query(self, urlpath, req = {}, conn = None, headers = {}):
url = self.uri + urlpath

if conn is None:
conn = connection.Connection()
if self.conn is None:
conn = connection.Connection()
else:
conn = self.conn

ret = conn._request(url, req, headers)
return json.loads(ret)
Expand Down
2 changes: 1 addition & 1 deletion krakenex/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, uri = 'api.kraken.com', timeout = 30):
timeout -- blocking operations' timeout in seconds (default: 30)
"""
self.headers = {
'User-Agent': 'krakenex/0.0.4.1 (+https://github.com/veox/python3-krakenex)'
'User-Agent': 'krakenex/0.0.5 (+https://github.com/veox/python3-krakenex)'
}

self.conn = http.client.HTTPSConnection(uri, timeout = timeout)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from distutils.core import setup

setup(name='krakenex',
version='0.0.4.1',
version='0.0.5',
description='kraken.com cryptocurrency exchange API',
author='Noel Maersk',
author_email='veox@wemakethings.net',
Expand Down

0 comments on commit af23823

Please sign in to comment.