Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Updates toopher-python API for new features
Browse files Browse the repository at this point in the history
Allow arbitrary parameters to be passed to API using **kwargs.
Access raw API response for Pairing and Auth using __getattr__
Use terminal_name_extra in demo.py
  • Loading branch information
Drew Shafer committed Jul 26, 2013
1 parent a5a328a commit f55c910
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 13 additions & 2 deletions demo.py
@@ -1,5 +1,6 @@
import os
import sys
import uuid

import toopher
from toopher import ToopherApiError
Expand Down Expand Up @@ -29,6 +30,9 @@ def print_sep(char='-'):
while not secret:
secret = raw_input('TOOPHER_CONSUMER_SECRET=')

if os.environ.get('TOOPHER_BASE_URL'):
toopher.BASE_URL = os.environ.get('TOOPHER_BASE_URL').rstrip('/')

api = toopher.ToopherApi(key, secret)

while True:
Expand Down Expand Up @@ -69,18 +73,25 @@ def print_sep(char='-'):
raise
print 'Could not check pairing status (reason: %s)' % e

terminal_extras = {}

while True:
print 'Step 2: Authenticate log in'
print_sep()

terminal_name = raw_input('Enter a terminal name for this authentication request [%s]: ' % DEFAULT_TERMINAL_NAME)
if not terminal_name:
terminal_name = DEFAULT_TERMINAL_NAME

if terminal_name in terminal_extras:
terminal_extra = terminal_extras[terminal_name]
else:
terminal_extra = terminal_extras[terminal_name] = uuid.uuid4()

print 'Sending authentication request...'

try:
request_status = api.authenticate(pairing_id, terminal_name)
request_status = api.authenticate(pairing_id, terminal_name, terminal_name_extra=terminal_extra)
request_id = request_status.id
except ToopherApiError, e:
print 'Error initiating authentication (reason: %s)' % e
Expand All @@ -104,4 +115,4 @@ def print_sep(char='-'):
print 'The request was ' + automation + result + "!"
break

raw_input('Press return to authenticate again, or Ctrl-C to exit')
raw_input('Press return to authenticate again, or Ctrl-C to exit')
18 changes: 16 additions & 2 deletions toopher/__init__.py
Expand Up @@ -10,10 +10,12 @@ def __init__(self, key, secret):
self.client = oauth2.Client(oauth2.Consumer(key, secret))
self.client.ca_certs = os.path.join(os.path.dirname(os.path.abspath(__file__)), "toopher.pem")

def pair(self, pairing_phrase, user_name):
def pair(self, pairing_phrase, user_name, **kwargs):
uri = BASE_URL + "/pairings/create"
params = {'pairing_phrase': pairing_phrase,
'user_name': user_name}

params.update(kwargs)

result = self._request(uri, "POST", params)
return PairingStatus(result)
Expand All @@ -24,12 +26,14 @@ def get_pairing_status(self, pairing_id):
result = self._request(uri, "GET")
return PairingStatus(result)

def authenticate(self, pairing_id, terminal_name, action_name=None):
def authenticate(self, pairing_id, terminal_name, action_name=None, **kwargs):
uri = BASE_URL + "/authentication_requests/initiate"
params = {'pairing_id': pairing_id,
'terminal_name': terminal_name}
if action_name:
params['action_name'] = action_name

params.update(kwargs)

result = self._request(uri, "POST", params)
return AuthenticationStatus(result)
Expand Down Expand Up @@ -70,10 +74,15 @@ def __init__(self, json_response):
self.user_name = user['name']
except Exception:
raise ToopherApiError("Could not parse pairing status from response")

self._raw_data = json_response

def __nonzero__(self):
return self.enabled

def __getattr__(self, name):
return self._raw_data.get(name)


class AuthenticationStatus(object):
def __init__(self, json_response):
Expand All @@ -90,8 +99,13 @@ def __init__(self, json_response):
except Exception:
raise ToopherApiError("Could not parse authentication status from response")

self._raw_data = json_response

def __nonzero__(self):
return self.granted

def __getattr__(self, name):
return self._raw_data.get(name)


class ToopherApiError(Exception): pass

0 comments on commit f55c910

Please sign in to comment.