mirror of
https://github.com/OpenBankProject/API-Manager.git
synced 2026-02-06 10:59:00 +00:00
feature/added the add/delete apiCollectionEndpoint
This commit is contained in:
parent
72b8557e88
commit
cfa10d9102
@ -10,4 +10,15 @@ class ApiCollectionsForm(forms.Form):
|
||||
}
|
||||
),
|
||||
required=False
|
||||
)
|
||||
|
||||
class ApiCollectionEndpointsForm(forms.Form):
|
||||
operation_id = forms.CharField(
|
||||
label='Operation Id',
|
||||
widget=forms.TextInput(
|
||||
attrs={
|
||||
'class': 'form-control',
|
||||
}
|
||||
),
|
||||
required=True
|
||||
)
|
||||
@ -0,0 +1,43 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load static %}
|
||||
{% block content %}
|
||||
<div id="api-collection-detail">
|
||||
<h2>Add Api Collection</h2>
|
||||
<form class="form" action="" method="post">
|
||||
{% csrf_token %}
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-md-4">
|
||||
<div class="form-group">
|
||||
{{ form.operation_id.label_tag}}
|
||||
{{ form.operation_id }}
|
||||
</div>
|
||||
<input type="text" class="hidden" name="api-collection-id" value={{ api_collection_id }} >
|
||||
<button type="submit" class="btn btn-primary btn-green">Add</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<h2>Existing API Collections</h2>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<th>Operation Ids</th>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for api_collection_endpoint in api_collection_endpoints %}
|
||||
<tr>
|
||||
<td>{{ api_collection_endpoint.operation_id }}</td>
|
||||
<td>
|
||||
<form action="{% url 'delete-api-collection-endpoint' api_collection_endpoint.api_collection_id api_collection_endpoint.operation_id %}"
|
||||
method="post">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn btn-primary btn-red">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@ -6,23 +6,25 @@
|
||||
<h1>API Collections</h1>
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-2">
|
||||
<label class="form-group">API COLLECTION ID:</label> <br>
|
||||
<label class="form-group">Api Collection Id:</label> <br>
|
||||
</div>
|
||||
<div class="col-xs-12 col-sm-2">
|
||||
<label class="form-group">COLLECTION Name:</label> <br>
|
||||
<label class="form-group">Collection Name:</label> <br>
|
||||
</div>
|
||||
<div class="col-xs-12 col-sm-8">
|
||||
<label class="form-group">Details:</label> <br>
|
||||
<label class="form-group">Json Body:</label> <br>
|
||||
</div>
|
||||
</div>
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
{% for api_collection in api_collections %}
|
||||
{% url 'my-api-collection-detail' api_collection.api_collection_id as url_collection_detail %}
|
||||
<div class="runner">
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-sm-2">
|
||||
<div class="form-group" cols="1" rows="1">
|
||||
<div class="api_collection_id">{{ api_collection.api_collection_id }}</div></div>
|
||||
<a class="api_collection_id" href="{{ url_collection_detail }}">{{ api_collection.api_collection_id }}
|
||||
</a></div>
|
||||
</div>
|
||||
<div class="col-xs-12 col-sm-2">
|
||||
<div class="form-group" cols="1" rows="1">
|
||||
|
||||
@ -5,7 +5,8 @@ URLs for config app
|
||||
|
||||
from django.conf.urls import url
|
||||
|
||||
from apicollections.views import IndexView, apicollections_save, apicollections_delete
|
||||
from apicollections.views import IndexView, apicollections_save, \
|
||||
apicollections_delete, DetailView, DeleteCollectionEndpointView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$',
|
||||
@ -14,5 +15,14 @@ urlpatterns = [
|
||||
url(r'save/apicollection', apicollections_save,
|
||||
name='apicollection-save'),
|
||||
url(r'delete/apicollection', apicollections_delete,
|
||||
name='apicollection-delete')
|
||||
name='apicollection-delete'),
|
||||
url(r'^my-api-collection-ids/(?P<api_collection_id>[\w\@\.\+-]+)$',
|
||||
DetailView.as_view(),
|
||||
name='my-api-collection-detail'),
|
||||
url(r'^delete/api-collections/(?P<api_collection_id>[\w-]+)/api-collection-endpoint/(?P<operation_id>[\w\@\.\+-]+)$',
|
||||
DeleteCollectionEndpointView.as_view(),
|
||||
name='delete-api-collection-endpoint'),
|
||||
# url(r'^add/api-collections/(?P<api_collection_id>[\w-]+)/api-collection-endpoints/(?P<operation_id>[\w\@\.\+-]+)$',
|
||||
# AddCollectionEndpointView.as_view(),
|
||||
# name='add-api-collection-endpoint'),
|
||||
]
|
||||
|
||||
@ -4,12 +4,14 @@ Views of config app
|
||||
"""
|
||||
|
||||
import json
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.views.generic import FormView
|
||||
from obp.api import API, APIError
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from base.utils import exception_handle, error_once_only
|
||||
from .forms import ApiCollectionsForm
|
||||
from django.urls import reverse_lazy
|
||||
from .forms import ApiCollectionsForm, ApiCollectionEndpointsForm
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
|
||||
@ -39,9 +41,9 @@ class IndexView(LoginRequiredMixin, FormView):
|
||||
else:
|
||||
# set the default endpoint there, the first item will be the new endpoint.
|
||||
default_api_endpoint = {
|
||||
"api_collection_name": "Testing",
|
||||
"api_collection_name": "Customer",
|
||||
"is_sharable": True,
|
||||
"description":"This is for testing"
|
||||
"description":"Describe the purpose of the collection"
|
||||
}
|
||||
api_collections.insert(0,json.dumps(default_api_endpoint))
|
||||
|
||||
@ -50,6 +52,87 @@ class IndexView(LoginRequiredMixin, FormView):
|
||||
})
|
||||
return context
|
||||
|
||||
class DetailView(LoginRequiredMixin, FormView):
|
||||
"""Index view for config"""
|
||||
template_name = "apicollections/detail.html"
|
||||
form_class = ApiCollectionEndpointsForm
|
||||
success_url = reverse_lazy('my-api-collection-detail')
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Posts api collection endpoint data to API"""
|
||||
try:
|
||||
data = form.cleaned_data
|
||||
api = API(self.request.session.get('obp'))
|
||||
api_collection_id = super(DetailView, self).get_context_data()['view'].kwargs['api_collection_id']
|
||||
|
||||
urlpath = '/my/api-collection-ids/{}/api-collection-endpoints'.format(api_collection_id)
|
||||
payload = {
|
||||
'operation_id': data['operation_id']
|
||||
}
|
||||
api_collection_endpoint = api.post(urlpath, payload=payload)
|
||||
except APIError as err:
|
||||
messages.error(self.request, err)
|
||||
return super(DetailView, self).form_invalid(form)
|
||||
except:
|
||||
messages.error(self.request, 'Unknown Error')
|
||||
return super(DetailView, self).form_invalid(form)
|
||||
if 'code' in api_collection_endpoint and api_collection_endpoint['code']>=400:
|
||||
messages.error(self.request, api_collection_endpoint['message'])
|
||||
return super(DetailView, self).form_invalid(form)
|
||||
else:
|
||||
msg = 'Operation Id {} has been added.'.format(data['operation_id'])
|
||||
messages.success(self.request, msg)
|
||||
self.success_url = self.request.path
|
||||
return super(DetailView, self).form_valid(form)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(DetailView, self).get_context_data(**kwargs)
|
||||
api_collection_id = context['view'].kwargs['api_collection_id']
|
||||
|
||||
api = API(self.request.session.get('obp'))
|
||||
urlpath = '/my/api-collection-ids/{}/api-collection-endpoints'.format(api_collection_id)
|
||||
api_collection_endpoints =[]
|
||||
try:
|
||||
response = api.get(urlpath)
|
||||
if 'code' in response and response['code'] >= 400:
|
||||
error_once_only(self.request, response['message'])
|
||||
else:
|
||||
api_collection_endpoints=response['api_collection_endpoints']
|
||||
except APIError as err:
|
||||
error_once_only(self.request, Exception("OBP-API server is not running or do not response properly. "
|
||||
"Please check OBP-API server. "
|
||||
"Details: " + str(err)))
|
||||
except BaseException as err:
|
||||
error_once_only(self.request, (Exception("Unknown Error. Details:" + str(err))))
|
||||
else:
|
||||
context.update({
|
||||
'api_collection_endpoints': api_collection_endpoints,
|
||||
'api_collection_id': api_collection_id
|
||||
})
|
||||
return context
|
||||
|
||||
class DeleteCollectionEndpointView(LoginRequiredMixin, FormView):
|
||||
"""View to delete an api collection endpoint"""
|
||||
def post(self, request, *args, **kwargs):
|
||||
"""Deletes api collection endpoint from API"""
|
||||
api = API(self.request.session.get('obp'))
|
||||
try:
|
||||
urlpath = '/my/api-collections-ids/{}/api-collection-endpoints/{}'\
|
||||
.format(kwargs['api_collection_id'],kwargs['operation_id'])
|
||||
result = api.delete(urlpath)
|
||||
if result is not None and 'code' in result and result['code']>=400:
|
||||
messages.error(request, result['message'])
|
||||
else:
|
||||
msg = 'Operation Id {} has been deleted.'.format(kwargs['operation_id'])
|
||||
messages.success(request, msg)
|
||||
except APIError as err:
|
||||
messages.error(request, err)
|
||||
except:
|
||||
messages.error(self.request, 'Unknown Error')
|
||||
|
||||
redirect_url = reverse('my-api-collection-detail',kwargs={"api_collection_id":kwargs['api_collection_id']})
|
||||
return HttpResponseRedirect(redirect_url)
|
||||
|
||||
@exception_handle
|
||||
@csrf_exempt
|
||||
def apicollections_save(request):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user