Skip to content

Commit

Permalink
create and update function add check
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed Feb 7, 2018
1 parent f33bdc2 commit 016f893
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Getting started
# Create function.
# the current directory has a main.zip file (main.py which has a function of myhandler)
client.create_function('service_name', 'function_name', 'main.my_handler', codeZipFile = 'main.zip')
client.create_function('service_name', 'function_name', 'python3', 'main.my_handler', codeZipFile = 'main.zip')
# Invoke function synchronously.
client.invoke_function('service_name', 'function_name')
Expand Down
2 changes: 1 addition & 1 deletion fc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__author__ = 'Aliyun Function Compute'
__version__ = '2.0.2'
__version__ = '2.0.4'

from .client import Client
from .fc_exceptions import FcError
Expand Down
8 changes: 7 additions & 1 deletion fc2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from . import fc_exceptions
import platform

from .util import typeassert


class Client(object):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -292,6 +294,8 @@ def _check_function_param_valid(self, codeZipFile, codeDir, codeOSSBucket, codeO

return True


@typeassert(serviceName=str, functionName=str, runtime=str, handler=str, memorySize=(int, type(None)), timeout=(int, type(None)))
def create_function(
self, serviceName, functionName, runtime, handler,
codeZipFile=None, codeDir=None, codeOSSBucket=None, codeOSSObject=None,
Expand Down Expand Up @@ -362,7 +366,8 @@ def create_function(
r = self._do_request(method, path, headers, body=json.dumps(payload).encode('utf-8'))
# 'etag' now in headers
return FcHttpResponse(r.headers, r.json())


@typeassert(serviceName=str, functionName=str, handler = (str, type(None)), runtime = (str, type(None)), memorySize=(int, type(None)), timeout=(int, type(None)) )
def update_function(
self, serviceName, functionName,
codeZipFile=None, codeDir=None, codeOSSBucket=None, codeOSSObject=None,
Expand Down Expand Up @@ -403,6 +408,7 @@ def update_function(
'timeout': 60, // in second
}
"""

method = 'PUT'
path = '/{0}/services/{1}/functions/{2}'.format(self.api_version, serviceName, functionName)
headers = self._build_common_headers(method, path, headers)
Expand Down
28 changes: 28 additions & 0 deletions fc2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import os
import zipfile

from inspect import signature
from functools import wraps


def zip_dir(inputDir, output):
"""
Expand Down Expand Up @@ -44,3 +47,28 @@ def _archive_dir(parentDirectory):
_archive_dir(inputDir)

zipOut.close()


def typeassert(*ty_args, **ty_kwargs):
def decorate(func):
# If in optimized mode, disable type checking
if not __debug__:
return func

# Map function argument names to supplied types
sig = signature(func)
bound_types = sig.bind_partial(*ty_args, **ty_kwargs).arguments

@wraps(func)
def wrapper(*args, **kwargs):
bound_values = sig.bind(*args, **kwargs)
# Enforce type assertions across supplied arguments
for name, value in bound_values.arguments.items():
if name in bound_types:
if not isinstance(value, bound_types[name]):
raise TypeError(
'Argument {} must be {}'.format(name, bound_types[name])
)
return func(*args, **kwargs)
return wrapper
return decorate
8 changes: 8 additions & 0 deletions test/function_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def test_update(self):
with self.assertRaises(fc2.FcError):
self.client.update_function(self.serviceName, functionName, description='invalid', headers ={'if-match':'invalid etag'})

with self.assertRaises(Exception):
self.client.update_function(self.serviceName, functionName, codeZipFile='test/hello_world/hello_world.zip', runtime = 10)

self.assertEqual(func['description'], desc)

self.client.delete_function(self.serviceName, functionName)
Expand Down Expand Up @@ -290,6 +293,11 @@ def test_check_op_function_param_check(self):
self.serviceName, functionName,
handler='main.my_handler', runtime='python2.7', codeOSSBucket='test-bucket' , description=desc)

with self.assertRaises(Exception):
function = self.client.create_function(
self.serviceName, functionName,
handler='main.my_handler', runtime=10, codeZipFile='test/hello_world/hello_world.zip')


if __name__ == '__main__':
unittest.main()

0 comments on commit 016f893

Please sign in to comment.