improve application's webservices config (#22064)

This commit is contained in:
Josue Kouka 2018-02-22 19:05:37 +01:00
parent 7c2b416140
commit e10c859365
2 changed files with 36 additions and 15 deletions

View File

@ -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<username>\w+)/$', views.myapp.user_details, name='user-details'),
url('user/(?P<username>\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<username>\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<username>\w+)/$', 'mandayejs.applications.views.archimed_account_details',
name='archimed-account-details')
]
)
SITE_LOGOUT_LOCATOR = '.account_logoff'

View File

@ -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)