Accessing XML REST API using FastAPI by creating swagger UI
To create a swagger UI for XML Rest API using FastAPI,
we have taken public xml based public REST Api : http://www.dneonline.com/calculator.asmx
install the below pip modules
requests
fastapi
uvicorn
Post installation of create a sample program with filename: main.py like below to access the api
main.py
import requests
import logging
from typing import Optional
from typing import Union
from fastapi import FastAPI, Response
#from enum import Enum
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
logging.basicConfig(filename="std.log", format='%(asctime)s %(levelname)s %(message)s', filemode='w')
logger=logging.getLogger()
logger.setLevel(logging.DEBUG)
def getdata_dneonline(newurl, newfname,x,y):
try:
#url = "http://www.dneonline.com/calculator.asmx"
#f = open("addHelper.xml", "rt")
f = open(newfname, "rt")
payload = f.read()
f.close()
headers = {
'Content-Type': 'text/xml'
}
response = requests.request("POST", newurl, headers=headers, data=payload.format(a=x,b=y))
logger.debug(response.text)
return response.text
except Exception as e:
print("Err: ", e)
#untangle.parse(getdata_dneonline("http://www.dneonline.com/calculator.asmx", "addHelper.xml"))
app = FastAPI()
@app.get('/addition')
async def calculator_add(inputA: int, inputB: int):
#return getdata_dneonline("http://www.dneonline.com/calculator.asmx", "addHelper.xml")
return Response(content=getdata_dneonline("http://www.dneonline.com/calculator.asmx", "addHelpers.xml",inputA,inputB), media_type="application/xml")
@app.get('/subtraction')
async def calculator_sub(inputA: int, inputB: int):
#return getdata_dneonline("http://www.dneonline.com/calculator.asmx", "addHelper.xml")
return Response(content=getdata_dneonline("http://www.dneonline.com/calculator.asmx", "subHelpers.xml",inputA,inputB), media_type="application/xml")
@app.get('/Multiply')
async def calculator_multiply(inputA: int, inputB: int):
#return getdata_dneonline("http://www.dneonline.com/calculator.asmx", "addHelper.xml")
return Response(content=getdata_dneonline("http://www.dneonline.com/calculator.asmx", "multipleHelpers.xml",inputA,inputB), media_type="application/xml")
@app.get('/Division')
async def calculator_divide(inputA: int, inputB: int):
#return getdata_dneonline("http://www.dneonline.com/calculator.asmx", "addHelper.xml")
return Response(content=getdata_dneonline("http://www.dneonline.com/calculator.asmx", "divideHelpers.xml",inputA,inputB), media_type="application/xml")
To start program execute below command
uvicorn main:app --reload
post starting service, access the url https://127.0.0.1:8000/docs, it will open the swagger ui like bloew

you can playing with public rest api like below

You can get the sample code from my github: https://github.com/hemanth22/python-restapi/tree/feature/logger
If you like the blog, please like and comment and share.