The goal of this week's assignment is to develop and document APIs using Flask, and manage them with Azure API Management.
- I first made a
app_basic.py
file along with anapp_flasgger.py
file. In each, I made a home page and a hello page. - In the Home page, I only included a "Hello" response.
- In the Hello page, I included:
- Name (upper case)
- Last name (upper case)
- Birthday
- When you run the flask app, it will show up like this:
![image](https://private-user-images.githubusercontent.com/123939070/281202536-ab09fd3f-aa38-4b66-a444-edb266440412.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQyNTY3NTAsIm5iZiI6MTcyNDI1NjQ1MCwicGF0aCI6Ii8xMjM5MzkwNzAvMjgxMjAyNTM2LWFiMDlmZDNmLWFhMzgtNGI2Ni1hNDQ0LWVkYjI2NjQ0MDQxMi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODIxJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgyMVQxNjA3MzBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mYTc3MzkxZGNmMDQxNmIxZmNmNmUxNmNlY2Y4MDk4Yjg2MTRmZmYyY2M2YWFmMzEzOTUwYTQ4NTQ1NWYxZjkzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.YpNMYg4isir2AVBWSuIuqbBE_yKzpIDYj4-_3TBHq-8)
I placed this in the URL: hello?name=helen&lastname=zheng&birthday=October%20
- In the Google Shell terminal, you will need to install AZURE CLI with this:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
. Copy and paste the line in and enter. - Type in
az login --use-device-code
and wait for a link and a code to appear in the terminal. Copy the code and press the link to log in to your Azure account. This will help connect your Google Shell with your Azure account. - Install
sudo apt-get install azure-functions-core-tools-4
in the terminal for azure functions core tools. - Now input
func init
. - In your
local.settings.json
file, change the settings to this:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}
- Install azure.functions with
pip install azure.functions
- In your
function_app.py
, change the code to your code:
import azure.functions as func
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.function_name(name="HttpExample")
@app.route(route="hello")
def hello_get(req: func.HttpRequest) -> func.HttpResponse:
return func.HttpResponse("HttpExample function processed a request!")
-
Now to deploy the app, you will need to create a resource group, storage account, and a function app.
- For Resource group:
az group create --name <put the resource group name here that you want to create>-rg --location <insert location; if you are US east put eastus>
- For Resource group:
-
For Storage Account:
az storage account create --name <name of your storage account> --location <insert location; like the resource group> --resource-group <insert your resource group here> --sku Standard_LRS
-
For Function App:
az functionapp create --resource-group <insert your resource group here>-rg --consumption-plan-location <insert your location> --runtime python --runtime-version 3.9 --functions-version 4 --name <create a name of your function app> --os-type linux --storage-account <put your storage account name>
-
Now you can deploy your app:
func azure functionapp publish <your function app name>
-
Once the link appears, you have successfully deployed it! 😄
Link: https://helenapi.azurewebsites.net/api/hello
![image](https://private-user-images.githubusercontent.com/123939070/281198909-8cfa7e0b-5e74-4c60-8bc9-a599ee5ac535.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQyNTY3NTAsIm5iZiI6MTcyNDI1NjQ1MCwicGF0aCI6Ii8xMjM5MzkwNzAvMjgxMTk4OTA5LThjZmE3ZTBiLTVlNzQtNGM2MC04YmM5LWE1OTllZTVhYzUzNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODIxJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgyMVQxNjA3MzBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01ZWMwNzg3YWIyYjJlODUwZWIyZWJlMWZhZjA3ZGJiZTVkODIzMjA3MTYwNGQ3MzhjMWNkZWFhYTg4ZDY0MGMxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.1ecFTsOKdS9Ymu7STl-ZTg0pD8l1uPzhTvE9KFiy21U)
This is because I put this in the function_app,py
file:
![image](https://private-user-images.githubusercontent.com/123939070/281199468-06a29291-4753-490a-8db3-86114da4d45b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQyNTY3NTAsIm5iZiI6MTcyNDI1NjQ1MCwicGF0aCI6Ii8xMjM5MzkwNzAvMjgxMTk5NDY4LTA2YTI5MjkxLTQ3NTMtNDkwYS04ZGIzLTg2MTE0ZGE0ZDQ1Yi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODIxJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgyMVQxNjA3MzBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1iZDkyM2FlNDk1NmU4ZDJiMWI4NzJiYjY0MmE2YjFlZTE4MjE5NjY1YmRkMjM5NGExNjkyZmZmNGIyODViYTllJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.MEAn031onr-yLmqLGSI-e2xHKEpdYLxsUwTDyocKZL0)
For Flasgger, check out this app_flasgger.py
file
When you need to open Swagger, use your flask app URL, add in apidocs
at the end of your application:
Now press on the GET and you will see this:
One complication I ran into was in the function_app.py
file where the app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
was originally just app = func.FunctionApp()
. The Azure deployment did not work without the ANONYMOUS portion in the function app. This is due to credentials purposes. Without the ANONYMOUS portion in the code, my azure endpoint URL came out to be: