Skip to content

Commit 13b256f

Browse files
committed
Add SDK samples in dev ref, move HTTP streams to HTTP trigger
1 parent d10d4ad commit 13b256f

File tree

2 files changed

+89
-89
lines changed

2 files changed

+89
-89
lines changed

articles/azure-functions/functions-bindings-http-webhook-trigger.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
414414
::: zone pivot="programming-language-python"
415415
# [v2](#tab/python-v2)
416416

417-
This example uses [HTTP streams](functions-reference-python.md#http-streams) to return chunked response data.
417+
This example is an HTTP triggered function that uses [HTTP streams](functions-reference-python.md#http-streams) to return chunked response data. You might use these capabilities to support scenarios like sending event data through a pipeline for real time visualization or detecting anomalies in large sets of data and providing instant notifications.
418418

419419
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/function_app.py" range="5-26" :::
420420

421-
To learn more, including how to enable HTTP streams in your project, see [HTTP streams](functions-reference-python.md#http-streams).
421+
To learn more, including how to enable HTTP streams in your project, see [HTTP streams](functions-bindings-http-webhook-trigger.md?tabs=python-v2&pivots=programming-language-python#http-streams).
422422

423423
This example shows a trigger binding and a Python function that uses the binding. The function looks for a `name` parameter either in the query string or the body of the HTTP request.
424424

@@ -1011,10 +1011,86 @@ You can now stream requests to and responses from your HTTP endpoint in Node.js
10111011
::: zone pivot="programming-language-python"
10121012
### HTTP streams
10131013

1014-
HTTP streams support in Python lets you accept and return data from your HTTP endpoints using FastAPI request and response APIs enabled in your functions. These APIs enable the host to process data in HTTP messages as chunks instead of having to read an entire message into memory. For more information, see [HTTP streams in Python](./functions-reference-python.md#http-streams)
1014+
HTTP streams support in Python lets you accept and return data from your HTTP endpoints using FastAPI request and response APIs enabled in your functions. These APIs enable the host to process data in HTTP messages as chunks instead of having to read an entire message into memory.
1015+
1016+
### Prerequisites
1017+
1018+
* [Azure Functions runtime](functions-versions.md?pivots=programming-language-python) version 4.34.1, or a later version.
1019+
* [Python](https://www.python.org/downloads/) version 3.8, or a later [supported version](#python-version).
1020+
1021+
### Enable HTTP streams
1022+
1023+
HTTP streams are disabled by default. You need to enable this feature in your application settings and also update your code to use the FastAPI package. Note that when enabling HTTP streams, the function app will default to using HTTP streaming, and the original HTTP functionality will not work.
1024+
1025+
1. Add the `azurefunctions-extensions-http-fastapi` extension package to the `requirements.txt` file in the project, which should include at least these packages:
1026+
1027+
:::code language="text" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/requirements.txt" range="5-6" :::
1028+
1029+
1. Add this code to the `function_app.py` file in the project, which imports the FastAPI extension:
1030+
1031+
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/function_app.py" range="8" :::
1032+
1033+
1. When you deploy to Azure, add the following [application setting](./functions-how-to-use-azure-function-app-settings.md#settings) in your function app:
1034+
1035+
`"PYTHON_ENABLE_INIT_INDEXING": "1"`
1036+
1037+
When running locally, you also need to add these same settings to the `local.settings.json` project file.
1038+
1039+
### HTTP streams examples
1040+
1041+
After you enable the HTTP streaming feature, you can create functions that stream data over HTTP.
1042+
1043+
This example is an HTTP triggered function that receives and processes streaming data from a client in real time. It demonstrates streaming upload capabilities that can be helpful for scenarios like processing continuous data streams and handling event data from IoT devices.
1044+
1045+
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_upload/function_app.py" range="5-25" :::
1046+
1047+
### Calling HTTP streams
1048+
1049+
You must use an HTTP client library to make streaming calls to a function's FastAPI endpoints. The client tool or browser you're using might not natively support streaming or could only return the first chunk of data.
1050+
1051+
You can use a client script like this to send streaming data to an HTTP endpoint:
1052+
1053+
```python
1054+
import httpx # Be sure to add 'httpx' to 'requirements.txt'
1055+
import asyncio
1056+
1057+
async def stream_generator(file_path):
1058+
chunk_size = 2 * 1024 # Define your own chunk size
1059+
with open(file_path, 'rb') as file:
1060+
while chunk := file.read(chunk_size):
1061+
yield chunk
1062+
print(f"Sent chunk: {len(chunk)} bytes")
1063+
1064+
async def stream_to_server(url, file_path):
1065+
timeout = httpx.Timeout(60.0, connect=60.0)
1066+
async with httpx.AsyncClient(timeout=timeout) as client:
1067+
response = await client.post(url, content=stream_generator(file_path))
1068+
return response
1069+
1070+
async def stream_response(response):
1071+
if response.status_code == 200:
1072+
async for chunk in response.aiter_raw():
1073+
print(f"Received chunk: {len(chunk)} bytes")
1074+
else:
1075+
print(f"Error: {response}")
1076+
1077+
async def main():
1078+
print('helloworld')
1079+
# Customize your streaming endpoint served from core tool in variable 'url' if different.
1080+
url = 'http://localhost:7071/api/streaming_upload'
1081+
file_path = r'<file path>'
1082+
1083+
response = await stream_to_server(url, file_path)
1084+
print(response)
1085+
1086+
if __name__ == "__main__":
1087+
asyncio.run(main())
1088+
```
1089+
10151090

10161091
>[!IMPORTANT]
1017-
> HTTP streams support for Python is currently in preview and is only supported for the Python v2 programming model.
1092+
> HTTP streams support for Python is Generally Available and is only supported for the Python v2 programming model.
1093+
10181094
::: zone-end
10191095
### Working with client identities
10201096

articles/azure-functions/functions-reference-python.md

Lines changed: 9 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ For select triggers and bindings, you can work with data types implemented by th
513513

514514
### SDK Types
515515

516-
| Service | Trigger | Input binding | Output binding |
517-
|-|-|-|-|
518-
| [Azure Blobs][blob-sdk-types] | **Generally Available** | **Generally Available** | _SDK types not recommended.<sup>1</sup>_ |
519-
| [Azure Service Bus][servicebus-sdk-types] | **Preview** | _Input binding doesn't exist_ | _SDK types not recommended.<sup>1</sup>_ |
520-
| [Azure Event Hubs][eventhub-sdk-types] | **Preview** | _Input binding doesn't exist_ | _SDK types not recommended.<sup>1</sup>_ |
521-
| [Azure Cosmos DB][cosmos-sdk-types] | _SDK types not used<sup>2</sup>_ | **Preview** | _SDK types not recommended.<sup>1</sup>_ |
516+
| Service | Trigger | Input binding | Output binding | Samples |
517+
|-------------------------------------------|----------------------------------|-------------------------------|------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
518+
| [Azure Blobs][blob-sdk-types] | **Generally Available** | **Generally Available** | _SDK types not recommended.<sup>1</sup>_ | [Quickstart](https://github.com/Azure-Samples/azure-functions-blob-sdk-bindings-python),<br/>[`BlobClient`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_blobclient/function_app.py),<br/>[`ContainerClient`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_containerclient/function_app.py),<br/>[`StorageStreamDownloader`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_storagestreamdownloader/function_app.py) |
519+
| [Azure Cosmos DB][cosmos-sdk-types] | _SDK types not used<sup>2</sup>_ | **Preview** | _SDK types not recommended.<sup>1</sup>_ | [Quickstart](https://github.com/Azure-Samples/azure-functions-cosmosdb-sdk-bindings-python), <br/> [`ContainerProxy`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_containerproxy/function_app.py),<br/>[`CosmosClient`](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_cosmosclient/function_app.py),<br/>[`DatabaseProxy`](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_databaseproxy/function_app.py) |
520+
| [Azure Event Hubs][eventhub-sdk-types] | **Preview** | _Input binding doesn't exist_ | _SDK types not recommended.<sup>1</sup>_ | [Quickstart](https://github.com/Azure-Samples/azure-functions-eventhub-sdk-bindings-python), <br/> [`EventData`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-eventhub/samples/eventhub_samples_eventdata/function_app.py) |
521+
| [Azure Service Bus][servicebus-sdk-types] | **Preview** | _Input binding doesn't exist_ | _SDK types not recommended.<sup>1</sup>_ | [Quickstart](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-servicebus/samples/README.md), <br/> [`ServiceBusReceivedMessage`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-servicebus/samples/servicebus_samples_single/function_app.py) |
522522

523523
[blob-sdk-types]: ./functions-bindings-storage-blob.md?pivots=programming-language-python#sdk-binding-types
524524
[cosmos-sdk-types]: ./functions-bindings-cosmosdb-v2.md?pivots=programming-language-python#sdk-binding-types
@@ -535,91 +535,15 @@ For select triggers and bindings, you can work with data types implemented by th
535535
HTTP streams lets you accept and return data from your HTTP endpoints using FastAPI request and response APIs enabled in your functions. These APIs lets the host process large data in HTTP messages as chunks instead of reading an entire message into memory.
536536

537537
This feature makes it possible to handle large data stream, OpenAI integrations, deliver dynamic content, and support other core HTTP scenarios requiring real-time interactions over HTTP. You can also use FastAPI response types with HTTP streams. Without HTTP streams, the size of your HTTP requests and responses are limited by memory restrictions that can be encountered when processing entire message payloads all in memory.
538+
539+
To learn more, including how to enable HTTP streams in your project, see [HTTP Streams](functions-bindings-http-webhook-trigger.md?tabs=python-v2&pivots=programming-language-python#http-streams).
538540
::: zone pivot="python-mode-configuration"
539541
> [!IMPORTANT]
540542
> Support for HTTP streams requires the [Python v2 programming model](functions-reference-python.md?pivots=python-mode-decorators#http-streams).
541543
::: zone-end
542544
::: zone pivot="python-mode-decorators"
543545
> [!IMPORTANT]
544-
> HTTP streams support for Python is Generally Available and requires you to use the Python v2 programming model.
545-
546-
### Prerequisites
547-
548-
* [Azure Functions runtime](functions-versions.md?pivots=programming-language-python) version 4.34.1, or a later version.
549-
* [Python](https://www.python.org/downloads/) version 3.8, or a later [supported version](#python-version).
550-
551-
### Enable HTTP streams
552-
553-
HTTP streams are disabled by default. You need to enable this feature in your application settings and also update your code to use the FastAPI package. Note that when enabling HTTP streams, the function app will default to using HTTP streaming, and the original HTTP functionality will not work.
554-
555-
1. Add the `azurefunctions-extensions-http-fastapi` extension package to the `requirements.txt` file in the project, which should include at least these packages:
556-
557-
:::code language="text" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/requirements.txt" range="5-6" :::
558-
559-
1. Add this code to the `function_app.py` file in the project, which imports the FastAPI extension:
560-
561-
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/function_app.py" range="8" :::
562-
563-
1. When you deploy to Azure, add the following [application setting](./functions-how-to-use-azure-function-app-settings.md#settings) in your function app:
564-
565-
`"PYTHON_ENABLE_INIT_INDEXING": "1"`
566-
567-
When running locally, you also need to add these same settings to the `local.settings.json` project file.
568-
569-
### HTTP streams examples
570-
571-
After you enable the HTTP streaming feature, you can create functions that stream data over HTTP.
572-
573-
This example is an HTTP triggered function that streams HTTP response data. You might use these capabilities to support scenarios like sending event data through a pipeline for real time visualization or detecting anomalies in large sets of data and providing instant notifications.
574-
575-
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_download/function_app.py" range="5-26" :::
576-
577-
This example is an HTTP triggered function that receives and processes streaming data from a client in real time. It demonstrates streaming upload capabilities that can be helpful for scenarios like processing continuous data streams and handling event data from IoT devices.
578-
579-
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-http-fastapi/samples/fastapi_samples_streaming_upload/function_app.py" range="5-25" :::
580-
581-
### Calling HTTP streams
582-
583-
You must use an HTTP client library to make streaming calls to a function's FastAPI endpoints. The client tool or browser you're using might not natively support streaming or could only return the first chunk of data.
584-
585-
You can use a client script like this to send streaming data to an HTTP endpoint:
586-
587-
```python
588-
import httpx # Be sure to add 'httpx' to 'requirements.txt'
589-
import asyncio
590-
591-
async def stream_generator(file_path):
592-
chunk_size = 2 * 1024 # Define your own chunk size
593-
with open(file_path, 'rb') as file:
594-
while chunk := file.read(chunk_size):
595-
yield chunk
596-
print(f"Sent chunk: {len(chunk)} bytes")
597-
598-
async def stream_to_server(url, file_path):
599-
timeout = httpx.Timeout(60.0, connect=60.0)
600-
async with httpx.AsyncClient(timeout=timeout) as client:
601-
response = await client.post(url, content=stream_generator(file_path))
602-
return response
603-
604-
async def stream_response(response):
605-
if response.status_code == 200:
606-
async for chunk in response.aiter_raw():
607-
print(f"Received chunk: {len(chunk)} bytes")
608-
else:
609-
print(f"Error: {response}")
610-
611-
async def main():
612-
print('helloworld')
613-
# Customize your streaming endpoint served from core tool in variable 'url' if different.
614-
url = 'http://localhost:7071/api/streaming_upload'
615-
file_path = r'<file path>'
616-
617-
response = await stream_to_server(url, file_path)
618-
print(response)
619-
620-
if __name__ == "__main__":
621-
asyncio.run(main())
622-
```
546+
> HTTP streams support for Python is Generally Available and requires you to use the Python v2 programming model.
623547
624548
::: zone-end
625549

0 commit comments

Comments
 (0)