Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7780b58
testing 1p
kappratiksha Oct 21, 2022
7b8128f
handle response
kappratiksha Oct 25, 2022
aac3cae
handle exceptions
kappratiksha Oct 26, 2022
e7d5cf3
remove print statements
kappratiksha Oct 26, 2022
56f63a8
Merge branch 'GoogleCloudPlatform:master' into master
kappratiksha Nov 9, 2022
8a26b33
some changes
kappratiksha Nov 9, 2022
1d1fbfb
Merge branch 'master' of https://github.com/kappratiksha/functions-fr…
kappratiksha Nov 9, 2022
9855468
push more 1p changes
kappratiksha Nov 21, 2022
c7f94d1
Merge branch 'GoogleCloudPlatform:master' into master
kappratiksha Nov 21, 2022
0c74e60
add exceptions
kappratiksha Nov 22, 2022
a7e26e8
add unit tests
kappratiksha Nov 29, 2022
0108b31
lint fix
kappratiksha Nov 29, 2022
7febdf2
fix imports
kappratiksha Nov 29, 2022
4526f7a
use built-in exceptions
kappratiksha Dec 2, 2022
f288727
Merge branch 'GoogleCloudPlatform:master' into master
kappratiksha Dec 5, 2022
2593071
add more tests
kappratiksha Dec 5, 2022
a38248e
Merge branch 'master' of https://github.com/kappratiksha/functions-fr…
kappratiksha Dec 5, 2022
66e5569
address comments
kappratiksha Dec 7, 2022
fc2e7c5
refactor the decorator function
kappratiksha Dec 8, 2022
7dbbfed
add more tests
kappratiksha Dec 8, 2022
29db0b9
Merge branch 'master' into master
kappratiksha Dec 9, 2022
1265c12
add comments
kappratiksha Dec 12, 2022
2f04db3
Merge branch 'master' of https://github.com/kappratiksha/functions-fr…
kappratiksha Dec 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@
# limitations under the License.

import functools
import inspect
import io
import json
import logging
import os.path
import pathlib
import sys
import types

from inspect import signature
from typing import Type

import cloudevents.exceptions as cloud_exceptions
import flask
import werkzeug

from cloudevents.http import from_http, is_binary

from functions_framework import _function_registry, event_conversion
from functions_framework import _function_registry, event_conversion, typed_event
from functions_framework.background_event import BackgroundEvent
from functions_framework.exceptions import (
EventConversionException,
Expand Down Expand Up @@ -67,6 +72,32 @@ def wrapper(*args, **kwargs):
return wrapper


def typed(googleType):
# no parameter to the decorator
if isinstance(googleType, types.FunctionType):
func = googleType
typed_event.register_typed_event("", func)

@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)

return wrapper
# type parameter provided to the decorator
else:

def func_decorator(func):
typed_event.register_typed_event(googleType, func)

@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)

return wrapper

return func_decorator


