from flask import Module, jsonify
from flask.views import MethodView

from core.api.decorators import jsonp

api = Module(
    __name__,
    url_prefix='/api'
)


def jsonify_status_code(*args, **kw):
    response = jsonify(*args, **kw)
    response.status_code = kw['code']
    return response


@api.route('/')
def index():
    """
    The root of the API returns an error
    """
    return jsonify_status_code(
        code=400,
        message='Room no 404: File not found'
    )


class TestModelAPI(MethodView):
    @jsonp
    def get(self, id=None):
        if id:
            return jsonify(
                code=200,
                value=0
            )
        else:
            return jsonify(
                code=200,
                value=1
            )


TestModel_view = TestModelAPI.as_view('test_model_api')
api.add_url_rule(
    '/test',
    view_func=TestModel_view,
    methods=['GET']
)

api.add_url_rule(
    '/test/<string:id>',
    view_func=TestModel_view,
    methods=['GET']
)

from flask import Module, jsonify, request
from flask.views import MethodView

from core.api.decorators import jsonp

api = Module(
    __name__,
    url_prefix='/api'
)


def jsonify_status_code(*args, **kw):
    response = jsonify(*args, **kw)
    response.status_code = kw['code']
    return response


@api.route('/')
def index():
    """
    The root of the API returns an error
    """
    return jsonify_status_code(
        code=400,
        message='Room no 404: File not found'
    )


class CreateUnikernel(MethodView):
    @jsonp
    def get(self):
        return jsonify_status_code(
            code=405,
            message='HTTP method GET is not allowed for this URL'
        )

    @jsonp
    def post(self):
        content = request.get_json(force=False, silent=True)
        if not content:
            return jsonify_status_code(
                code=400,
                message='Bad HTTP POST request'
            )
        else:
            # Validate JSON
            pass


CreateUnikernel_view = CreateUnikernel.as_view('create_unikernel')
api.add_url_rule(
    '/unikernel/create',
    view_func=CreateUnikernel_view,
    methods=['GET', 'POST']
)

Add new Flask MethodView called CreateUnikernel
Add new Flask MethodView called CreateUnikernel

- from flask import Module, jsonify
+ from flask import Module, jsonify, request
  from flask.views import MethodView
  
  from core.api.decorators import jsonp
  
  api = Module(
      __name__,
      url_prefix='/api'
  )
  
  
  def jsonify_status_code(*args, **kw):
      response = jsonify(*args, **kw)
      response.status_code = kw['code']
      return response
  
  
  @api.route('/')
  def index():
      """
      The root of the API returns an error
      """
      return jsonify_status_code(
          code=400,
          message='Room no 404: File not found'
      )
  
  
- class TestModelAPI(MethodView):
+ class CreateUnikernel(MethodView):
      @jsonp
-     def get(self, id=None):
+     def get(self):
-         if id:
+         return jsonify_status_code(
+             code=405,
+             message='HTTP method GET is not allowed for this URL'
+         )
+ 
+     @jsonp
+     def post(self):
+         content = request.get_json(force=False, silent=True)
+         if not content:
-             return jsonify(
+             return jsonify_status_code(
-                 code=200,
+                 code=400,
-                 value=0
+                 message='Bad HTTP POST request'
              )
          else:
+             # Validate JSON
-             return jsonify(
-                 code=200,
-                 value=1
-             )
+             pass
  
  
- TestModel_view = TestModelAPI.as_view('test_model_api')
+ CreateUnikernel_view = CreateUnikernel.as_view('create_unikernel')
  api.add_url_rule(
-     '/test',
+     '/unikernel/create',
-     view_func=TestModel_view,
+     view_func=CreateUnikernel_view,
-     methods=['GET']
+     methods=['GET', 'POST']
  )
  
- api.add_url_rule(
-     '/test/<string:id>',
-     view_func=TestModel_view,
-     methods=['GET']
- )
- 
Add new Flask MethodView called CreateUnikernel
## Code Before:
from flask import Module, jsonify
from flask.views import MethodView

from core.api.decorators import jsonp

api = Module(
    __name__,
    url_prefix='/api'
)


def jsonify_status_code(*args, **kw):
    response = jsonify(*args, **kw)
    response.status_code = kw['code']
    return response


@api.route('/')
def index():
    """
    The root of the API returns an error
    """
    return jsonify_status_code(
        code=400,
        message='Room no 404: File not found'
    )


class TestModelAPI(MethodView):
    @jsonp
    def get(self, id=None):
        if id:
            return jsonify(
                code=200,
                value=0
            )
        else:
            return jsonify(
                code=200,
                value=1
            )


TestModel_view = TestModelAPI.as_view('test_model_api')
api.add_url_rule(
    '/test',
    view_func=TestModel_view,
    methods=['GET']
)

api.add_url_rule(
    '/test/<string:id>',
    view_func=TestModel_view,
    methods=['GET']
)

## Instruction:
Add new Flask MethodView called CreateUnikernel
## Code After:
from flask import Module, jsonify, request
from flask.views import MethodView

from core.api.decorators import jsonp

api = Module(
    __name__,
    url_prefix='/api'
)


def jsonify_status_code(*args, **kw):
    response = jsonify(*args, **kw)
    response.status_code = kw['code']
    return response


@api.route('/')
def index():
    """
    The root of the API returns an error
    """
    return jsonify_status_code(
        code=400,
        message='Room no 404: File not found'
    )


class CreateUnikernel(MethodView):
    @jsonp
    def get(self):
        return jsonify_status_code(
            code=405,
            message='HTTP method GET is not allowed for this URL'
        )

    @jsonp
    def post(self):
        content = request.get_json(force=False, silent=True)
        if not content:
            return jsonify_status_code(
                code=400,
                message='Bad HTTP POST request'
            )
        else:
            # Validate JSON
            pass


CreateUnikernel_view = CreateUnikernel.as_view('create_unikernel')
api.add_url_rule(
    '/unikernel/create',
    view_func=CreateUnikernel_view,
    methods=['GET', 'POST']
)
