Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/readonly.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Example script demonstrating read-only API calls to Luno."""

import os
import time

Expand Down
2 changes: 2 additions & 0 deletions luno_python/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
"""Luno Python SDK."""

VERSION = "0.0.10"
27 changes: 17 additions & 10 deletions luno_python/base_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Base HTTP client for Luno API."""

import json
import platform

import requests
import six

try:
from json.decoder import JSONDecodeError
Expand All @@ -20,8 +21,11 @@


class BaseClient:
"""Base HTTP client for making authenticated requests to the Luno API."""

def __init__(self, base_url="", timeout=0, api_key_id="", api_key_secret=""):
"""
"""Initialise the base client.

:type base_url: str
:type timeout: float
:type api_key_id: str
Expand All @@ -34,7 +38,7 @@ def __init__(self, base_url="", timeout=0, api_key_id="", api_key_secret=""):
self.session = requests.Session()

def set_auth(self, api_key_id, api_key_secret):
"""Provides the client with an API key and secret.
"""Set the API key and secret for authentication.

:type api_key_id: str
:type api_key_secret: str
Expand All @@ -43,7 +47,7 @@ def set_auth(self, api_key_id, api_key_secret):
self.api_key_secret = api_key_secret

def set_base_url(self, base_url):
"""Overrides the default base URL. For internal use.
"""Set the base URL for API requests.

:type base_url: str
"""
Expand All @@ -52,7 +56,7 @@ def set_base_url(self, base_url):
self.base_url = base_url.rstrip("/")

def set_timeout(self, timeout):
"""Sets the timeout, in seconds, for requests made by the client.
"""Set the timeout in seconds for API requests.

:type timeout: float
"""
Expand All @@ -61,9 +65,9 @@ def set_timeout(self, timeout):
self.timeout = timeout

def do(self, method, path, req=None, auth=False):
"""Performs an API request and returns the response.
"""Perform an API request and return the response.

TODO: Handle 429s
TODO: Handle 429s.

:type method: str
:type path: str
Expand All @@ -76,7 +80,8 @@ def do(self, method, path, req=None, auth=False):
try:
params = json.loads(json.dumps(req))
except TypeError as e:
raise TypeError("luno: request parameters must be JSON-serializable: %s" % str(e)) from e
msg = "luno: request parameters must be JSON-serializable: %s"
raise TypeError(msg % str(e)) from e
headers = {"User-Agent": self.make_user_agent()}
args = dict(timeout=self.timeout, params=params, headers=headers)
if auth:
Expand All @@ -92,7 +97,8 @@ def do(self, method, path, req=None, auth=False):
raise Exception("luno: unknown API error (%s)" % res.status_code)

def make_url(self, path, params):
"""
"""Construct the full URL for an API request.

:type path: str
:rtype: str
"""
Expand All @@ -102,7 +108,8 @@ def make_url(self, path, params):
return self.base_url + "/" + path.lstrip("/")

def make_user_agent(self):
"""
"""Generate the User-Agent string for API requests.

:rtype: str
"""
return f"LunoPythonSDK/{VERSION} python/{PYTHON_VERSION} {SYSTEM} {ARCH}"
Loading