Merge pull request #2690 from alphagov/client-init-refactor

Refactor API client code to be less duplicative/repetitive
This commit is contained in:
Chris Hill-Scott
2019-01-30 14:06:38 +00:00
committed by GitHub
23 changed files with 47 additions and 92 deletions

View File

@@ -107,35 +107,47 @@ def create_app(application):
asset_fingerprinter._asset_root = application.config['ASSET_PATH']
init_app(application)
antivirus_client.init_app(application)
statsd_client.init_app(application)
zendesk_client.init_app(application)
for client in (
# Gubbins
csrf,
login_manager,
proxy_fix,
request_helper,
# Internal API clients
antivirus_client,
api_key_api_client,
billing_api_client,
complaint_api_client,
email_branding_client,
events_api_client,
inbound_number_client,
invite_api_client,
job_api_client,
letter_branding_client,
letter_jobs_client,
notification_api_client,
org_invite_api_client,
organisations_client,
platform_stats_api_client,
provider_client,
service_api_client,
status_api_client,
template_folder_api_client,
template_statistics_client,
user_api_client,
# External API clients
statsd_client,
zendesk_client,
):
client.init_app(application)
logging.init_app(application, statsd_client)
csrf.init_app(application)
request_helper.init_app(application)
service_api_client.init_app(application)
user_api_client.init_app(application)
api_key_api_client.init_app(application)
job_api_client.init_app(application)
notification_api_client.init_app(application)
status_api_client.init_app(application)
invite_api_client.init_app(application)
org_invite_api_client.init_app(application)
template_statistics_client.init_app(application)
events_api_client.init_app(application)
provider_client.init_app(application)
email_branding_client.init_app(application)
letter_branding_client.init_app(application)
organisations_client.init_app(application)
letter_jobs_client.init_app(application)
inbound_number_client.init_app(application)
billing_api_client.init_app(application)
complaint_api_client.init_app(application)
platform_stats_api_client.init_app(application)
template_folder_api_client.init_app(application)
login_manager.init_app(application)
login_manager.login_view = 'main.sign_in'
login_manager.login_message_category = 'default'
login_manager.session_protection = None
@@ -147,8 +159,6 @@ def create_app(application):
from .status import status as status_blueprint
application.register_blueprint(status_blueprint)
proxy_fix.init_app(application)
add_template_filters(application)
register_errorhandlers(application)

View File

@@ -16,6 +16,9 @@ class NotifyAdminAPIClient(BaseAPIClient):
redis_client = RedisClient()
def __init__(self):
super().__init__("a" * 73, "b")
def init_app(self, app):
self.base_url = app.config['API_HOST_NAME']
self.service_id = app.config['ADMIN_CLIENT_USER_NAME']

View File

@@ -7,8 +7,6 @@ KEY_TYPE_TEST = 'test'
class ApiKeyApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_api_keys(self, service_id):
return self.get(url='/service/{}/api-keys'.format(service_id))

View File

@@ -2,10 +2,6 @@ from app.notify_client import NotifyAdminAPIClient
class BillingAPIClient(NotifyAdminAPIClient):
# Fudge assert in the super __init__ so
# we can set those variables later.
def __init__(self):
super().__init__("a" * 73, "b")
def get_billable_units_ft(self, service_id, year):
return self.get(

View File

@@ -2,10 +2,6 @@ from app.notify_client import NotifyAdminAPIClient
class ComplaintApiClient(NotifyAdminAPIClient):
# Fudge assert in the super __init__ so
# we can set those variables later.
def __init__(self):
super().__init__("a" * 73, "b")
def get_all_complaints(self, page=1):
params = {'page': page}

View File

@@ -3,9 +3,6 @@ from app.notify_client import NotifyAdminAPIClient, cache
class EmailBrandingClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
@cache.set('email_branding-{branding_id}')
def get_email_branding(self, branding_id):
return self.get(url='/email-branding/{}'.format(branding_id))

View File

@@ -2,8 +2,6 @@ from app.notify_client import NotifyAdminAPIClient
class EventsApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def create_event(self, event_type, event_data):
data = {

View File

@@ -3,9 +3,6 @@ from app.notify_client import NotifyAdminAPIClient
class InboundNumberClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_available_inbound_sms_numbers(self):
return self.get(url='/inbound-number/available')

View File

@@ -6,8 +6,6 @@ from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
class InviteApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def init_app(self, app):
super().init_app(app)

View File

@@ -18,9 +18,6 @@ class JobApiClient(NotifyAdminAPIClient):
NON_SCHEDULED_JOB_STATUSES = JOB_STATUSES - {'scheduled', 'cancelled'}
def __init__(self):
super().__init__("a" * 73, "b")
@staticmethod
def __convert_statistics(job):
results = defaultdict(int)

View File

@@ -3,9 +3,6 @@ from app.notify_client import NotifyAdminAPIClient
class LetterBrandingClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_letter_branding(self):
return self.get(url='/dvla_organisations')

View File

@@ -3,9 +3,6 @@ from app.notify_client import NotifyAdminAPIClient
class LetterJobsClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def submit_returned_letters(self, references):
return self.post(
url='/letters/returned',

View File

@@ -2,8 +2,6 @@ from app.notify_client import NotifyAdminAPIClient, _attach_current_user
class NotificationApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_notifications_for_service(
self,

View File

@@ -3,8 +3,6 @@ from app.notify_client import NotifyAdminAPIClient, _attach_current_user
class OrgInviteApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def init_app(self, app):
super().init_app(app)

View File

@@ -3,9 +3,6 @@ from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
class OrganisationsClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_organisations(self):
return self.get(url='/organisations')

View File

@@ -2,10 +2,6 @@ from app.notify_client import NotifyAdminAPIClient
class PlatformStatsAPIClient(NotifyAdminAPIClient):
# Fudge assert in the super __init__ so
# we can set those variables later.
def __init__(self):
super().__init__("a" * 73, "b")
def get_aggregate_platform_stats(self, params_dict=None):
return self.get("/platform-stats", params=params_dict)

View File

@@ -3,8 +3,6 @@ from app.notify_client import NotifyAdminAPIClient, _attach_current_user
class ProviderClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_all_providers(self):
return self.get(

View File

@@ -3,10 +3,6 @@ from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
class ServiceAPIClient(NotifyAdminAPIClient):
# Fudge assert in the super __init__ so
# we can set those variables later.
def __init__(self):
super().__init__("a" * 73, "b")
@cache.delete('user-{user_id}')
def create_service(

View File

@@ -3,8 +3,6 @@ from app.notify_client import NotifyAdminAPIClient
class StatusApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_status(self, *params):
return self.get(url='/_status', *params)

View File

@@ -2,10 +2,6 @@ from app.notify_client import NotifyAdminAPIClient, cache
class TemplateFolderAPIClient(NotifyAdminAPIClient):
# Fudge assert in the super __init__ so
# we can set those variables later.
def __init__(self):
super().__init__('a' * 73, 'b')
@cache.delete('service-{service_id}-template-folders')
def create_template_folder(

View File

@@ -2,8 +2,6 @@ from app.notify_client import NotifyAdminAPIClient
class TemplateStatisticsApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def get_template_statistics_for_service(self, service_id, limit_days=None):
params = {}

View File

@@ -18,8 +18,6 @@ ALLOWED_ATTRIBUTES = {
class UserApiClient(NotifyAdminAPIClient):
def __init__(self):
super().__init__("a" * 73, "b")
def init_app(self, app):
super().init_app(app)