feature/ add more feature with api-collections

This commit is contained in:
Reena-cell 2022-12-06 09:11:47 +01:00
parent 84163a066d
commit 33e2149018
7 changed files with 66 additions and 17 deletions

View File

@ -137,6 +137,13 @@ DATABASE = {
"PORT": "5432",
}
}
# This is an optional setting.
# CALLBACK_BASE_URL can be used to explicitly set the redirect base url that API Manager uses during OAuth authentication.
# If CALLBACK_BASE_URL is not set, API Manager (this applicaiton) in the function "get_redirect_url" will use the Django HTTP_HOST environ field (see here: https://docs.djangoproject.com/en/4.1/ref/request-response/). Note, this might be modified by NGINX via the directive: proxy_set_header Host $http_host;
# In order to be explicit you can set it here e.g.
# CALLBACK_BASE_URL="https://apimanager.example.com"
```

View File

@ -1,7 +1,7 @@
{% extends 'base.html' %} {% load static %} {% load i18n %}
{% block page_title %} {{ block.super }} / {% trans "Customer List" %}{% endblock page_title %} {% block content %}
<div id="apicollectionlist">
<h1>{% trans "My API Collection List" %}</h1>
<h1>{% trans "API Collection List" %}</h1>
<form class="form-inline" method="get">
<input type="submit" class="btn btn-default" value ='{% trans "Export CSV" %}' onclick="javascript: form.action='{% url 'export-csv-apicollection' %}';">
</form>
@ -9,17 +9,17 @@
<table class="table table-hover tablesorter" id="apicollectionlist" aria-describedby="apicollectionlist list">
<thead>
<th scope="col">{% trans "API Collection Id" %}</th>
<th scope="col">{% trans "User Id" %}</th>
<th scope="col">{% trans "User Name" %}</th>
<th scope="col">{% trans "API Collection Name" %}</th>
<th scope="col">{% trans "More info" %}</th>
</thead>
<tbody>
{% for apicollection in apicollections_list %}
{% url 'apicollection_update' apicollection.api_collection_id apicollection.user_id as url_apicollection_update %}
{% url 'my-api-collection-detail' apicollection.api_collection_id as url_collection_detail %}
<tr id="{{ apicollection.api_collection_id }}">
<td>{{ apicollection.api_collection_id }}</td>
<td>{{ apicollection.user_id }}</td>
<td>{{ apicollection.username }}</td>
<td>{{ apicollection.api_collection_name }}</td>
<td>
<div class="popuptext">
@ -32,7 +32,7 @@
</ul>
</div>
</td>
<td><a href="{{ url_apicollection_update }}" class="btn btn-primary">{% trans "View" %}</a></td>
<td><a type= "button" class="btn btn-primary btn-primary" href= "{{ url_collection_detail }}">{% trans "View" %}</a></td>
</tr>
{% endfor %}
</tbody>

View File

@ -23,13 +23,21 @@ class ApiCollectionListView(IndexView, LoginRequiredMixin, FormView ):
success_url = '/apicollections/list'
def get_apicollections(self, context):
"""Get All user """
api = API(self.request.session.get('obp'))
try:
apicollections_list = []
urlpath = '/my/api-collections'
result = api.get(urlpath)
if 'api_collections' in result:
apicollections_list.extend(result['api_collections'])
#TODO: NOTE- THIS RETURNS ALL USER. THIS IS A POTENTIAL PERFORMANCE ISSUE. WE NEED ENDPOINT FOR COLLECTION.
#Endpoint will return users.
get_all_users_url_path = '/users'
user = api.get(get_all_users_url_path)
for i in user["users"]:
# Returns the APIs collections for a user.
api_collections_for_user_url_path = '/users/{}/api-collections'.format(i["user_id"])
api_collections_for_user = api.get(api_collections_for_user_url_path)
if 'api_collections' in api_collections_for_user:
apicollections_list.extend(api_collections_for_user['api_collections'])
except APIError as err:
messages.error(self.request, err)
return []
@ -40,8 +48,16 @@ class ApiCollectionListView(IndexView, LoginRequiredMixin, FormView ):
return apicollections_list
def get_context_data(self, **kwargs):
api = API(self.request.session.get('obp'))
context = super(IndexView, self).get_context_data(**kwargs)
apicollections_list = self.get_apicollections(context)
try:
for final_collection_list in apicollections_list:
url_path = "/users/user_id/{}".format(final_collection_list["user_id"])
result = api.get(url_path)
final_collection_list["username"] = result["username"]
except Exception as e:
messages.error(self.request, "Unknown Error {}".format(str(e)))
context.update({
'apicollections_list': apicollections_list,
})

View File

@ -26,7 +26,7 @@
<div class="row">
<div class="col-xs-12 col-sm-2">
<div class="form-group" cols="1" rows="1">
<a class="api_collection_id" href="{{ url_collection_detail }}">{{ api_collection.api_collection_id }}</a></div>
<a class="api_collection_id" href ="#" onclick="api_explorer_url_locale('{{api_collection.collection_on_api_explorer_url}}')" target="_blank">{{ api_collection.api_collection_id }}</a></div>
</div>
{% if api_collection.api_collection_id %}
<div class="col-xs-12 col-sm-2">
@ -60,14 +60,19 @@
</div>
{% endif %}
{% if forloop.counter0 == 0 %}
<div class="col-sm-12 col-sm-2">
<div class="col-sm-6 col-sm-2">
<div class="form-group">
<button class="btn btn-primary btn-green forSave">{% trans "Create" %}</button>
</div>
</div>
{% endif %}
{% if forloop.counter0 > 0 %}
<div class="col-sm-12 col-sm-2">
<div class="col-sm-3 col-sm-1">
<div class="form-group">
<a type= "button" class="btn btn-primary btn-primary" href="{{ url_collection_detail }}">{% trans "View" %}</a>
</div>
</div>
<div class="col-sm-3 col-sm-1">
<div class="form-group">
<button class="btn btn-primary btn-red forDelete">{% trans "Delete" %}</button>
</div>
@ -81,5 +86,16 @@
{% block extrajs %}
<script>
function api_explorer_url_locale(collection_on_api_explorer_url) {
var currentURL = window.location.href.split("/");
if (currentURL[3] == "en") {
location.href = collection_on_api_explorer_url + "&locale=en_GB";
}
else {
location.href = collection_on_api_explorer_url + "&locale=es_ES";
}
}
</script>
<script type="text/javascript" src="{% static 'apicollections/js/apicollections.js' %}"></script>
{% endblock extrajs %}

View File

@ -13,7 +13,7 @@ from django.urls import reverse, reverse_lazy
from base.utils import exception_handle, error_once_only
from .forms import ApiCollectionsForm, ApiCollectionEndpointsForm
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
class IndexView(LoginRequiredMixin, FormView):
"""Index view for API Collection"""
@ -32,6 +32,8 @@ class IndexView(LoginRequiredMixin, FormView):
error_once_only(self.request, response['message'])
else:
api_collections=response['api_collections']
for locale in api_collections:
locale["collection_on_api_explorer_url"] = f"{settings.API_EXPLORER}/?api-collection-id={locale['api_collection_id']}"
except APIError as err:
messages.error(self.request, err)
except BaseException as err:
@ -113,6 +115,8 @@ class DeleteCollectionEndpointView(LoginRequiredMixin, FormView):
"""Deletes api collection endpoint from API"""
api = API(self.request.session.get('obp'))
try:
get_api_collection_by_Id_url = "/my/api-collections/{}".format(kwargs["api_collection_id"])
result = api.get(get_api_collection_by_Id_url)
urlpath = '/my/api-collections/{}/api-collection-endpoints/{}'.format(kwargs['api_collection_name'],kwargs['operation_id'])
result = api.delete(urlpath)
if result is not None and 'code' in result and result['code']>=400:

View File

@ -253,6 +253,7 @@ API_MANAGER_DATE_FORMAT= '%Y-%m-%d'
API_HOST = 'http://127.0.0.1:8080'
API_EXPLORER = 'http://127.0.0.1:8082'
# Only override this if you have a separate portal instance
API_PORTAL = API_HOST
API_BASE_PATH = '/obp/v'
@ -301,13 +302,14 @@ LOGO_URL = 'https://static.openbankproject.com/images/OBP/favicon.png'
OVERRIDE_CSS_URL = None
VERIFY = True
CALLBACK_BASE_URL = ""
# Local settings can override anything in here
# Local settings can replace any value ABOVE
try:
from apimanager.local_settings import * # noqa
except ImportError:
pass
# EVERYTHING BELOW HERE WILL NOT BE OVERWRITTEN BY LOCALSETTINGS!
# EVERYTHING BELOW HERE WILL *NOT* BE OVERWRITTEN BY LOCALSETTINGS!
# DO NOT TRY TO DO SO YOU WILL BE IGNORED!
# Settings here might use parts overwritten in local settings

View File

@ -16,6 +16,7 @@ from .api import API, APIError
from .authenticator import AuthenticatorError
from .forms import DirectLoginForm, GatewayLoginForm
from .oauth import OAuthAuthenticator
from django.conf import settings
class LoginToDjangoMixin(object):
@ -53,8 +54,11 @@ class OAuthInitiateView(RedirectView):
Gets the callback URI to where the user shall be returned after
initiation at OAuth server
"""
base_url = '{}://{}'.format(
request.scheme, request.environ['HTTP_HOST'])
if settings.CALLBACK_BASE_URL:
base_url = settings.CALLBACK_BASE_URL
else:
base_url = '{}://{}'.format(
request.scheme, request.environ['HTTP_HOST'])
uri = base_url + reverse('oauth-authorize')
if 'next' in request.GET:
uri = '{}?next={}'.format(uri, request.GET['next'])