2022年8月10日 星期三

Using httpx in FastAPI to call Flask API

calculate.py

from flask import Flask, redirect, url_for, request
import datetime

app = Flask(__name__)

@app.route('/calculate',methods = ['POST', 'GET'])
def calculate():
   now = datetime.datetime.now()
   if request.method == 'POST':
      res = f"POST {now}"
   else:
      res = f"GET {now}"
   print(res)
   return res

if __name__ == '__main__':
   app.run(debug = True, host='0.0.0.0', port=5000)


client.py

import httpx

with httpx.Client() as client:
    r = client.post("http://127.0.0.1:5000/calculate")
    print(r.status_code)
    print(r.text)


api.py

# uvicorn main:app --reload
# pip install fastapi[all]
# pip install uvicorn

from fastapi import FastAPI
from enum import Enum
from typing import Union
import httpx

import webbrowser
webbrowser.open("http://127.0.0.1:8000")
webbrowser.open("http://127.0.0.1:8000/redoc")
webbrowser.open("http://127.0.0.1:8000/openapi.json")
webbrowser.open("http://127.0.0.1:8000/docs")

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello World"}
    
@app.get("/pretend")
async def pretend():
    async with httpx.AsyncClient() as client:
        r = await client.post("http://127.0.0.1:5000/calculate")
        print(r.status_code)
        print(r.text)
        return r.text
   

沒有留言:

張貼留言

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...