From fa64ba409541998c82e0e472fb831412155c5f06 Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Thu, 22 Feb 2018 19:05:37 +0100 Subject: [PATCH] improve application's webservices config (#22064) --- mandayejs/applications/__init__.py | 47 +++++++++++++++++++++++++++----------- mandayejs/applications/views.py | 4 ++-- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/mandayejs/applications/__init__.py b/mandayejs/applications/__init__.py index 297e574..f80ad56 100644 --- a/mandayejs/applications/__init__.py +++ b/mandayejs/applications/__init__.py @@ -20,7 +20,7 @@ import os from importlib import import_module from django.conf import settings -from django.conf.urls import patterns, url +from django.conf.urls import url from django.http import Http404 from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import resolve @@ -35,8 +35,8 @@ def get_app_settings(): def app_web_services(request, path): app = get_app_settings() - if hasattr(app, 'urlpatterns'): - view, args, kwargs = resolve(request.path, urlconf=app) + if hasattr(app, 'SITE_WEBSERVICES'): + view, args, kwargs = resolve(request.path, urlconf=app.SITE_WEBSERVICES) return view(request, *args, **kwargs) raise Http404 @@ -90,6 +90,28 @@ class AppSettings(object): return slugify(type(self).__name__) +class AppWebservice(object): + '''- endpoints = [ + {'foo': 'https://example.org/foo/'}, + {'bar': 'https://example.org/bar/'}, + ] + - urlpatterns = [ + url('user/(?P\w+)/$', views.myapp.user_details, name='user-details'), + url('user/(?P\w+/books/$)', views.myapp.user_books, name='user-books'), + ] + ''' + + def __init__(self, endpoints=None, urlpatterns=None): + self.endpoints = endpoints + self.urlpatterns = urlpatterns + + def get_endpoint(self, name): + for endpoint in self.endpoints: + if endpoint.get(name, None): + return endpoint[name] + return None + + # Test App Settings class Test(AppSettings): SITE_LOGIN_PATH = '/' @@ -188,16 +210,15 @@ class Archimed(AppSettings): SITE_FORCE_REDIRECT_LOCATOR = '.connectBox' - SITE_WS_ENDPOINT = { - 'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount', - } - - urlpatterns = patterns( - '', - url( - r'account/(?P\w+)/$', - 'mandayejs.applications.views.archimed_account_details', - name='archimed-account-details'), + SITE_WEBSERVICES = AppWebservice( + [ + {'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount'}, + {'login_url': '/DEFAULT/Ermes/Recherche/logon.svc/logon'} + ], + [ + url(r'account/(?P\w+)/$', 'mandayejs.applications.views.archimed_account_details', + name='archimed-account-details') + ] ) SITE_LOGOUT_LOCATOR = '.account_logoff' diff --git a/mandayejs/applications/views.py b/mandayejs/applications/views.py index 40fef02..1926d7a 100644 --- a/mandayejs/applications/views.py +++ b/mandayejs/applications/views.py @@ -44,7 +44,7 @@ class ArchimedAccountDetails(APIView): logger = logging.getLogger(__name__) app_settings = get_app_settings() ws_uri = request.build_absolute_uri( - app_settings.SITE_WS_ENDPOINT['account_details']) + app_settings.SITE_WEBSERVICES.get_endpoint('account_details')) # mellon truncates username to 30 characters # thus the passed username must be truncated to 30 characters @@ -62,7 +62,7 @@ class ArchimedAccountDetails(APIView): return Response('User %s is not associated' % username, status=status.HTTP_404_NOT_FOUND) login_url = request.build_absolute_uri( - '/DEFAULT/Ermes/Recherche/logon.svc/logon') + app_settings.SITE_WEBSERVICES.get_endpoint('login_url')) with requests.Session() as session: login_info = credentials.to_login_info(decrypt=True) -- 2.11.0