From cfa10d910202c29177905f463cdef50f54c92939 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Thu, 14 Oct 2021 10:55:41 +0200 Subject: [PATCH] feature/added the add/delete apiCollectionEndpoint --- apimanager/apicollections/forms.py | 11 +++ .../templates/apicollections/detail.html | 43 +++++++++ .../templates/apicollections/index.html | 10 +- apimanager/apicollections/urls.py | 14 ++- apimanager/apicollections/views.py | 91 ++++++++++++++++++- 5 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 apimanager/apicollections/templates/apicollections/detail.html diff --git a/apimanager/apicollections/forms.py b/apimanager/apicollections/forms.py index 2f77fb2..936c630 100644 --- a/apimanager/apicollections/forms.py +++ b/apimanager/apicollections/forms.py @@ -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 ) \ No newline at end of file diff --git a/apimanager/apicollections/templates/apicollections/detail.html b/apimanager/apicollections/templates/apicollections/detail.html new file mode 100644 index 0000000..7108758 --- /dev/null +++ b/apimanager/apicollections/templates/apicollections/detail.html @@ -0,0 +1,43 @@ +{% extends 'base.html' %} +{% load static %} +{% block content %} +
+

Add Api Collection

+
+ {% csrf_token %} +
+
+
+ {{ form.operation_id.label_tag}} + {{ form.operation_id }} +
+ + +
+
+
+ +

Existing API Collections

+
+ + + + + + {% for api_collection_endpoint in api_collection_endpoints %} + + + + + {% endfor %} + +
Operation Ids
{{ api_collection_endpoint.operation_id }} +
+ {% csrf_token %} + +
+
+
+
+{% endblock %} diff --git a/apimanager/apicollections/templates/apicollections/index.html b/apimanager/apicollections/templates/apicollections/index.html index 8bc020a..6e8d672 100644 --- a/apimanager/apicollections/templates/apicollections/index.html +++ b/apimanager/apicollections/templates/apicollections/index.html @@ -6,23 +6,25 @@

API Collections

-
+
-
+
-
+
{% csrf_token %} {% for api_collection in api_collections %} + {% url 'my-api-collection-detail' api_collection.api_collection_id as url_collection_detail %}
-
{{ api_collection.api_collection_id }}
+ {{ api_collection.api_collection_id }} +
diff --git a/apimanager/apicollections/urls.py b/apimanager/apicollections/urls.py index afb0f13..84fbe65 100644 --- a/apimanager/apicollections/urls.py +++ b/apimanager/apicollections/urls.py @@ -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[\w\@\.\+-]+)$', + DetailView.as_view(), + name='my-api-collection-detail'), + url(r'^delete/api-collections/(?P[\w-]+)/api-collection-endpoint/(?P[\w\@\.\+-]+)$', + DeleteCollectionEndpointView.as_view(), + name='delete-api-collection-endpoint'), + # url(r'^add/api-collections/(?P[\w-]+)/api-collection-endpoints/(?P[\w\@\.\+-]+)$', + # AddCollectionEndpointView.as_view(), + # name='add-api-collection-endpoint'), ] diff --git a/apimanager/apicollections/views.py b/apimanager/apicollections/views.py index 8236093..1b39dd3 100644 --- a/apimanager/apicollections/views.py +++ b/apimanager/apicollections/views.py @@ -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):