Last active
March 23, 2022 12:37
-
-
Save sttt/5d6a64c2cd0340f0f57f3e98c6ccfc0e to your computer and use it in GitHub Desktop.
An example of transferring data for calculation to FastAPI, and returning the result.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
pip3 install uvicorn pydantic fastapi | |
[the path to the current fastapi project]>uvicorn main:app --host=127.0.0.1 --port=8888 --reload --workers 4 | |
get swagger: | |
http://127.0.0.1:8888/docs | |
http://127.0.0.1:8888/redoc | |
get API: | |
http://127.0.0.1:8888/ | |
:::-> test_calc.vbs ::: | |
Set xmlhttp = CreateObject("msxml2.xmlhttp") | |
myurl = "http://localhost:8888/calc" | |
str_json = "{""name"": ""python"", ""description"": ""tool for developing applications"", ""price"": 46.60, ""tax"": 230.00}" | |
xmlhttp.Open "POST", myurl, False | |
xmlhttp.setRequestHeader "Content-Type", "application/json" | |
xmlhttp.Send str_json | |
MsgBox (xmlhttp.responseText) | |
""" | |
from fastapi import FastAPI | |
from fastapi.responses import PlainTextResponse | |
from pydantic import BaseModel | |
app = FastAPI() | |
class Transaction(BaseModel): | |
name: str | |
description: str | |
price: float | |
tax: float | |
@app.get("/", response_class = PlainTextResponse) | |
async def hello(): | |
return "Hello World!" | |
@app.post("/calc/") | |
async def calc_excel(item: Transaction): | |
item_dict = item.dict() | |
if item.tax: | |
price_with_tax = item.price + item.tax | |
item_dict.update({"price_with_tax": price_with_tax}) | |
return item_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set xmlhttp = CreateObject("msxml2.xmlhttp") | |
myurl = "http://localhost:8888/calc" | |
str_json = "{""name"": ""python"", ""description"": ""tool for developing applications"", ""price"": 46.60, ""tax"": 230.00}" | |
xmlhttp.Open "POST", myurl, False | |
xmlhttp.setRequestHeader "Content-Type", "application/json" | |
xmlhttp.Send str_json | |
MsgBox (xmlhttp.responseText) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment