API-Manager/apimanager/connectormethod/views.py

78 lines
2.8 KiB
Python
Raw Permalink Normal View History

2022-08-03 11:38:46 +00:00
# -*- coding: utf-8 -*-
"""
Views of config app
"""
import json
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
2022-08-15 09:02:46 +00:00
from django.http import HttpResponseRedirect, HttpResponse
2022-08-03 11:38:46 +00:00
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
2022-08-15 09:02:46 +00:00
from .forms import ConnectorMethodForm
2022-08-03 11:38:46 +00:00
from django.views.decorators.csrf import csrf_exempt
class IndexView(LoginRequiredMixin, FormView):
2022-08-15 09:02:46 +00:00
"""Index view for config"""
template_name = r"connectormethod/index.html"
2022-08-03 11:38:46 +00:00
form_class = ConnectorMethodForm
success_url = reverse_lazy('connectormethod-index')
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
api = API(self.request.session.get('obp'))
urlpath = '/management/connector-methods'
connectormethod =[]
try:
response = api.get(urlpath)
if 'code' in response and response['code'] >= 400:
error_once_only(self.request, response['message'])
else:
connectormethod=response['connector_methods']
except APIError as err:
2022-09-22 21:43:17 +00:00
messages.error(self.request, err)
2022-12-23 11:30:33 +00:00
except Exception as err:
error_once_only(self.request, err)
2022-08-03 11:38:46 +00:00
else:
2022-08-29 22:32:57 +00:00
default_connector_method_endpoint = {
"connector_method_name": "Method Name",
"programming_lang": "Scala",
"Method Body":"Enter Your Method Body"
}
connectormethod.insert(0,json.dumps(default_connector_method_endpoint))
2022-08-03 11:38:46 +00:00
context.update({
2022-08-15 09:02:46 +00:00
'connectormethods': connectormethod
2022-08-03 11:38:46 +00:00
})
return context
@exception_handle
@csrf_exempt
def connectormethod_save(request):
api = API(request.session.get('obp'))
2022-08-15 09:02:46 +00:00
urlpath = '/management/connector-methods'
2022-08-03 11:38:46 +00:00
payload = {
2022-08-15 09:02:46 +00:00
'method_name': request.POST.get('connector_method_name').strip(),
'programming_lang': request.POST.get('connector_method_programming_lang'),
'method_body': request.POST.get('connector_method_body').strip()
2022-08-03 11:38:46 +00:00
}
result = api.post(urlpath, payload = payload)
return result
@exception_handle
@csrf_exempt
2022-08-15 09:02:46 +00:00
def connectormethod_update(request):
connector_method_id = request.POST.get('connector_method_id').strip()
urlpath = '/management/connector-methods/{}'.format(connector_method_id)
2022-08-03 11:38:46 +00:00
api = API(request.session.get('obp'))
2022-08-15 09:02:46 +00:00
payload = {
'programming_lang': request.POST.get('connector_method_programming_lang_update'),
'method_body': request.POST.get('connector_method_body_update').strip()
}
result = api.put(urlpath, payload=payload)
2022-08-03 11:38:46 +00:00
return result