azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

Using Microsoft Table Storage Output Bindings

Open tonybaloney opened this issue 3 years ago • 1 comments

I am trying to connect the FastAPI based Functions app to Azure File Storage through the Output bindings.

{
      "name": "mntbl",
      "type": "table",
      "tableName": "maintable",
      "partitionKey": "district_id",
      "rowKey": "ri_id",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    },

mntbl is the name of the binding and as per the docs I need to add it to the main function.

As it stands before:

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return AsgiMiddleware(app).handle(req, context)

I can add it here by:


def main(req: func.HttpRequest, mntbl: func.Out[str], context: func.Context) -> func.HttpResponse:
    return AsgiMiddleware(app).handle(req, mntbl, context)

But I guess I'll need to make appropriate changes in the http_asgi.py file as well.

The relevant parts of that file are I suppose:

class AsgiMiddleware:
    def __init__(self, app):
        logging.debug("Instantiating ASGI middleware.")
        self._app = app
        self.loop = asyncio.new_event_loop()
        logging.debug("asyncio event loop initialized.")

    # Usage
    # main = func.AsgiMiddleware(app).main
    @property
    def main(self) -> Callable[[HttpRequest, Context], HttpResponse]:
        return self._handle

    # Usage
    # return func.AsgiMiddleware(app).handle(req, context)
    def handle(
        self, req: HttpRequest, context: Optional[Context] = None
    ) -> HttpResponse:
        logging.info(f"Handling {req.url} as ASGI request.")
        return self._handle(req, context)

    def _handle(self, req: HttpRequest,
                context: Optional[Context]) -> HttpResponse:
        asgi_request = AsgiRequest(req, context)
        asyncio.set_event_loop(self.loop)
        scope = asgi_request.to_asgi_http_scope()
        asgi_response = self.loop.run_until_complete(
            AsgiResponse.from_app(self._app, scope, req.get_body())
        )

        return asgi_response.to_func_response()

But I am unsure how to handle it here? Any inputs?

The alternate would be to use the Tables SDK directly I suppose, but it would be useful to be able to use the functions binding as well as they take care of auth etc.

tonybaloney avatar Aug 09 '22 04:08 tonybaloney

Originally asked by @harshnisar

tonybaloney avatar Aug 09 '22 04:08 tonybaloney