Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python HTTPTrigger Lesson #132

Merged
merged 38 commits into from Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
67b53e6
Add project template
dana-cmd Nov 15, 2021
ce0631f
WIP added project and lesson template
dana-cmd Nov 15, 2021
beabf5c
WIP added project and lesson template
dana-cmd Nov 15, 2021
6429a8c
WIP added funcs
dana-cmd Nov 15, 2021
8261695
WIP added 3
dana-cmd Nov 15, 2021
dcce085
Delete old project
dana-cmd Nov 16, 2021
1ef8995
Working post version
dana-cmd Nov 16, 2021
f888e15
Working dataclass version
dana-cmd Nov 16, 2021
67d15f5
Rename folders
dana-cmd Nov 16, 2021
b9cdd09
Added documentation and homework.
dana-cmd Nov 16, 2021
18dd549
Added documentation and homework.
dana-cmd Nov 16, 2021
87fc872
Homework solution
dana-cmd Nov 16, 2021
35b23b9
Homework solution
dana-cmd Nov 16, 2021
47a9130
Add image
dana-cmd Nov 16, 2021
62e1263
Fix small doc errors and fix workspace paths
dana-cmd Nov 17, 2021
62b9d6f
Fix img path
dana-cmd Nov 17, 2021
dbc081f
Add :
dana-cmd Nov 17, 2021
00ef9f0
Add Python to index table
dana-cmd Nov 17, 2021
5e33859
Fix link
dana-cmd Nov 17, 2021
6c6f547
Fix title
dana-cmd Nov 17, 2021
3d3e372
Add test file
dana-cmd Nov 17, 2021
e77df6d
Fix path
dana-cmd Nov 17, 2021
11636ba
Fix homework path
dana-cmd Nov 17, 2021
39276e0
Add test file
dana-cmd Nov 17, 2021
4ae80e7
Rename file and fix link
dana-cmd Nov 17, 2021
5eb3595
Rename file
dana-cmd Nov 17, 2021
415cbf9
Link lessons
dana-cmd Nov 17, 2021
3aae040
Change link name
dana-cmd Nov 17, 2021
02851b3
Fix prerequisits name and move .vscode folder
dana-cmd Nov 18, 2021
1d4479c
Add missing parentesis
dana-cmd Nov 18, 2021
db9c529
Add tours for Python lesson and homework. Adjust the output of the fu…
dana-cmd Nov 20, 2021
4465842
Fix broken links in typescript tour.
dana-cmd Nov 20, 2021
26513c6
Modify begin on line
dana-cmd Nov 20, 2021
b57c38f
Fix formatting
dana-cmd Nov 20, 2021
a2549e7
Fix typos.
dana-cmd Nov 21, 2021
dfdb854
Path in Python tour works in workspace project
dana-cmd Nov 21, 2021
6a8f1ea
Revert change in Typescript tour
dana-cmd Nov 21, 2021
3581314
Remove .Net references
dana-cmd Nov 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
201 changes: 201 additions & 0 deletions .tours/python/http/.tours/01creating-a-function-app.tour
@@ -0,0 +1,201 @@
{
"$schema": "https://aka.ms/codetour-schema",
"title": "Python HTTP - 01. Creating a Function App",
"steps": [
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 1,
"character": 1
},
"end": {
"line": 17,
"character": 28
}
},
"description": "The `__init__.py` file is a Python file containing the code of your function exported as an Azure Function.",
"title": "Walk through Azure Function file structure - __init__.py (1)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 6,
"character": 1
},
"end": {
"line": 6,
"character": 86
}
},
"description": "As we use the HTTP triggered Azure Function template, we receive the HTTP request as input to our function.",
"title": "Walk through Azure Function file structure - __init__.py (2)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 9,
"character": 1
},
"end": {
"line": 16,
"character": 66
}
},
"description": "These lines of code shows us how to extract information from the request object. We can fetch data from the query parameters (`req.params`) or the body of the request (`req_body`).",
"title": "Walk through Azure Function file structure - __init__.py (3)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 18,
"character": 1
},
"end": {
"line": 23,
"character": 149
}
},
"description": "Next we define our response text depending on the existence of the `name` property handed over to the function via a query parameter or in the body of the HTTP request.",
"title": "Walk through Azure Function file structure - __init__.py (4)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 22,
"character": 1
},
"end": {
"line": 23,
"character": 50
}
},
"description": "We return a result message and a response status. The response status is defaulted with 200. ",
"title": "Walk through Azure Function file structure - __init__.py (5 )"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/function.json",
"selection": {
"start": {
"line": 1,
"character": 1
},
"end": {
"line": 20,
"character": 2
}
},
"description": "The `function.json` file contains the [function specific configuration](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference?tabs=blob).",
"title": "Walk through Azure Function file structure - function.json"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/function.json",
"selection": {
"start": {
"line": 5,
"character": 7
},
"end": {
"line": 5,
"character": 31
}
},
"description": "The attribute `authLevel`contains the authorization level for our function.",
"title": "Walk through Azure Function file structure - function.json (authorization level)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/function.json",
"selection": {
"start": {
"line": 6,
"character": 7
},
"end": {
"line": 8,
"character": 21
}
},
"description": "In addition we define the input bindings i.e. the information that is passed into our function. We define the name that we can use to access the binding in our function code. Here we use the name `req`. \r\nThe trigger binding is a special type of input binding for a function as it is the one that represents the trigger for the function execution. You can only have one trigger binding per function but multiple other input bindings. ",
"title": "Walk through Azure Function file structure - function.json (trigger and input binding)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/function.json",
"selection": {
"start": {
"line": 9,
"character": 6
},
"end": {
"line": 12,
"character": 8
}
},
"description": "The attribute `methods` contains an array of the HTTP verbs that the user can use to invoke your function via an HTTP request.",
"title": "Walk through Azure Function file structure - function.json (HTTP methods)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/HelloWorldHttpTrigger/function.json",
"selection": {
"start": {
"line": 14,
"character": 5
},
"end": {
"line": 18,
"character": 6
}
},
"description": "As counterpart of the input binding we also define an output binding for our function. This binding represents the data that is returned from our function after execution. In our scenario we have the type of `http` as output binding which means that we return an HTTP response.",
"title": "Walk through Azure Function file structure - function.json (output binding)"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/host.json",
"selection": {
"start": {
"line": 1,
"character": 1
},
"end": {
"line": 16,
"character": 1
}
},
"description": "The `host.json` file [global configuration options](https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json) for all functions in a function app.",
"title": "Walk through Azure Function file structure - host.json"
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/local.settings.json",
"selection": {
"start": {
"line": 1,
"character": 1
},
"end": {
"line": 8,
"character": 1
}
},
"description": "The `local.settings.json` file contains the [app settings and connection strings](https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json) for local development. Make sure that the file is excluded in your `.gitignore` file. For the sake of this lesson we have excluded this file from `.gitignore`.",
"title": "Walk through Azure Function file structure - local.settings.json "
},
{
"file": "../../../src/python/AzureFunctions.Http/01CreatingAFunctionApp/requirements.txt",
"selection": {
"start": {
"line": 1,
"character": 1
},
"end": {
"line": 6,
"character": 71
}
},
"description": "Before we can execute the function we first must install the dependencies via `pip install -r requirements.txt`",
"title": "Install Python dependencies"
}
]
}
@@ -0,0 +1,46 @@
{
"$schema": "https://aka.ms/codetour-schema",
"title": "Python HTTP - 02. Changing the template for GET requests",
"steps": [
{
"file": "../../../src/python/AzureFunctions.Http/02ChangingTheTemplateForGetRequest/HelloWorldHttpTrigger/function.json",
"description": "Remove the `post` entry from the `methods`array in the `function.json` file. Now the function can only be triggered by a GET request.",
"line": 10,
"title": "Restrict the function endpoint to HTTP GET"
},
{
"file": "../../../src/python/AzureFunctions.Http/02ChangingTheTemplateForGetRequest/HelloWorldHttpTrigger/__init__.py",
"description": "We leave the `req` parameter associated with the `GET` request unchanged. However, we will removed the `body` parameter associated with the `POST` request.",
"line": 9,
"title": "Fetch query parameter from HTTP request"
},
{
"file": "../../../src/python/AzureFunctions.Http/02ChangingTheTemplateForGetRequest/HelloWorldHttpTrigger/__init__.py",
"description": "We define a variable for the response code.",
"line": 14,
"title": "Define variable for the HTTP response code"
},
{
"file": "../../../src/python/AzureFunctions.Http/02ChangingTheTemplateForGetRequest/HelloWorldHttpTrigger/__init__.py",
"description": "Add an `if` statement to the function that checks if the name value is `null`, an empty string or `undefined`. If this is this case we return an HTTP code 400 as response, otherwise we return an HTTP code 200. Now the function has proper return values for both correct and incorrect invocations.",
"line": 11,
"selection": {
"start": {
"line": 12,
"character": 1
},
"end": {
"line": 13,
"character": 1
}
},
"title": "Implement the logic for the response"
},
{
"file": "../../../src/python/AzureFunctions.Http/02ChangingTheTemplateForGetRequest/HelloWorldHttpTrigger/__init__.py",
"description": "Run the function via `func host start`. Invoke it once without name value in the query string: [http://localhost:7071/api/HelloWorldHttpTrigger](http://localhost:7071/api/HelloWorldHttpTrigger) and once with a name value: [http://localhost:7071/api/HelloWorldHttpTrigger?name=AzureFunctionsUniversity](http://localhost:7071/api/HelloWorldHttpTrigger?name=AzureFunctionsUniversity)",
"line": 21,
"title": "Run and call the function"
}
]
}
@@ -0,0 +1,66 @@
{
"$schema": "https://aka.ms/codetour-schema",
"title": "Python HTTP - 03. Changing the template for POST requests",
"steps": [
{
"file": "../../../src/python/AzureFunctions.Http/03ChangingTheTemplateForPostRequests/HelloWorldHttpTrigger/function.json",
"selection": {
"start": {
"line": 8,
"character": 1
},
"end": {
"line": 12,
"character": 8
}
},
"description": "Let's change the function to also allow POST requests and test it by posting a request with JSON content in the body. Add the `post` entry in the `methods`array in the `function.json` file.",
"title": "Change function.json to enable HTTP POST requests"
},
{
"file": "../../../src/python/AzureFunctions.Http/03ChangingTheTemplateForPostRequests/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 10,
"character": 5
},
"end": {
"line": 10,
"character": 32
}
},
"description": "The function in the `__init__.py` file currently only handles GET requests. We need to add some logic to use the query string for GET and the request body for POST requests. This can be done by checking the method property of the request parameter.",
"title": "Implement distinction between GET and POST requests"
},
{
"file": "../../../src/python/AzureFunctions.Http/03ChangingTheTemplateForPostRequests/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 27,
"character": 9
},
"end": {
"line": 28,
"character": 158
}
},
"description": "We return a message in case of an invalid request to handle the two different error situations for GET and POST.",
"title": "Adopt error message"
},
{
"file": "../../../src/python/AzureFunctions.Http/03ChangingTheTemplateForPostRequests/HelloWorldHttpTrigger/__init__.py",
"selection": {
"start": {
"line": 30,
"character": 1
},
"end": {
"line": 30,
"character": 27
}
},
"description": "Now we can run the function via `func host start`. To trigger the logic we send a POST request to the endpoint. If you're using the [VSCode REST client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) you can use this in a .http file:\r\n\r\n```http\r\nPOST http://localhost:7071/api/HelloWorldHttpTrigger\r\nContent-Type: application/json\r\n\r\n{\r\n \"name\": \"Your name\"\r\n}\r\n```",
"title": "Run and call the function"
}
]
}