From 77f0cdfe65b5cffb1ba5eb4dd2aabb71e5d112b9 Mon Sep 17 00:00:00 2001 From: Reena Aheer Date: Tue, 18 Apr 2023 22:30:36 +0200 Subject: [PATCH] API_Version --- apimanager/apimanager/settings.py | 2 +- apimanager/atms/views.py | 4 ++- apimanager/base/context_processors.py | 4 +-- apimanager/obp/api.py | 50 +++++++++------------------ 4 files changed, 23 insertions(+), 37 deletions(-) diff --git a/apimanager/apimanager/settings.py b/apimanager/apimanager/settings.py index 9b68ed5..4b38fd2 100644 --- a/apimanager/apimanager/settings.py +++ b/apimanager/apimanager/settings.py @@ -324,7 +324,7 @@ API_ROOT = { "v510": OBPv510 } # For some reason, swagger is not available at the latest API version -API_URL_SWAGGER = API_HOST + '/obp/v1.4.0/resource-docs/v' + OBPv510 + '/swagger' # noqa +API_URL_SWAGGER = API_HOST + '/obp/v1.4.0/resource-docs/v' + OBPv500 + '/swagger' # noqa if not OAUTH_CONSUMER_KEY: raise ImproperlyConfigured('Missing settings for OAUTH_CONSUMER_KEY') diff --git a/apimanager/atms/views.py b/apimanager/atms/views.py index 277adde..7a5a0ed 100644 --- a/apimanager/atms/views.py +++ b/apimanager/atms/views.py @@ -17,6 +17,7 @@ from django.utils.translation import ugettext_lazy as _ from django.views.decorators.csrf import csrf_exempt from django.urls import reverse, reverse_lazy from base.utils import exception_handle, error_once_only +from django.conf import settings CHOOSE = "Choose..." @@ -203,6 +204,7 @@ class UpdateAtmsView(LoginRequiredMixin, FormView): template_name = "atms/update.html" success_url = '/atms/list' form_class = CreateAtmForm + v510 = settings.API_ROOT['v510'] def dispatch(self, request, *args, **kwargs): self.api = API(request.session.get('obp')) @@ -374,7 +376,7 @@ class UpdateAtmsView(LoginRequiredMixin, FormView): def atm_attributes(self, **kwargs): atm_attributes_url_path = "/banks/{}/atms/{}/attributes".format(self.kwargs['bank_id'], self.kwargs['atm_id']) try: - atm_attributes_result = self.api.get(atm_attributes_url_path)["atm_attributes"] + atm_attributes_result = self.api.get(atm_attributes_url_path, version=self.v510)["atm_attributes"] return atm_attributes_result except Exception as err: messages.error(self.request, "Unknown Error {}".format(err)) diff --git a/apimanager/base/context_processors.py b/apimanager/base/context_processors.py index a8f30c0..1ad1068 100644 --- a/apimanager/base/context_processors.py +++ b/apimanager/base/context_processors.py @@ -13,7 +13,7 @@ USER_CURRENT = "/users/current" def api_root(request): """Returns the configured API_ROOT""" - return {'API_ROOT': settings.API_ROOT["v500"]} + return {'API_ROOT': settings.API_ROOT} def portal_page(request): @@ -38,7 +38,7 @@ def api_username(request): """Returns the API username/email of the logged-in user""" nametodisplay = 'not authenticated' get_current_user_api_url = USER_CURRENT - #Here we can not get the user from obp-api side, so we use the django auth user id here. + #Here we can not get the user from obp-api side, so we use the django auth user id here. cache_key_django_user_id = request.session._session.get('_auth_user_id') cache_key = '{},{},{}'.format('api_username',get_current_user_api_url, cache_key_django_user_id) apicaches=None diff --git a/apimanager/obp/api.py b/apimanager/obp/api.py index 6c57cea..d533ddc 100644 --- a/apimanager/obp/api.py +++ b/apimanager/obp/api.py @@ -43,7 +43,7 @@ class API(object): self.start_session(session_data) self.session_data = session_data - def call(self, method='GET', url='', payload=None): + def call(self, method='GET', url='', payload=None, version=settings.API_ROOT['v500']): """Workhorse which actually calls the API""" log(logging.INFO, '{} {}'.format(method, url)) if payload: @@ -64,20 +64,18 @@ class API(object): response.execution_time = elapsed return response - def get(self, urlpath=''): + def get(self, urlpath='', version=settings.API_ROOT['v500']): """ Gets data from the API Convenience call which uses API_ROOT from settings """ - url = settings.API_ROOT["v500"] + urlpath + url = version + urlpath response = self.handle_response(self.call('GET', url)) if response is not None and 'code' in response: - url = settings.API_ROOT["v510"] + urlpath - response = self.handle_response(self.call('GET', url)) - if response is not None and 'code' in response: - raise APIError(response['message']) - return response + raise APIError(response['message']) + else: + return response def delete(self, urlpath): """ @@ -85,14 +83,9 @@ class API(object): Convenience call which uses API_ROOT from settings """ - url = settings.API_ROOT["v500"] + urlpath - response = self.handle_response(self.call('DELETE', url)) - if response is not None and 'code' in response: - url = settings.API_ROOT["v510"] + urlpath - response = self.handle_response(self.call('DELETE', url)) - if response is not None and 'code' in response: - raise APIError(response['message']) - return response + url = settings.API_ROOT + urlpath + response = self.call('DELETE', url) + return self.handle_response(response) def post(self, urlpath, payload): """ @@ -100,28 +93,19 @@ class API(object): Convenience call which uses API_ROOT from settings """ - url = settings.API_ROOT["v500"] + urlpath - response = self.handle_response(self.call('POST', url, payload)) - if response is not None and 'code' in response: - url = settings.API_ROOT["v510"] + urlpath - response = self.handle_response(self.call('POST', url, payload)) - if response is not None and 'code' in response: - raise APIError(response['message']) - return response + url = settings.API_ROOT + urlpath + response = self.call('POST', url, payload) + return self.handle_response(response) + def put(self, urlpath, payload): """ Puts data on given urlpath with given payload Convenience call which uses API_ROOT from settings """ - url = settings.API_ROOT["v500"] + urlpath - response = self.handle_response(self.call('PUT', url, payload)) - if response is not None and 'code' in response: - url = settings.API_ROOT["v510"] + urlpath - response = self.handle_response(self.call('PUT', url, payload)) - if response is not None and 'code' in response: - raise APIError(response['message']) - return response + url = settings.API_ROOT + urlpath + response = self.call('PUT', url, payload) + return self.handle_response(response) def handle_response_error(self, prefix, error): if 'Invalid or expired access token' in error: @@ -198,4 +182,4 @@ class API(object): result = self.get('/users') for user in result['users']: choices.append((user['user_id'], user['username'])) - return choices + return choices \ No newline at end of file