コード例 #1
0
 def do_POST(self):
     if self.path == "/functions":
         data = json.loads(
             self.rfile.read(int(self.headers["Content-Length"])).decode())
         self._send_json(execute_function(Functions, data))
     else:
         self.send_error(404)
コード例 #2
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    # Defer the import until triggered so that failure is attached to the response
    try:
        import Functions
    except ModuleNotFoundError:
        # Try the Azure Functions name if the "natural" name was missing
        import __app__.Functions as Functions

    invokeFunction = False
    if req.method == "POST":
        try:
            invokeFunction = True
            payload = req.get_json()
        except Exception as ex:
            return func.HttpResponse(f"Invalid request: {ex}", status_code=400)

    _, _, param = req.url.partition("?")
    if req.method == "GET" and param.startswith("invoke="):
        invokeFunction = True
        payload = param.partition("=")[2]
        payload = json.loads(unquote(payload))

    if invokeFunction:
        try:
            return func.HttpResponse(
                execute_function(Functions, payload),
                mimetype="application/json",
            )
        except Exception as ex:
            return func.HttpResponse(f"Invalid request: {ex}", status_code=422)

    return func.HttpResponse(get_all_metadata(Functions),
                             mimetype="application/json")
コード例 #3
0
ファイル: main.py プロジェクト: shaofengzhu/FunctionsTemplate
def main(req: func.HttpRequest) -> func.HttpResponse:
    # Defer the import until triggered so that failure is attached to the response
    try:
        import Functions
    except ModuleNotFoundError:
        # Try the Azure Functions name if the "natural" name was missing
        import __app__.Functions as Functions

    if req.method == "POST":
        try:
            payload = req.get_json()
        except Exception as ex:
            return func.HttpResponse(f"Invalid request: {ex}", status_code=400)
        try:
            return func.HttpResponse(
                execute_function(Functions, payload),
                mimetype="application/json",
            )
        except Exception as ex:
            return func.HttpResponse(f"Invalid request: {ex}", status_code=422)

    return func.HttpResponse(get_all_metadata(Functions),
                             mimetype="application/json")
コード例 #4
0
 def do_GET(self):
     path, _, param = self.path.partition("?")
     if path == "/functions":
         if param.startswith("invoke="):
             payload = param.partition("=")[2]
             data = json.loads(unquote(payload))
             self._send_json(execute_function(Functions, data))
         else:
             self._send_json(get_all_metadata(Functions))
     elif path == "/functions.html":
         self.send_response(200)
         self.send_header("Content-Type", "text/html;charset=utf-8")
         devMode = os.getenv("EXCEL_DEVMODE", "").lower() in {"1", "yes", "true"}
         pageHtml = _getPageHtml(devMode=devMode).encode()
         self.send_header("Content-Length", str(len(pageHtml)))
         self.end_headers()
         self.wfile.write(pageHtml)
     elif path == "/":
         self.send_response(302)
         self.send_header("Location", "/functions.html")
         self.end_headers()
     else:
         self.send_error(404)