def http(func):
"""Decorator that registers http as user function signature type."""
_function_registry.REGISTRY_MAP[
Expand Down Expand Up @@ -106,6 +137,21 @@ def _run_cloud_event(function, request):
function(event)


def _typed_event_func_wrapper(function, request, inputType: Type):
def view_func(path):
data = request.get_json()
input = inputType.from_dict(data)
response = function(input)
if response is None:
return "OK"
if response.__class__.__module__ == "builtins":
return response
typed_event.validate_return_type(response)
return json.dumps(response.to_dict())

return view_func


def _cloud_event_view_func_wrapper(function, request):
def view_func(path):
ce_exception = None
Expand Down Expand Up @@ -174,7 +220,7 @@ def view_func(path):
return view_func


def _configure_app(app, function, signature_type):
def _configure_app(app, function, signature_type, inputType):
# Mount the function at the root. Support GCF's default path behavior
# Modify the url_map and view_functions directly here instead of using
# add_url_rule in order to create endpoints that route all methods
Expand Down Expand Up @@ -216,6 +262,21 @@ def _configure_app(app, function, signature_type):
app.view_functions[signature_type] = _cloud_event_view_func_wrapper(
function, flask.request
)
elif signature_type == _function_registry.TYPED_SIGNATURE_TYPE:
# validity_check()
app.url_map.add(
werkzeug.routing.Rule(
"/", defaults={"path": ""}, endpoint=signature_type, methods=["POST"]
)
)
app.url_map.add(
werkzeug.routing.Rule(
"/<path:path>", endpoint=signature_type, methods=["POST"]
)
)
app.view_functions[signature_type] = _typed_event_func_wrapper(
function, flask.request, inputType
)
else:
raise FunctionsFrameworkException(
"Invalid signature type: {signature_type}".format(
Expand Down Expand Up @@ -288,8 +349,9 @@ def handle_none(rv):
# Get the configured function signature type
signature_type = _function_registry.get_func_signature_type(target, signature_type)
function = _function_registry.get_user_function(source, source_module, target)
inputType = _function_registry.get_func_input_type(target)

_configure_app(_app, function, signature_type)
_configure_app(_app, function, signature_type, inputType)

return _app

Expand Down
2 changes: 1 addition & 1 deletion src/functions_framework/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@click.option(
"--signature-type",
envvar="FUNCTION_SIGNATURE_TYPE",
type=click.Choice(["http", "event", "cloudevent"]),
type=click.Choice(["http", "event", "cloudevent", "typed"]),
default="http",
)
@click.option("--host", envvar="HOST", type=click.STRING, default="0.0.0.0")
Expand Down
11 changes: 11 additions & 0 deletions src/functions_framework/_function_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import sys
import types

from re import T
from typing import Type

from functions_framework.exceptions import (
InvalidConfigurationException,
InvalidTargetTypeException,
Expand All @@ -28,10 +31,13 @@
HTTP_SIGNATURE_TYPE = "http"
CLOUDEVENT_SIGNATURE_TYPE = "cloudevent"
BACKGROUNDEVENT_SIGNATURE_TYPE = "event"
TYPED_SIGNATURE_TYPE = "typed"

# REGISTRY_MAP stores the registered functions.
# Keys are user function names, values are user function signature types.
REGISTRY_MAP = {}
INPUT_MAP = {}
CONTEXT_MAP = {}


def get_user_function(source, source_module, target):
Expand Down Expand Up @@ -120,3 +126,8 @@ def get_func_signature_type(func_name: str, signature_type: str) -> str:
if os.environ.get("ENTRY_POINT"):
os.environ["FUNCTION_TRIGGER_TYPE"] = sig_type
return sig_type


def get_func_input_type(func_name: str) -> Type:
registered_type = INPUT_MAP[func_name] if func_name in INPUT_MAP else ""
return registered_type
4 changes: 4 additions & 0 deletions src/functions_framework/event_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@


def background_event_to_cloud_event(request) -> CloudEvent:
print("background_event_to_cloud_event")
"""Converts a background event represented by the given HTTP request into a CloudEvent."""
event_data = marshal_background_event_data(request)
if not event_data:
Expand Down Expand Up @@ -185,6 +186,7 @@ def background_event_to_cloud_event(request) -> CloudEvent:


def is_convertable_cloud_event(request) -> bool:
print("is_convertable_cloud_event")
"""Is the given request a known CloudEvent that can be converted to background event."""
if is_binary(request.headers):
event_type = request.headers.get("ce-type")
Expand All @@ -208,6 +210,7 @@ def _split_ce_source(source) -> Tuple[str, str]:


def cloud_event_to_background_event(request) -> Tuple[Any, Context]:
print("cloud_event_to_background_event")
"""Converts a background event represented by the given HTTP request into a CloudEvent."""
try:
event = from_http(request.headers, request.get_data())
Expand Down Expand Up @@ -294,6 +297,7 @@ def _split_resource(context: Context) -> Tuple[str, str, str]:


def marshal_background_event_data(request):
print("marshal_background_event_data")
"""Marshal the request body of a raw Pub/Sub HTTP request into the schema that is expected of
a background event"""
try:
Expand Down
101 changes: 101 additions & 0 deletions src/functions_framework/typed_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import inspect

from inspect import signature

from functions_framework import _function_registry


class TypedEvent(object):

# Supports v1beta1, v1beta2, and v1 event formats.
def __init__(
self,
data,
):
self.data = data


def register_typed_event2(decorator_type, contextSet, func):
print("######")
print(decorator_type)
print(contextSet)
sig = signature(func)
annotation_type = list(sig.parameters.values())[0].annotation
print(annotation_type)
type_validity_check(decorator_type, annotation_type)
if decorator_type == "":
decorator_type = annotation_type

_function_registry.INPUT_MAP[func.__name__] = decorator_type
_function_registry.REGISTRY_MAP[
func.__name__
] = _function_registry.TYPED_SIGNATURE_TYPE
_function_registry.CONTEXT_MAP[func.__name__] = contextSet


def register_typed_event(decorator_type, func):
sig = signature(func)
annotation_type = list(sig.parameters.values())[0].annotation

type_validity_check(decorator_type, annotation_type)
if decorator_type == "":
decorator_type = annotation_type

_function_registry.INPUT_MAP[func.__name__] = decorator_type
_function_registry.REGISTRY_MAP[
func.__name__
] = _function_registry.TYPED_SIGNATURE_TYPE


def validate_return_type(response):
if not (hasattr(response, "to_dict") and callable(getattr(response, "to_dict"))):
raise AttributeError(
"The type {response} does not have the required method called "
" 'to_dict'.".format(response=response)
)


def type_validity_check(decorator_type, annotation_type):
if decorator_type == "" and annotation_type is inspect._empty:
raise TypeError(
"The function defined does not contain Type of the input object."
)

if (
decorator_type != ""
and annotation_type is not inspect._empty
and decorator_type != annotation_type
):
raise TypeError(
"The object type provided via 'typed' {decorator_type}"
"is different from the one in the function annotation {annotation_type}.".format(
decorator_type=decorator_type, annotation_type=annotation_type
)
)

if decorator_type == "":
decorator_type = annotation_type

if not (
hasattr(decorator_type, "from_dict")
and callable(getattr(decorator_type, "from_dict"))
):
raise AttributeError(
"The type {decorator_type} does not have the required method called "
" 'from_dict'.".format(decorator_type=decorator_type)
)
55 changes: 55 additions & 0 deletions tests/test_functions/typed_events/missing_from_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Function used to test handling functions using typed decorators."""
from typing import Any, TypeVar

import flask

import functions_framework

T = TypeVar("T")


def from_str(x: Any) -> str:
assert isinstance(x, str)
return x


def from_int(x: Any) -> int:
assert isinstance(x, int) and not isinstance(x, bool)
return x


class TestTypeMissingFromDict:
name: str
age: int

def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age

def to_dict(self) -> dict:
result: dict = {}
result["name"] = from_str(self.name)
result["age"] = from_int(self.age)
return result


@functions_framework.typed(TestTypeMissingFromDict)
def function_typed_missing_from_dict(test_type: TestTypeMissingFromDict):
valid_event = test_type.name == "john" and test_type.age == 10
if not valid_event:
flask.abort(500)
return test_type
Loading