Saturday, May 21, 2022

Azure Container Apps

Summary

This is a continuation of a previous post on proxying a SOAP API to REST.  In this post, I'll deploy the containerized proxy to Azure Container Apps and front end it with Azure API Management (APIM).

Architecture



Code

Proxy App

I modified the Python FastAPI app slightly to serve up an OpenAPI file.  That file is used by APIM during provisioning.

  1. from fastapi import FastAPI, HTTPException
  2. from fastapi.responses import FileResponse
  3. from zeep import Client
  4. import logging
  5.  
  6. logging.getLogger('zeep').setLevel(logging.ERROR)
  7. client = Client('https://www.w3schools.com/xml/tempconvert.asmx?wsdl')
  8. app = FastAPI()
  9.  
  10. @app.get("/openapi.yml")
  11. async def openapi():
  12. return FileResponse("openapi.yml")
  13.  
  14. @app.get("/CelsiusToFahrenheit")
  15. async def celsiusToFahrenheit(temp: int):
  16. try:
  17. soapResponse = client.service.CelsiusToFahrenheit(temp)
  18. fahrenheit = int(round(float(soapResponse),0))
  19. except:
  20. raise HTTPException(status_code=400, detail="SOAP request error")
  21. else:
  22. return {"temp": fahrenheit}
  23.  
  24.  
  25. @app.get("/FahrenheitToCelsius")
  26. async def fahrenheitToCelsius(temp: int):
  27. try:
  28. soapResponse = client.service.FahrenheitToCelsius(temp)
  29. celsius = int(round(float(soapResponse),0))
  30. except:
  31. raise HTTPException(status_code=400, detail="SOAP request error")
  32. else:
  33. return {"temp": celsius}

OpenAPI Spec


  1. swagger: '2.0'
  2. info:
  3. title: apiproxy
  4. description: REST to SOAP proxy
  5. version: 1.0.0
  6. schemes:
  7. - http
  8. produces:
  9. - application/json
  10. paths:
  11. /CelsiusToFahrenheit:
  12. get:
  13. summary: Convert celsius temp to fahrenheit
  14. parameters:
  15. - name: temp
  16. in: path
  17. required: true
  18. type: integer
  19. responses:
  20. '200':
  21. description: converted temp
  22. schema:
  23. type: object
  24. properties:
  25. temp:
  26. type: integer
  27. '400':
  28. description: General error
  29. /FahrenheitToCelsius:
  30. get:
  31. summary: Convert fahrenheit temp to celsius
  32. parameters:
  33. - name: temp
  34. in: path
  35. required: true
  36. type: integer
  37. responses:
  38. '200':
  39. description: converted temp
  40. schema:
  41. type: object
  42. properties:
  43. temp:
  44. type: integer
  45. '400':
  46. description: General error

Deployment


Create + Configure Azure Container Registry





Visual Studio Code - Build Image in Azure







Create + Configure Azure Container App






Execution

Deploy and Test Container App in APIM



Copyright ©1993-2024 Joey E Whelan, All rights reserved.