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