baremetrics: Simplify response generation.

This commit is contained in:
neiljp (Neil Pilgrim) 2018-05-27 20:02:37 -07:00 committed by showell
parent 016011437e
commit b51b6d0361

View file

@ -110,12 +110,13 @@ class BaremetricsHandler(object):
account_data = account_response.json() account_data = account_response.json()
account_data = account_data['account'] account_data = account_data['account']
response = '**Your account information:** \n' template = ['**Your account information:** ',
response += 'Id: {id}\n'.format(id=account_data['id']) 'Id: {id}',
response += 'Company: {company}\n'.format(company=account_data['company']) 'Company: {company}',
response += 'Default Currency: {currency_name}'.format(currency_name=account_data['default_currency']['name']) 'Default Currency: {currency}']
return response return "\n".join(template).format(currency=account_data['default_currency']['name'],
**account_data)
def get_sources(self) -> str: def get_sources(self) -> str:
url = 'https://api.baremetrics.com/v1/sources' url = 'https://api.baremetrics.com/v1/sources'
@ -126,9 +127,9 @@ class BaremetricsHandler(object):
response = '**Listing sources:** \n' response = '**Listing sources:** \n'
for index, source in enumerate(sources_data): for index, source in enumerate(sources_data):
response += '{}.ID: {}\nProvider: {}\nProvider ID: {}\n\n'.format(index + 1, source['id'], response += ('{_count}.ID: {id}\n'
source['provider'], 'Provider: {provider}\n'
source['provider_id']) 'Provider ID: {provider_id}\n\n').format(_count=index + 1, **source)
return response return response
@ -139,18 +140,19 @@ class BaremetricsHandler(object):
plans_data = plans_response.json() plans_data = plans_response.json()
plans_data = plans_data['plans'] plans_data = plans_data['plans']
template = '{}.Name: {}\nActive: {}\nInterval: {}\nInterval Count: {}\nAmounts: \n' template = '\n'.join(['{_count}.Name: {name}',
response = '**Listing plans:** \n' 'Active: {active}',
'Interval: {interval}',
'Interval Count: {interval_count}',
'Amounts: '])
response = ['**Listing plans:** ']
for index, plan in enumerate(plans_data): for index, plan in enumerate(plans_data):
response += template.format(index + 1, plan['name'], plan['active'], plan['interval'], response += ([template.format(_count=index + 1, **plan)] +
plan['interval_count']) [' - {amount} {currency}'.format(**amount)
for amount in plan['amounts']] +
[''])
for amount in plan['amounts']: return '\n'.join(response + [''])
response += ' - {} {}\n'.format(amount['amount'], amount['currency'])
response += '\n'
return response
def get_customers(self, source_id: str) -> str: def get_customers(self, source_id: str) -> str:
url = 'https://api.baremetrics.com/v1/{}/customers'.format(source_id) url = 'https://api.baremetrics.com/v1/{}/customers'.format(source_id)
@ -159,18 +161,22 @@ class BaremetricsHandler(object):
customers_data = customers_response.json() customers_data = customers_response.json()
customers_data = customers_data['customers'] customers_data = customers_data['customers']
template = '{}.Name: {}\nDisplay Name: {}\nOID: {}\nActive: {}\nEmail: {}\nNotes: {}\nCurrent Plans: \n' # FIXME BUG here? mismatch of name and display name?
response = '**Listing customers:** \n' template = '\n'.join(['{_count}.Name: {display_name}',
'Display Name: {name}',
'OID: {oid}',
'Active: {is_active}',
'Email: {email}',
'Notes: {notes}',
'Current Plans: '])
response = ['**Listing customers:** ']
for index, customer in enumerate(customers_data): for index, customer in enumerate(customers_data):
response += template.format(index + 1, customer['display_name'], customer['name'], customer['oid'], response += ([template.format(_count=index + 1, **customer)] +
customer['is_active'], customer['email'], customer['notes']) [' - {name}'.format(**plan)
for plan in customer['current_plans']] +
[''])
for plan in customer['current_plans']: return '\n'.join(response + [''])
response += ' - {}\n'.format(plan['name'])
response += '\n'
return response
def get_subscriptions(self, source_id: str) -> str: def get_subscriptions(self, source_id: str) -> str:
url = 'https://api.baremetrics.com/v1/{}/subscriptions'.format(source_id) url = 'https://api.baremetrics.com/v1/{}/subscriptions'.format(source_id)
@ -179,21 +185,24 @@ class BaremetricsHandler(object):
subscriptions_data = subscriptions_response.json() subscriptions_data = subscriptions_response.json()
subscriptions_data = subscriptions_data['subscriptions'] subscriptions_data = subscriptions_data['subscriptions']
template = '{}.Customer Name: {}\nCustomer Display Name: {}\nCustomer OID: {}\nCustomer Email: {}\n' \ template = '\n'.join(['{_count}.Customer Name: {name}',
'Active: {}\nPlan Name: {}\nPlan Amounts: \n' 'Customer Display Name: {display_name}',
response = '**Listing subscriptions:** \n' 'Customer OID: {oid}',
'Customer Email: {email}',
'Active: {_active}',
'Plan Name: {_plan_name}',
'Plan Amounts:' ])
response = ['**Listing subscriptions:** ']
for index, subscription in enumerate(subscriptions_data): for index, subscription in enumerate(subscriptions_data):
response += template.format(index + 1, subscription['customer']['name'], response += ([template.format(_count=index + 1,
subscription['customer']['display_name'], _active=subscription['active'],
subscription['customer']['oid'], subscription['customer']['email'], _plan_name=subscription['plan']['name'],
subscription['active'], subscription['plan']['name']) **subscription['customer'])] +
[' - {amount} {symbol}'.format(**amount)
for amount in subscription['plan']['amounts']] +
[''])
for amount in subscription['plan']['amounts']: return '\n'.join(response + [''])
response += ' - {} {}\n'.format(amount['amount'], amount['symbol'])
response += '\n'
return response
def create_plan(self, parameters: List[str]) -> str: def create_plan(self, parameters: List[str]) -> str:
data_header = { data_header = {