Exemple #1
0
class Versions(web.View):

    versions = {}

    def __init__(self, *args, wsk_args=None, **kwargs):
        super(Versions, self).__init__(*args, **kwargs)

    @aiohttp_apispec.docs(
        tags=["versions"],
        summary="Get available API versions",
    )
    @aiohttp_apispec.response_schema(responses.Versions(), 200)
    @aiohttp_apispec.response_schema(responses.Failure(), 400)
    async def get(self):
        versions = []
        for ver, info in self.versions.items():
            resp = await info(self.request)
            versions.append(json.loads(resp.body))

        return web.json_response({"versions": versions})
class Versions(web.View):

    versions = {}

    def __init__(self, *args, wsk_args=None, **kwargs):
        super(Versions, self).__init__(*args, **kwargs)

    @aiohttp_apispec.docs(
        tags=["versions"],
        summary="Get available API versions",
    )
    @aiohttp_apispec.response_schema(responses.Versions(), 200)
    @aiohttp_apispec.response_schema(responses.Failure(), 400)
    async def get(self):
        versions = []
        for ver, info in self.versions.items():
            resp = await info(self.request)
            versions.append(json.loads(resp.body))

        response = {"versions": versions, "links": []}
        # But here we use the global app coming in the request
        doc = self.request.app.router.named_resources().get("swagger.docs")
        if doc:
            doc = {
                "rel": "help",
                "type": "text/html",
                "href": "%s" % doc.url_for()
            }
            response["links"].append(doc)

        spec = self.request.app.router.named_resources().get("swagger.spec")
        if spec:
            spec = {
                "rel": "describedby",
                "type": "application/json",
                "href": "%s" % spec.url_for(),
            }
            response["links"].append(spec)

        return web.json_response(response)
Exemple #3
0
    class ModelPredict(web.View):
        model_name = model_name
        model_obj = model_obj

        @aiohttp_apispec.docs(tags=["v1", "v1.models"],
                              summary="Make a prediction given the input data",
                              responses={
                                  501: {
                                      "description":
                                      "Functionality not implemented"
                                  },
                              })
        @aiohttp_apispec.querystring_schema(args)
        @aiohttp_apispec.response_schema(ModelResponse(), 200)
        @aiohttp_apispec.response_schema(responses.Failure(), 400)
        @aiohttpparser.parser.use_args(args)
        async def post(self, args):
            """Make a prediction given the input data."""

            urls = args.get("url")
            files = args.get("data")
            if (not any([urls, files]) or all([urls, files])):
                raise web.HTTPBadRequest(
                    reason="You must provide either 'url' or "
                    "'data' in the payload")

            args["urls"] = urls

            if files:
                # FIXME(aloga): only handling one file, see comment on top of
                # file and [1] for more details
                # [1] https://github.com/noirbizarre/flask-restplus/issues/491
                tmp = werkzeug.datastructures.FileStorage(
                    stream=files.file, filename=files.filename)
                args["files"] = [tmp]

                ret = self.model_obj.predict_data(args)
            elif urls:
                ret = self.model_obj.predict_url(args)
            return web.json_response(ret)
Exemple #4
0
    class Handler(object):
        model_name = None
        model_obj = None

        def __init__(self, model_name, model_obj):
            self.model_name = model_name
            self.model_obj = model_obj

        @aiohttp_apispec.docs(
            tags=["models"],
            summary="Make a prediction given the input data",
            produces=accept.validate.choices if accept else None,
        )
        @aiohttp_apispec.querystring_schema(handler_args)
        @aiohttp_apispec.response_schema(response(), 200)
        @aiohttp_apispec.response_schema(responses.Failure(), 400)
        async def post(self, request, wsk_args=None):
            args = await aiohttpparser.parser.parse(handler_args, request)
            if wsk_args:
                args.update(wsk_args)
            task = self.model_obj.predict(**args)
            await task

            ret = task.result()['output']

            if isinstance(ret, model.v2.wrapper.ReturnedFile):
                ret = open(ret.filename, 'rb')

            accept = args.get("accept", "application/json")
            if accept != "application/json":
                response = web.Response(
                    body=ret,
                    content_type=accept,
                )
                return response
            if self.model_obj.has_schema:
                self.model_obj.validate_response(ret)
                return web.json_response(ret)

            return web.json_response({"status": "OK", "predictions": ret})
Exemple #5
0
def get_app():
    global APP

    APP = web.Application()
    APP.router.add_get('/', get_version, name="v1")
    APP.add_routes(routes)

    return APP


@aiohttp_apispec.docs(
    tags=["v1", "versions"],
    summary="Get V1 API version information",
)
@aiohttp_apispec.response_schema(responses.Version(), 200)
@aiohttp_apispec.response_schema(responses.Failure(), 400)
async def get_version(request):
    # NOTE(aloga): we use the router table from this application (i.e. the
    # global APP in this module) to be able to build the correct url, as it can
    # be prefixed outside of this module (in an add_subapp() call)
    root = APP.router["v1"].url_for()
    version = {
        "version":
        "deprecated",
        "id":
        "v1",
        "links": [{
            "rel": "self",
            "type": "application/json",
            "href": "%s" % root,
        }]