azure-functions-runtime-v1 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. azure_functions_runtime_v1-1.0.0/PKG-INFO +111 -0
  2. azure_functions_runtime_v1-1.0.0/README.md +71 -0
  3. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/__init__.py +22 -0
  4. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/context.py +69 -0
  5. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/datumdef.py +233 -0
  6. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/generic.py +74 -0
  7. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/meta.py +170 -0
  8. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/nullable_converters.py +126 -0
  9. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/out.py +13 -0
  10. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/retrycontext.py +35 -0
  11. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/bindings/tracecontext.py +44 -0
  12. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/functions.py +392 -0
  13. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/handle_event.py +319 -0
  14. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/loader.py +102 -0
  15. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/logging.py +25 -0
  16. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/otel.py +116 -0
  17. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/__init__.py +2 -0
  18. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/app_setting_manager.py +100 -0
  19. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/constants.py +60 -0
  20. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/executor.py +34 -0
  21. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/helpers.py +38 -0
  22. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/threadpool.py +86 -0
  23. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/tracing.py +69 -0
  24. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/typing_inspect.py +294 -0
  25. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/validators.py +20 -0
  26. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/utils/wrappers.py +52 -0
  27. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1/version.py +4 -0
  28. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1.egg-info/PKG-INFO +111 -0
  29. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1.egg-info/SOURCES.txt +58 -0
  30. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1.egg-info/dependency_links.txt +1 -0
  31. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1.egg-info/requires.txt +20 -0
  32. azure_functions_runtime_v1-1.0.0/azure_functions_runtime_v1.egg-info/top_level.txt +3 -0
  33. azure_functions_runtime_v1-1.0.0/pyproject.toml +91 -0
  34. azure_functions_runtime_v1-1.0.0/setup.cfg +4 -0
  35. azure_functions_runtime_v1-1.0.0/tests/protos/FunctionRpc_pb2.py +215 -0
  36. azure_functions_runtime_v1-1.0.0/tests/protos/FunctionRpc_pb2_grpc.py +69 -0
  37. azure_functions_runtime_v1-1.0.0/tests/protos/__init__.py +43 -0
  38. azure_functions_runtime_v1-1.0.0/tests/protos/identity/ClaimsIdentityRpc_pb2.py +29 -0
  39. azure_functions_runtime_v1-1.0.0/tests/protos/identity/ClaimsIdentityRpc_pb2_grpc.py +4 -0
  40. azure_functions_runtime_v1-1.0.0/tests/protos/identity/__init__.py +0 -0
  41. azure_functions_runtime_v1-1.0.0/tests/protos/shared/NullableTypes_pb2.py +33 -0
  42. azure_functions_runtime_v1-1.0.0/tests/protos/shared/NullableTypes_pb2_grpc.py +4 -0
  43. azure_functions_runtime_v1-1.0.0/tests/protos/shared/__init__.py +0 -0
  44. azure_functions_runtime_v1-1.0.0/tests/unittests/default_template/__init__.py +28 -0
  45. azure_functions_runtime_v1-1.0.0/tests/unittests/test_app_setting_manager.py +45 -0
  46. azure_functions_runtime_v1-1.0.0/tests/unittests/test_code_quality.py +36 -0
  47. azure_functions_runtime_v1-1.0.0/tests/unittests/test_datumdef.py +161 -0
  48. azure_functions_runtime_v1-1.0.0/tests/unittests/test_handle_event.py +137 -0
  49. azure_functions_runtime_v1-1.0.0/tests/unittests/test_logging.py +32 -0
  50. azure_functions_runtime_v1-1.0.0/tests/unittests/test_nullable_converters.py +110 -0
  51. azure_functions_runtime_v1-1.0.0/tests/unittests/test_opentelemetry.py +209 -0
  52. azure_functions_runtime_v1-1.0.0/tests/unittests/test_rpc_messages.py +124 -0
  53. azure_functions_runtime_v1-1.0.0/tests/unittests/test_threadpool.py +50 -0
  54. azure_functions_runtime_v1-1.0.0/tests/unittests/test_types.py +164 -0
  55. azure_functions_runtime_v1-1.0.0/tests/unittests/test_typing_inspect.py +142 -0
  56. azure_functions_runtime_v1-1.0.0/tests/unittests/test_utilities.py +328 -0
  57. azure_functions_runtime_v1-1.0.0/tests/utils/__init__.py +2 -0
  58. azure_functions_runtime_v1-1.0.0/tests/utils/constants.py +7 -0
  59. azure_functions_runtime_v1-1.0.0/tests/utils/mock_classes.py +49 -0
  60. azure_functions_runtime_v1-1.0.0/tests/utils/testutils.py +34 -0
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.4
2
+ Name: azure-functions-runtime-v1
3
+ Version: 1.0.0
4
+ Summary: Python Language Worker for Azure Functions V1 Runtime
5
+ Author-email: "Azure Functions team at Microsoft Corp." <azurefunctions@microsoft.com>
6
+ Project-URL: Documentation, https://github.com/Azure/azure-functions-python-worker/blob/dev/azure_functions_runtime_v1/README.md
7
+ Project-URL: Repository, https://github.com/Azure/azure-functions-python-worker
8
+ Keywords: azure,functions,azurefunctions,python,serverless
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Programming Language :: Python
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Operating System :: Microsoft :: Windows
14
+ Classifier: Operating System :: POSIX
15
+ Classifier: Operating System :: MacOS :: MacOS X
16
+ Classifier: Environment :: Web Environment
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Classifier: Intended Audience :: Developers
19
+ Requires-Python: >=3.13
20
+ Description-Content-Type: text/markdown
21
+ Requires-Dist: azure-functions
22
+ Provides-Extra: dev
23
+ Requires-Dist: azure-monitor-opentelemetry; extra == "dev"
24
+ Requires-Dist: flake8==6.*; extra == "dev"
25
+ Requires-Dist: pytest; extra == "dev"
26
+ Requires-Dist: requests==2.*; extra == "dev"
27
+ Requires-Dist: coverage; extra == "dev"
28
+ Requires-Dist: grpcio~=1.70.0; extra == "dev"
29
+ Requires-Dist: grpcio-tools~=1.70.0; extra == "dev"
30
+ Requires-Dist: protobuf~=5.29.0; extra == "dev"
31
+ Requires-Dist: pytest-sugar; extra == "dev"
32
+ Requires-Dist: pytest-cov; extra == "dev"
33
+ Requires-Dist: pytest-xdist; extra == "dev"
34
+ Requires-Dist: pytest-randomly; extra == "dev"
35
+ Requires-Dist: pytest-instafail; extra == "dev"
36
+ Requires-Dist: pytest-rerunfailures; extra == "dev"
37
+ Requires-Dist: pytest-asyncio; extra == "dev"
38
+ Requires-Dist: pre-commit; extra == "dev"
39
+ Requires-Dist: invoke; extra == "dev"
40
+
41
+ # <img src="https://raw.githubusercontent.com/Azure/azure-functions-python-worker/dev/docs/Azure.Functions.svg" width = "30" alt="Functions Header Image - Lightning Logo"> Azure Functions Python Worker
42
+
43
+ | Branch | Build Status | CodeCov | Test Status |
44
+ |--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
45
+ | dev | [![Build Status](https://img.shields.io/azure-devops/build/azfunc/public/658/dev)](https://azfunc.visualstudio.com/public/_build/latest?definitionId=658&branchName=dev) | [![codecov](https://codecov.io/gh/Azure/azure-functions-python-worker/branch/dev/graph/badge.svg)](https://codecov.io/gh/Azure/azure-functions-python-worker) | [![Test Status](https://img.shields.io/azure-devops/build/azfunc/public/658/dev)](https://azfunc.visualstudio.com/public/_build/latest?definitionId=658&branchName=dev) |
46
+
47
+ Python support for Azure Functions is based on Python 3.13 serverless hosting on Linux and the Functions 4.0 runtime.
48
+
49
+ Here is the current status of Python in Azure Functions:
50
+
51
+ What are the supported Python versions?
52
+
53
+ | Azure Functions Runtime | Python 3.13 |
54
+ |----------------------------------|-------------|
55
+ | Azure Functions 4.0 | ✔ |
56
+
57
+ For information about Azure Functions Runtime, please refer to [Azure Functions runtime versions overview](https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions) page.
58
+
59
+ ### What's available?
60
+
61
+ - Build, test, debug, and publish using Azure Functions Core Tools (CLI) or Visual Studio Code
62
+ - Deploy Python Function project onto consumption, dedicated, elastic premium, or flex consumption plan.
63
+ - Deploy Python Function project in a custom docker image onto dedicated or elastic premium plan.
64
+ - Triggers / Bindings : Blob, Cosmos DB, Event Grid, Event Hub, HTTP, Kafka, MySQL, Queue, ServiceBus, SQL, Timer, and Warmup
65
+ - Triggers / Bindings : Custom binding support
66
+
67
+ ### What's new?
68
+
69
+ - [SDK Type Bindings for Blob](https://techcommunity.microsoft.com/t5/azure-compute-blog/azure-functions-sdk-type-bindings-for-azure-blob-storage-with/ba-p/4146744)
70
+ - [HTTP Streaming](https://techcommunity.microsoft.com/t5/azure-compute-blog/azure-functions-support-for-http-streams-in-python-is-now-in/ba-p/4146697)
71
+
72
+ ### Get Started
73
+
74
+ - [Create your first Python function](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python)
75
+ - [Developer guide](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python)
76
+ - [Binding API reference](https://docs.microsoft.com/en-us/python/api/azure-functions/azure.functions?view=azure-python)
77
+ - [Develop using VS Code](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code)
78
+ - [Create a Python Function on Linux using a custom docker image](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image)
79
+
80
+ # Give Feedback
81
+
82
+ Issues and feature requests are tracked in a variety of places. To report this feedback, please file an issue to the relevant repository below:
83
+
84
+ | Item | Description | Link |
85
+ |---------------|----------------------------------------------|--------------------------------------------------------------------------------|
86
+ | Python Worker | Programming Model, Triggers & Bindings | [File an Issue](https://github.com/Azure/azure-functions-python-worker/issues) |
87
+ | Runtime | Script Host & Language Extensibility | [File an Issue](https://github.com/Azure/azure-functions-host/issues) |
88
+ | VSCode | VSCode Extension for Azure Functions | [File an Issue](https://github.com/microsoft/vscode-azurefunctions/issues) |
89
+ | Core Tools | Command Line Interface for Local Development | [File an Issue](https://github.com/Azure/azure-functions-core-tools/issues) |
90
+ | Templates | Code Issues with Creation Template | [File an Issue](https://github.com/Azure/azure-functions-templates/issues) |
91
+
92
+ # Contribute
93
+
94
+ This project welcomes contributions and suggestions. Most contributions require you to agree to a
95
+ Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
96
+ the rights to use your contribution. For details, visit https://cla.microsoft.com.
97
+
98
+ Here are some pointers to get started:
99
+
100
+ - [Language worker architecture](https://github.com/Azure/azure-functions-python-worker/wiki/Worker-Architecture)
101
+ - [Setting up the development environment](https://github.com/Azure/azure-functions-python-worker/wiki/Contributor-Guide)
102
+ - [Adding support for a new binding](https://github.com/Azure/azure-functions-python-worker/wiki/Adding-support-for-a-new-binding-type)
103
+ - [Release instructions](https://github.com/Azure/azure-functions-python-worker/wiki/Release-Instructions)
104
+
105
+ When you submit a pull request, a CLA-bot will automatically determine whether you need to provide
106
+ a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions
107
+ provided by the bot. You will only need to do this once across all repos using our CLA.
108
+
109
+ This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
110
+ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
111
+ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
@@ -0,0 +1,71 @@
1
+ # <img src="https://raw.githubusercontent.com/Azure/azure-functions-python-worker/dev/docs/Azure.Functions.svg" width = "30" alt="Functions Header Image - Lightning Logo"> Azure Functions Python Worker
2
+
3
+ | Branch | Build Status | CodeCov | Test Status |
4
+ |--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
5
+ | dev | [![Build Status](https://img.shields.io/azure-devops/build/azfunc/public/658/dev)](https://azfunc.visualstudio.com/public/_build/latest?definitionId=658&branchName=dev) | [![codecov](https://codecov.io/gh/Azure/azure-functions-python-worker/branch/dev/graph/badge.svg)](https://codecov.io/gh/Azure/azure-functions-python-worker) | [![Test Status](https://img.shields.io/azure-devops/build/azfunc/public/658/dev)](https://azfunc.visualstudio.com/public/_build/latest?definitionId=658&branchName=dev) |
6
+
7
+ Python support for Azure Functions is based on Python 3.13 serverless hosting on Linux and the Functions 4.0 runtime.
8
+
9
+ Here is the current status of Python in Azure Functions:
10
+
11
+ What are the supported Python versions?
12
+
13
+ | Azure Functions Runtime | Python 3.13 |
14
+ |----------------------------------|-------------|
15
+ | Azure Functions 4.0 | ✔ |
16
+
17
+ For information about Azure Functions Runtime, please refer to [Azure Functions runtime versions overview](https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions) page.
18
+
19
+ ### What's available?
20
+
21
+ - Build, test, debug, and publish using Azure Functions Core Tools (CLI) or Visual Studio Code
22
+ - Deploy Python Function project onto consumption, dedicated, elastic premium, or flex consumption plan.
23
+ - Deploy Python Function project in a custom docker image onto dedicated or elastic premium plan.
24
+ - Triggers / Bindings : Blob, Cosmos DB, Event Grid, Event Hub, HTTP, Kafka, MySQL, Queue, ServiceBus, SQL, Timer, and Warmup
25
+ - Triggers / Bindings : Custom binding support
26
+
27
+ ### What's new?
28
+
29
+ - [SDK Type Bindings for Blob](https://techcommunity.microsoft.com/t5/azure-compute-blog/azure-functions-sdk-type-bindings-for-azure-blob-storage-with/ba-p/4146744)
30
+ - [HTTP Streaming](https://techcommunity.microsoft.com/t5/azure-compute-blog/azure-functions-support-for-http-streams-in-python-is-now-in/ba-p/4146697)
31
+
32
+ ### Get Started
33
+
34
+ - [Create your first Python function](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python)
35
+ - [Developer guide](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python)
36
+ - [Binding API reference](https://docs.microsoft.com/en-us/python/api/azure-functions/azure.functions?view=azure-python)
37
+ - [Develop using VS Code](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code)
38
+ - [Create a Python Function on Linux using a custom docker image](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image)
39
+
40
+ # Give Feedback
41
+
42
+ Issues and feature requests are tracked in a variety of places. To report this feedback, please file an issue to the relevant repository below:
43
+
44
+ | Item | Description | Link |
45
+ |---------------|----------------------------------------------|--------------------------------------------------------------------------------|
46
+ | Python Worker | Programming Model, Triggers & Bindings | [File an Issue](https://github.com/Azure/azure-functions-python-worker/issues) |
47
+ | Runtime | Script Host & Language Extensibility | [File an Issue](https://github.com/Azure/azure-functions-host/issues) |
48
+ | VSCode | VSCode Extension for Azure Functions | [File an Issue](https://github.com/microsoft/vscode-azurefunctions/issues) |
49
+ | Core Tools | Command Line Interface for Local Development | [File an Issue](https://github.com/Azure/azure-functions-core-tools/issues) |
50
+ | Templates | Code Issues with Creation Template | [File an Issue](https://github.com/Azure/azure-functions-templates/issues) |
51
+
52
+ # Contribute
53
+
54
+ This project welcomes contributions and suggestions. Most contributions require you to agree to a
55
+ Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
56
+ the rights to use your contribution. For details, visit https://cla.microsoft.com.
57
+
58
+ Here are some pointers to get started:
59
+
60
+ - [Language worker architecture](https://github.com/Azure/azure-functions-python-worker/wiki/Worker-Architecture)
61
+ - [Setting up the development environment](https://github.com/Azure/azure-functions-python-worker/wiki/Contributor-Guide)
62
+ - [Adding support for a new binding](https://github.com/Azure/azure-functions-python-worker/wiki/Adding-support-for-a-new-binding-type)
63
+ - [Release instructions](https://github.com/Azure/azure-functions-python-worker/wiki/Release-Instructions)
64
+
65
+ When you submit a pull request, a CLA-bot will automatically determine whether you need to provide
66
+ a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions
67
+ provided by the bot. You will only need to do this once across all repos using our CLA.
68
+
69
+ This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
70
+ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
71
+ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
@@ -0,0 +1,22 @@
1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ from .handle_event import (worker_init_request,
5
+ functions_metadata_request,
6
+ function_environment_reload_request,
7
+ invocation_request,
8
+ function_load_request)
9
+ from .utils.threadpool import (
10
+ start_threadpool_executor,
11
+ stop_threadpool_executor,
12
+ get_threadpool_executor,
13
+ )
14
+
15
+ __all__ = ('worker_init_request',
16
+ 'functions_metadata_request',
17
+ 'function_environment_reload_request',
18
+ 'invocation_request',
19
+ 'function_load_request',
20
+ 'start_threadpool_executor',
21
+ 'stop_threadpool_executor',
22
+ 'get_threadpool_executor')
@@ -0,0 +1,69 @@
1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ import threading
5
+
6
+ from typing import Type
7
+
8
+ from .retrycontext import RetryContext
9
+ from .tracecontext import TraceContext
10
+
11
+
12
+ class Context:
13
+ def __init__(self,
14
+ func_name: str,
15
+ func_dir: str,
16
+ invocation_id: str,
17
+ thread_local_storage: Type[threading.local],
18
+ trace_context: TraceContext,
19
+ retry_context: RetryContext) -> None:
20
+ self.__func_name = func_name
21
+ self.__func_dir = func_dir
22
+ self.__invocation_id = invocation_id
23
+ self.__thread_local_storage = thread_local_storage
24
+ self.__trace_context = trace_context
25
+ self.__retry_context = retry_context
26
+
27
+ @property
28
+ def invocation_id(self) -> str:
29
+ return self.__invocation_id
30
+
31
+ @property
32
+ def thread_local_storage(self) -> Type[threading.local]:
33
+ return self.__thread_local_storage
34
+
35
+ @property
36
+ def function_name(self) -> str:
37
+ return self.__func_name
38
+
39
+ @property
40
+ def function_directory(self) -> str:
41
+ return self.__func_dir
42
+
43
+ @property
44
+ def trace_context(self) -> TraceContext:
45
+ return self.__trace_context
46
+
47
+ @property
48
+ def retry_context(self) -> RetryContext:
49
+ return self.__retry_context
50
+
51
+
52
+ def get_context(invoc_request, name: str,
53
+ directory: str) -> Context:
54
+ """ For more information refer:
55
+ https://aka.ms/azfunc-invocation-context
56
+ """
57
+ trace_context = TraceContext(
58
+ invoc_request.trace_context.trace_parent,
59
+ invoc_request.trace_context.trace_state,
60
+ invoc_request.trace_context.attributes)
61
+
62
+ retry_context = RetryContext(
63
+ invoc_request.retry_context.retry_count,
64
+ invoc_request.retry_context.max_retry_count,
65
+ invoc_request.retry_context.exception)
66
+
67
+ return Context(
68
+ name, directory, invoc_request.invocation_id,
69
+ threading.local(), trace_context, retry_context)
@@ -0,0 +1,233 @@
1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ import json
5
+ import logging
6
+
7
+ from datetime import datetime
8
+ from typing import Any, List, Optional
9
+
10
+ from .nullable_converters import (
11
+ to_nullable_bool,
12
+ to_nullable_double,
13
+ to_nullable_string,
14
+ to_nullable_timestamp,
15
+ )
16
+
17
+ try:
18
+ from http.cookies import SimpleCookie
19
+ except ImportError:
20
+ from Cookie import SimpleCookie
21
+
22
+
23
+ class Datum:
24
+ def __init__(self, value, type):
25
+ self.value = value
26
+ self.type = type
27
+
28
+ @property
29
+ def python_value(self) -> Any:
30
+ if self.value is None or self.type is None:
31
+ return None
32
+ elif self.type in ('bytes', 'string', 'int', 'double'):
33
+ return self.value
34
+ elif self.type == 'json':
35
+ return json.loads(self.value)
36
+ elif self.type == 'collection_string':
37
+ return [v for v in self.value.string]
38
+ elif self.type == 'collection_bytes':
39
+ return [v for v in self.value.bytes]
40
+ elif self.type == 'collection_double':
41
+ return [v for v in self.value.double]
42
+ elif self.type == 'collection_sint64':
43
+ return [v for v in self.value.sint64]
44
+ else:
45
+ return self.value
46
+
47
+ @property
48
+ def python_type(self) -> type:
49
+ return type(self.python_value)
50
+
51
+ def __eq__(self, other):
52
+ if not isinstance(other, type(self)):
53
+ return False
54
+
55
+ return self.value == other.value and self.type == other.type
56
+
57
+ def __hash__(self):
58
+ return hash((type(self), (self.value, self.type)))
59
+
60
+ def __repr__(self):
61
+ val_repr = repr(self.value)
62
+ if len(val_repr) > 10:
63
+ val_repr = val_repr[:10] + '...'
64
+ return '<Datum ' + str(self.type) + val_repr + '>'
65
+
66
+ @classmethod
67
+ def from_typed_data(cls, protos):
68
+ try:
69
+ td = protos.TypedData
70
+ except Exception:
71
+ # Todo: better catch for Datum.from_typed_data(http.body)
72
+ # if the data being sent in is already protos.TypedData
73
+ td = protos
74
+ tt = td.WhichOneof('data')
75
+ if tt == 'http':
76
+ http = td.http
77
+ val = dict(
78
+ method=Datum(http.method, 'string'),
79
+ url=Datum(http.url, 'string'),
80
+ headers={
81
+ k: Datum(v, 'string') for k, v in http.headers.items()
82
+ },
83
+ body=(
84
+ Datum.from_typed_data(http.body)
85
+ or Datum(type='bytes', value=b'')
86
+ ),
87
+ params={
88
+ k: Datum(v, 'string') for k, v in http.params.items()
89
+ },
90
+ query={
91
+ k: Datum(v, 'string') for k, v in http.query.items()
92
+ },
93
+ )
94
+ elif tt == 'string':
95
+ val = td.string
96
+ elif tt == 'bytes':
97
+ val = td.bytes
98
+ elif tt == 'json':
99
+ val = td.json
100
+ elif tt == 'collection_bytes':
101
+ val = td.collection_bytes
102
+ elif tt == 'collection_string':
103
+ val = td.collection_string
104
+ elif tt == 'collection_sint64':
105
+ val = td.collection_sint64
106
+ elif tt == 'model_binding_data':
107
+ val = td.model_binding_data
108
+ elif tt is None:
109
+ return None
110
+ else:
111
+ raise NotImplementedError(
112
+ 'unsupported TypeData kind: {!r}'.format(tt)
113
+ )
114
+
115
+ return cls(val, tt)
116
+
117
+
118
+ def datum_as_proto(datum: Datum, protos):
119
+ if datum.type == 'string':
120
+ return protos.TypedData(string=datum.value)
121
+ elif datum.type == 'bytes':
122
+ return protos.TypedData(bytes=datum.value)
123
+ elif datum.type == 'json':
124
+ return protos.TypedData(json=datum.value)
125
+ elif datum.type == 'http':
126
+ return protos.TypedData(http=protos.RpcHttp(
127
+ status_code=datum.value['status_code'].value,
128
+ headers={
129
+ k: v.value
130
+ for k, v in datum.value['headers'].items()
131
+ },
132
+ cookies=parse_to_rpc_http_cookie_list(datum.value.get('cookies'), protos),
133
+ enable_content_negotiation=False,
134
+ body=datum_as_proto(datum.value['body'], protos),
135
+ ))
136
+ elif datum.type is None:
137
+ return None
138
+ elif datum.type == 'dict':
139
+ # TypedData doesn't support dict, so we return it as json
140
+ return protos.TypedData(json=json.dumps(datum.value))
141
+ elif datum.type == 'list':
142
+ # TypedData doesn't support list, so we return it as json
143
+ return protos.TypedData(json=json.dumps(datum.value))
144
+ elif datum.type == 'int':
145
+ return protos.TypedData(int=datum.value)
146
+ elif datum.type == 'double':
147
+ return protos.TypedData(double=datum.value)
148
+ elif datum.type == 'bool':
149
+ # TypedData doesn't support bool, so we return it as an int
150
+ return protos.TypedData(int=int(datum.value))
151
+ else:
152
+ raise NotImplementedError(
153
+ 'unexpected Datum type: {!r}'.format(datum.type)
154
+ )
155
+
156
+
157
+ def parse_to_rpc_http_cookie_list(cookies: Optional[List[SimpleCookie]], protos):
158
+ if cookies is None:
159
+ return cookies
160
+
161
+ rpc_http_cookies = []
162
+
163
+ for cookie in cookies:
164
+ for name, cookie_entity in cookie.items():
165
+ rpc_http_cookies.append(
166
+ protos.RpcHttpCookie(name=name,
167
+ value=cookie_entity.value,
168
+ domain=to_nullable_string(
169
+ cookie_entity['domain'],
170
+ 'cookie.domain',
171
+ protos),
172
+ path=to_nullable_string(
173
+ cookie_entity['path'],
174
+ 'cookie.path',
175
+ protos),
176
+ expires=to_nullable_timestamp(
177
+ parse_cookie_attr_expires(
178
+ cookie_entity), 'cookie.expires',
179
+ protos),
180
+ secure=to_nullable_bool(
181
+ bool(cookie_entity['secure']),
182
+ 'cookie.secure',
183
+ protos),
184
+ http_only=to_nullable_bool(
185
+ bool(cookie_entity['httponly']),
186
+ 'cookie.httpOnly',
187
+ protos),
188
+ same_site=parse_cookie_attr_same_site(
189
+ cookie_entity, protos),
190
+ max_age=to_nullable_double(
191
+ cookie_entity['max-age'],
192
+ 'cookie.maxAge',
193
+ protos)))
194
+
195
+ return rpc_http_cookies
196
+
197
+
198
+ def parse_cookie_attr_expires(cookie_entity):
199
+ expires = cookie_entity['expires']
200
+
201
+ if expires is not None and len(expires) != 0:
202
+ try:
203
+ return datetime.strptime(expires, "%a, %d %b %Y %H:%M:%S GMT")
204
+ except ValueError:
205
+ logging.error(
206
+ "Can not parse value %s of expires in the cookie "
207
+ "due to invalid format.", expires)
208
+ raise
209
+ except OverflowError:
210
+ logging.error(
211
+ "Can not parse value %s of expires in the cookie "
212
+ "because the parsed date exceeds the largest valid C "
213
+ "integer on your system.", expires)
214
+ raise
215
+
216
+ return None
217
+
218
+
219
+ def parse_cookie_attr_same_site(cookie_entity, protos):
220
+ same_site = getattr(protos.RpcHttpCookie.SameSite, "None")
221
+ try:
222
+ raw_same_site_str = cookie_entity['samesite'].lower()
223
+
224
+ if raw_same_site_str == 'lax':
225
+ same_site = protos.RpcHttpCookie.SameSite.Lax
226
+ elif raw_same_site_str == 'strict':
227
+ same_site = protos.RpcHttpCookie.SameSite.Strict
228
+ elif raw_same_site_str == 'none':
229
+ same_site = protos.RpcHttpCookie.SameSite.ExplicitNone
230
+ except Exception:
231
+ return same_site
232
+
233
+ return same_site
@@ -0,0 +1,74 @@
1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+ import typing
4
+ from typing import Any, Optional
5
+
6
+ from .datumdef import Datum
7
+
8
+
9
+ class GenericBinding:
10
+
11
+ @classmethod
12
+ def has_trigger_support(cls) -> bool:
13
+ return False
14
+
15
+ @classmethod
16
+ def check_input_type_annotation(cls, pytype: type) -> bool:
17
+ return issubclass(pytype, (str, bytes))
18
+
19
+ @classmethod
20
+ def check_output_type_annotation(cls, pytype: type) -> bool:
21
+ return issubclass(pytype, (str, bytes, bytearray))
22
+
23
+ @classmethod
24
+ def encode(cls, obj: Any, *,
25
+ expected_type: Optional[type]) -> Datum:
26
+ if isinstance(obj, str):
27
+ return Datum(type='string', value=obj)
28
+
29
+ elif isinstance(obj, (bytes, bytearray)):
30
+ return Datum(type='bytes', value=bytes(obj))
31
+ elif obj is None:
32
+ return Datum(type=None, value=obj)
33
+ elif isinstance(obj, dict):
34
+ return Datum(type='dict', value=obj)
35
+ elif isinstance(obj, list):
36
+ return Datum(type='list', value=obj)
37
+ elif isinstance(obj, int):
38
+ return Datum(type='int', value=obj)
39
+ elif isinstance(obj, float):
40
+ return Datum(type='double', value=obj)
41
+ elif isinstance(obj, bool):
42
+ return Datum(type='bool', value=obj)
43
+ else:
44
+ raise NotImplementedError
45
+
46
+ @classmethod
47
+ def decode(cls, data: Datum, *, trigger_metadata) -> typing.Any:
48
+ # Enabling support for Dapr bindings
49
+ # https://github.com/Azure/azure-functions-python-worker/issues/1316
50
+ if data is None:
51
+ return None
52
+ data_type = data.type
53
+
54
+ if data_type == 'string':
55
+ result = data.value
56
+ elif data_type == 'bytes':
57
+ result = data.value
58
+ elif data_type == 'json':
59
+ result = data.value
60
+ elif data_type is None:
61
+ result = None
62
+ else:
63
+ raise ValueError(
64
+ 'unexpected type of data received for the "generic" binding ',
65
+ repr(data_type)
66
+ )
67
+
68
+ return result
69
+
70
+ @classmethod
71
+ def has_implicit_output(cls, bind_name: Optional[str]) -> bool:
72
+ if bind_name == 'durableClient':
73
+ return False
74
+ return True