-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy path__main__.py
66 lines (62 loc) · 2.33 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# ======================================================= #
# IBM Cloud Code Engine - Functions-as-a-Service Sample #
# #
# __main.py__ (Python sample function) #
# #
# This sample code uses an external module "lorem" #
# to generate an arbitrary result message. IBM code #
# engine functions code with references to external #
# modules have to be deployed as code-bundles. #
#
# This sample shows how to access URL parameters in a #
# function. #
# #
# This sample shows how an external reference is coded #
# in the source file (__main__.py) and how the modul #
# is referenced in the requirements.txt. #
# #
# ======================================================= #
##
# import the referenced "lorem" module
from lorem_text import lorem
##
# The `main` function is the entry-point into the function.
#
# A function can define multiple functions, but it needs to
# have one dedicated 'main' function, which will be called
# by the runtime.
#
# The 'main' function has one optional argument, which
# carries all the parameters the function was invoked with.
#
# Those arguments are dynamic and can change between
# function invocations.
#
# Additionally, a function has access to some
# predefined and also user-defined environment variables.
#
def main(params):
words = 10
return {
# specify headers for the HTTP response
# we only set the Content-Type in this case, to
# ensure the text is properly displayed in the browser
"headers": {
"Content-Type": "text/plain;charset=utf-8",
},
## use the text generator to create a response sentence
# with 10 words
"body": lorem.words(words),
}
# Optional:
# If you used a function name different from 'main', make
# the function known under the 'main' symbol to the
# runtime, so it can be invoked.
#
# Example:
#
# def my_main_func_with_another_name(params) {
# ...
# }
# ...
# main = my_main_func_with_another_name