API_Version

This commit is contained in:
Reena Aheer 2023-04-18 22:30:36 +02:00
parent 2a6dbb89f5
commit 77f0cdfe65
4 changed files with 23 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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