示例#1
0
    def post(self, request, app_id):
        context = Util.get_context(request)
        context['app_id'] = app_id
        cmd = request.POST.get('cmd', None)
        adapter = DjangoAdapter(app_id, request)
        with adapter.open_api_logic() as logic_api:
            if cmd == 'create_function':
                zip_file = request.FILES['zip_file']
                function_name = request.POST['function_name']
                description = request.POST['description']
                runtime = request.POST['runtime']
                handler = request.POST['handler']
                sdk_config = get_sdk_config(adapter)

                zip_file.seek(0)
                zip_file_bin = zip_file.read()
                zip_file_bin = base64.b64encode(zip_file_bin)
                zip_file_bin = zip_file_bin.decode('utf-8')
                if not description:
                    description = None
                logic_api.create_function(function_name, description, runtime,
                                          handler, sdk_config, zip_file_bin,
                                          True)
            elif cmd == 'create_function_test':
                test_name = request.POST.get('test_name')
                function_name = request.POST.get('function_name')
                test_input = request.POST.get('test_input')
                logic_api.create_function_test(test_name, function_name,
                                               test_input)
            elif cmd == 'run_function':
                with adapter.open_sdk() as sdk_client:
                    # TODO SDK Sign-up Login and authentication takes a lot of time.
                    #  We are planning to store the SDK ID Password in the session.
                    function_name = request.POST.get('function_name')
                    payload = request.POST.get('payload')
                    payload = json.loads(payload)
                    start = time.time()
                    data = sdk_client.logic_run_function(
                        function_name, payload)
                    end = time.time()
                    data['duration'] = end - start
                    return JsonResponse(data)
            elif cmd == 'delete_function_test':
                test_name = request.POST.get('test_name')
                logic_api.delete_function_test(test_name)
            elif cmd == 'delete_function':
                function_name = request.POST.get('function_name')
                logic_api.delete_function(function_name)
            elif cmd == 'create_webhook':
                name = request.POST.get('name')
                description = request.POST.get('description')
                function_name = request.POST.get('function_name')
                logic_api.create_webhook(name, description, function_name)
            elif cmd == 'delete_webhook':
                name = request.POST.get('name')
                logic_api.delete_webhook(name)

        return redirect(request.path_info)  # Redirect back
示例#2
0
    def post(self, request, app_id):
        context = Util.get_context(request)
        context['app_id'] = app_id
        cmd = request.POST.get('cmd', None)
        adapter = DjangoAdapter(app_id, request)
        with adapter.open_api_logic() as logic_api:
            if cmd == 'create_function':
                zip_file = request.FILES['zip_file']
                function_name = request.POST['function_name']
                description = request.POST['description']
                runtime = request.POST['runtime']
                handler = request.POST['handler']
                use_logging = request.POST['use_logging'] == 'true'
                use_traceback = request.POST['use_traceback'] == 'true'
                use_standalone = request.POST['use_standalone'] == 'true'
                sdk_config = get_sdk_config(adapter, use_localhost=False)

                zip_file.seek(0)
                zip_file_bin = zip_file.read()
                zip_file_bytes = zip_file_bin
                zip_file_bin = base64.b64encode(zip_file_bin)
                zip_file_bin = zip_file_bin.decode('utf-8')
                if not description:
                    description = None

                try:
                    requirements_text = get_requirements_text_from_zipfile(
                        zip_file_bytes)
                except Exception as ex:
                    requirements_text = None
                    print(ex)
                    print(traceback.format_exc())

                requirements_zip_file_id = None
                response_stdout = None
                if requirements_text:
                    print('requirements_text:', requirements_text)
                    with adapter.open_sdk() as sdk_client:
                        response = sdk_client.logic_create_packages_zip(
                            requirements_text)
                        print('logic_create_packages_zip:', response)
                        requirements_zip_file_id = response.get(
                            'zip_file_id', None)
                        response_stdout = response.get('response_stdout', None)
                        if not requirements_zip_file_id:
                            raise RuntimeError(
                                'Requirements.txt error! retry or check this.')
                # response = logic_api.create_packages_zip(requirements_text)
                # print('logic_create_packages_zip:', response)
                # requirements_zip_file_id = response.get('zip_file_id', None)

                response_function_creation = logic_api.create_function(
                    function_name, description, runtime, handler, sdk_config,
                    zip_file_bin, True, use_logging, use_traceback,
                    requirements_zip_file_id, use_standalone)
                return JsonResponse({
                    'package_install_response_stdout':
                    response_stdout,
                    'response_function_creation':
                    response_function_creation
                })
            elif cmd == 'create_function_test':
                test_name = request.POST.get('test_name')
                function_name = request.POST.get('function_name')
                test_input = request.POST.get('test_input')
                logic_api.create_function_test(test_name, function_name,
                                               test_input)
            elif cmd == 'run_function':
                with adapter.open_sdk() as sdk_client:
                    # TODO SDK Sign-up Login and authentication takes a lot of time.
                    #  We are planning to store the SDK ID Password in the session.
                    function_name = request.POST.get('function_name')
                    payload = request.POST.get('payload')
                    payload = json.loads(payload)
                    start = time.time()
                    data = sdk_client.logic_run_function(
                        function_name, payload)
                    end = time.time()
                    data['duration'] = end - start
                    # print('data:', data)
                    return JsonResponse(data)
            elif cmd == 'delete_function_test':
                test_name = request.POST.get('test_name')
                logic_api.delete_function_test(test_name)
            elif cmd == 'delete_function':
                function_name = request.POST.get('function_name')
                function_version = request.POST.get('function_version', None)
                logic_api.delete_function(function_name, function_version)
            elif cmd == 'create_webhook':
                name = request.POST.get('name')
                description = request.POST.get('description')
                function_name = request.POST.get('function_name')
                logic_api.create_webhook(name, description, function_name)
            elif cmd == 'delete_webhook':
                name = request.POST.get('name')
                logic_api.delete_webhook(name)

        return redirect(request.path_info)  # Redirect back