Skip to content

Commit 8389320

Browse files
Update the python tutorial for invoke endpoints
1 parent 2f7c09b commit 8389320

File tree

1 file changed

+92
-84
lines changed

1 file changed

+92
-84
lines changed

python/intro/README.md

+92-84
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In the terminal type the following:
2828

2929
![user input](images/userinput.png)
3030
>```sh
31-
> fn init --runtime python --trigger http pythonfn
31+
> fn init --runtime python pythonfn
3232
>```
3333
3434
The output will be
@@ -89,7 +89,7 @@ def handler(ctx, data: io.BytesIO=None):
8989
9090
return response.Response(
9191
ctx, response_data=json.dumps(
92-
{"message": "Hello {0}".format(name)}),
92+
{"message": "Hello {0}".format(name)}),
9393
headers={"Content-Type": "application/json"}
9494
)
9595
@@ -116,10 +116,6 @@ version: 0.0.1
116116
runtime: python
117117
entrypoint: /python/bin/fdk /function/func.py handler
118118
memory: 256
119-
triggers:
120-
- name: pythonfn
121-
type: http
122-
source: /pythonfn
123119
```
124120
125121
The generated `func.yaml` file contains metadata about your function and
@@ -132,11 +128,7 @@ declares a number of properties including:
132128
in `--runtime`.
133129
* memory--The max memory size for a function in megabytes.
134130
* entrypoint--the name of the executable to invoke when your function is called,
135-
in this case `python3 func.py`.
136-
* triggers--identifies the automatically generated trigger name and source. For
137-
example, this function would be executed from the URL
138-
<http://localhost:8080/t/appname/pythonfn>. Where appname is the name of
139-
the app chosen for your function when it is deployed.
131+
in this case `/python/bin/fdk /function/func.py handler`.
140132

141133
There are other user specifiable properties but these will suffice for
142134
this example. Note that the name of your function is taken from the containing
@@ -164,8 +156,8 @@ Make sure your context is set to default and you are using a demo user. Use the
164156
>```
165157
166158
```cs
167-
CURRENT NAME PROVIDER API URL REGISTRY
168-
* default default http://localhost:8080 fndemouser
159+
CURRENT NAME PROVIDER API URL REGISTRY
160+
* default default http://localhost:8080 fndemouser
169161
```
170162
171163
If your context is not configured, please see [the context installation instructions](https://github.com/fnproject/tutorials/blob/master/install/README.md#configure-your-context) before proceeding. Your context determines where your function is deployed.
@@ -176,7 +168,7 @@ Next, functions are grouped together into an application. The application acts a
176168
![user input](images/userinput.png)
177169
>```sh
178170
> fn create app pythonapp
179-
>```
171+
>```
180172
181173
A confirmation is returned:
182174
@@ -206,18 +198,18 @@ You should see something similar to:
206198
```yaml
207199
Deploying pythonfn to app: pythonapp
208200
Bumped to version 0.0.2
209-
Building image fndemouser/pythonfn:0.0.2
201+
Building image fndemouser/pythonfn:0.0.2
210202
FN_REGISTRY: fndemouser
211203
Current Context: default
212204
Sending build context to Docker daemon 5.632kB
213205
Step 1/12 : FROM fnproject/python:3.7.1-dev as build-stage
214206
3.7.1-dev: Pulling from fnproject/python
215-
a5a6f2f73cd8: Pull complete
216-
3a6fba040982: Pull complete
217-
738ebe0cf907: Pull complete
218-
a4b11c375c52: Pull complete
219-
02c57c00f1bc: Pull complete
220-
5dac448549aa: Pull complete
207+
a5a6f2f73cd8: Pull complete
208+
3a6fba040982: Pull complete
209+
738ebe0cf907: Pull complete
210+
a4b11c375c52: Pull complete
211+
02c57c00f1bc: Pull complete
212+
5dac448549aa: Pull complete
221213
Digest: sha256:80dd569dfc2a616b513bba52d0e03ae9db933c7aa6039687c13bf59c1ce47410
222214
Status: Downloaded newer image for fnproject/python:3.7.1-dev
223215
---> f1676f17ed78
@@ -271,11 +263,11 @@ Removing intermediate container 389e75d27ea5
271263
---> fe7635e860ca
272264
Step 7/12 : FROM fnproject/python:3.7.1
273265
3.7.1: Pulling from fnproject/python
274-
a5a6f2f73cd8: Already exists
275-
3a6fba040982: Already exists
276-
738ebe0cf907: Already exists
277-
a4b11c375c52: Already exists
278-
02c57c00f1bc: Already exists
266+
a5a6f2f73cd8: Already exists
267+
3a6fba040982: Already exists
268+
738ebe0cf907: Already exists
269+
a4b11c375c52: Already exists
270+
02c57c00f1bc: Already exists
279271
Digest: sha256:af0c785e711e34f8d0ba5a346e9a7900f6557d9cd96a0e7d0ea6e51adba6e797
280272
Status: Downloaded newer image for fnproject/python:3.7.1
281273
---> eda33421b45b
@@ -300,8 +292,6 @@ Successfully tagged fndemouser/pythonfn:0.0.2
300292
301293
Updating function pythonfn using image fndemouser/pythonfn:0.0.2...
302294
Successfully created function: pythonfn with fndemouser/pythonfn:0.0.2
303-
Successfully created trigger: pythonfn
304-
Trigger Endpoint: http://localhost:8080/t/pythonapp/pythonfn
305295
```
306296
307297
All the steps to load the current language Docker image are displayed.
@@ -318,53 +308,11 @@ image `fndemouser/pythonfn:0.0.2`.
318308

319309
Note that the containing folder name `pythonfn` was used as the name of the
320310
generated Docker container and used as the name of the function that container
321-
was bound to. By convention it is also used to create the trigger name
322-
`pythonfn`.
311+
was bound to.
323312

324313
Normally you deploy an application without the `--verbose` option. If you rerun the command a new image and version is created and loaded.
325314

326315

327-
## Invoke your Deployed Function
328-
329-
There are two ways to call your deployed function.
330-
331-
### Invoke with the CLI
332-
333-
The first is using the Fn CLI which makes invoking your function relatively
334-
easy. Type the following:
335-
336-
![user input](images/userinput.png)
337-
>```sh
338-
> fn invoke pythonapp pythonfn
339-
>```
340-
341-
which results in:
342-
343-
```js
344-
{"message":"Hello World"}
345-
```
346-
347-
When you invoked "pythonapp pythonfn" the fn server looked up the "pythonapp"
348-
application and then looked for the Docker container image bound to the
349-
"pythonfn" function and executed the code. Fn `invoke` invokes your function
350-
directly and independently of any associated triggers. You can always invoke a
351-
function even without it having any triggers bound to it.
352-
353-
You can also pass data to the invoke command. Note that you set the content type
354-
for the data passed. For example:
355-
356-
![user input](images/userinput.png)
357-
>```sh
358-
> echo -n '{"name":"Bob"}' | fn invoke pythonapp pythonfn --content-type application/json
359-
>```
360-
361-
```js
362-
{"message":"Hello Bob"}
363-
```
364-
365-
The JSON data was parsed and since `name` was set to "Bob", that value is passed
366-
in the output.
367-
368316
### Understand fn deploy
369317
If you have used Docker before the output of `fn --verbose deploy` should look
370318
familiar--it looks like the output you see when running `docker build`
@@ -400,7 +348,6 @@ fndemouser/pythonfn 0.0.2 cde014cefdad 7 minutes ago
400348
```
401349
402350
### Explore your Application
403-
404351
The fn CLI provides a couple of commands to let us see what we've deployed.
405352
`fn list apps` returns a list of all of the defined applications.
406353

@@ -417,32 +364,93 @@ NAME ID
417364
pythonapp 01D4BBS7BPNG8G00GZJ0000001
418365
```
419366
420-
We can also see the functions that are defined by an application. Since functions are exposed via triggers, the `fn list triggers <appname>` command is used. To list the functions included in "pythonapp" we can type:
367+
The fn list functions <app-name> command lists all the functions associated with and app.
421368

422369
![User Input Icon](images/userinput.png)
423370
>```sh
424-
> fn list triggers pythonapp
371+
> fn list functions pythonapp
425372
>```
426373
427374
```sh
428-
FUNCTION NAME ID TYPE SOURCE ENDPOINT
429-
pythonfn pythonfn 01D8VGFVBBNG8G00GZJ0000003 http /pythonfn http://localhost:8080/t/pythonapp/pythonfn
375+
NAME IMAGE ID
376+
pythonfn fndemouser/pythonfn:0.0.2 01DJRP8FT8NG8G00GZJ0000002
430377
```
431378
432-
The output confirms that pythonapp contains a `pythonfn` function which may be
433-
requested via the specified URL.
379+
## Invoke your Deployed Function
380+
There are two ways to call your deployed function.
434381

435-
### Invoke with Curl
382+
### Invoke with the CLI
383+
The first is using the Fn CLI which makes invoking your function relatively
384+
easy. Type the following:
385+
386+
![user input](images/userinput.png)
387+
>```sh
388+
> fn invoke pythonapp pythonfn
389+
>```
390+
391+
which results in:
392+
393+
```js
394+
{"message":"Hello World"}
395+
```
396+
397+
When you invoked "pythonapp pythonfn" the fn server looked up the "pythonapp"
398+
application and then looked for the Docker container image bound to the
399+
"pythonfn" function and executed the code. Fn `invoke` invokes your function
400+
directly and independently.
401+
402+
You can also pass data to the invoke command. Note that you set the content type
403+
for the data passed. For example:
404+
405+
![user input](images/userinput.png)
406+
>```sh
407+
> echo -n '{"name":"Bob"}' | fn invoke pythonapp pythonfn --content-type application/json
408+
>```
409+
410+
```js
411+
{"message":"Hello Bob"}
412+
```
413+
414+
The JSON data was parsed and since `name` was set to "Bob", that value is passed
415+
in the output.
436416

437-
The other way to invoke your function is via HTTP. The Fn server exposes our
438-
deployed function at `http://localhost:8080/t/pythonapp/pythonfn`, a URL
439-
that incorporates our application and function trigger as path elements.
440417

418+
### Getting a Function's Invoke Endpoint
419+
In addition to using the Fn invoke command, we can call a function by using a URL. To do this, we must get the function's invoke endpoint. Use the command `fn inspect function <appname> <function-name>`. To list the nodefn function's invoke endpoint we can type:
420+
421+
![user input](images/userinput.png)
422+
>```sh
423+
> fn inspect function pythonapp pythonfn
424+
>```
425+
426+
```js
427+
{
428+
"annotations": {
429+
"fnproject.io/fn/invokeEndpoint": "http://localhost:8080/invoke/01DJRP8FT8NG8G00GZJ0000002"
430+
},
431+
"app_id": "01DJRP674QNG8G00GZJ0000001",
432+
"created_at": "2019-08-20T23:37:12.776Z",
433+
"id": "01DJRP8FT8NG8G00GZJ0000002",
434+
"idle_timeout": 30,
435+
"image": "fndemouser/pythonfn:0.0.2",
436+
"memory": 256,
437+
"name": "pythonfn",
438+
"timeout": 30,
439+
"updated_at": "2019-08-20T23:42:47.297Z"
440+
}
441+
```
442+
443+
The output confirms that `nodefn` functions invoke endpoint is:
444+
`http://localhost:8080/invoke/01DJRP8FT8NG8G00GZJ0000002`. We can use this URL
445+
to call the function.
446+
447+
448+
### Invoke with Curl
441449
Use `curl` to invoke the function:
442450

443451
![user input](images/userinput.png)
444452
>```sh
445-
> curl -H "Content-Type: application/json" http://localhost:8080/t/pythonapp/pythonfn
453+
> curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01DJRP8FT8NG8G00GZJ0000002
446454
>```
447455
448456
The result is once again the same.
@@ -456,7 +464,7 @@ the function back.
456464

457465
![user input](images/userinput.png)
458466
>```
459-
> curl -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/t/pythonapp/pythonfn
467+
> curl -X "POST" -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/invoke/01DJRP8FT8NG8G00GZJ0000002
460468
>```
461469
462470
The result is once again the same.

0 commit comments

Comments
 (0)