summaryrefslogtreecommitdiffstats
path: root/vio/vio/swagger/views/fakeplugin/execute/views.py
blob: 9b34907569376be17efebfb4e1dd660059635653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) 2019 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response

import json
import requests


class FakeExecute(APIView):

    def post(self, request):
        try:
            reqbody = json.loads(request.body)
            kwargs = {
                "verify": False,
            }
            url = reqbody["url"]
            method = reqbody.get("method", "get").lower()
            kwargs['headers'] = {
                "Accept": "application/json",
                "Content-Type": "application/json",
                "x-hm-authorization": "3d1f2ab6:8073:4da4:8f1a:eb2c349ba24",
            }
            kwargs['headers'].update(reqbody.get("headers", {}))
            if reqbody.get("body"):
                kwargs['json'] = reqbody["body"]
            resp = requests.request(method, url, **kwargs)
            respHeaders = resp.headers
            for k in ["Connection", "Keep-Alive", "Transfer-Encoding"]:
                if k in respHeaders:
                    del respHeaders[k]
            try:
                respData = resp.json()
            except Exception:
                respData = resp.content
        except Exception as ex:
            return Response(
                data={"error": str(ex)},
                status=status.HTTP_400_BAD_REQUEST)
        return Response(
            data=respData,
            status=resp.status_code,
            headers=respHeaders,
            content_type=respHeaders.get("Content-Type"))