{"id":220,"date":"2021-03-11T22:00:13","date_gmt":"2021-03-11T22:00:13","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=220"},"modified":"2022-04-11T20:24:24","modified_gmt":"2022-04-11T20:24:24","slug":"100-days-of-learning-day-3-calling-another-api-from-our-openfaas-function","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/100-days-challenge\/100-days-of-learning-day-3-calling-another-api-from-our-openfaas-function\/","title":{"rendered":"100 Days of Learning: Day 3 \u2013 Calling another API from our OpenFaaS function"},"content":{"rendered":"\n
Photo by NASA<\/a> on Unsplash<\/a><\/p>\n\n\n\n Here is my Log book<\/a><\/p>\n Building on from the previous days. What might not have been obvious at first is that you can invoke the function by talking directly to the API end point and not needing to use I am going to see if I can modify the helloworld function to take JSON as input and prettify it.<\/p>\n But first I will test out the code in the Python REPL<\/p>\n Ok that worked so next I need to modify the Rebuild and redeploy with Verify by throwing some JSON at it<\/p>\n Nice one! I also checked Docker Hub and indeed there is only 1 image that has been updated.<\/p>\n For this I am going to be following the example from Serverless for Everyone Else<\/a> that uses Node.js.<\/p>\n Which API to call? In the book Alex uses http:\/\/open-notify.org<\/a> to get the number of Astronauts currently in space. I will be using their API that gives the geolocation of where the International Space Station is.<\/p>\n See "International Space Station Current Location"<\/a> for more information on the API as well as a Python example.<\/p>\n <\/p>\n Create a new function from the node12 template<\/p>\n Whoops! I forgot to first create a directory in which to create the files, so now the files are mixed with my other function.<\/p>\nInvoking the function directly<\/h2>\n
faas-cli invoke<\/code><\/p>\n
$ curl --data-binary '{"key":"value"}' --header "Content-Type: application\/json" http:\/\/192.168.64.4:8080\/function\/helloworld\n{"key":"value"}\n<\/code><\/pre>\n
Warm up for tonight – JSON prettifier<\/h2>\n
$ python\n>>> import json\n>>> req = '{"z": 1, "a": 2, "c": 3, "b": 4}'\n>>> data = json.loads(req)\n>>> response = json.dumps(data, indent=4, sort_keys=True)\n>>> response\n'{\\n "a": 2, \\n "b": 4, \\n "c": 3, \\n "z": 1\\n}'\n<\/code><\/pre>\n
[handler.py](http:\/\/handler.py)<\/code> file. Disclaimer: I am not worried about error handling or any of the production coding here.<\/p>\n
import json\n\ndef handle(req):\n data = json.loads(req)\n response = json.dumps(data, indent=4, sort_keys=True)\n return response\n<\/code><\/pre>\n
faas-cli<\/code><\/p>\n
$ faas-cli up -f helloworld.yml\n...\nDeployed. 200 OK.\nURL: http:\/\/192.168.64.4:8080\/function\/helloworld.openfaas-fn\n<\/code><\/pre>\n
$ curl --data-binary '{"z": 1, "a": 2, "c": 3, "b": 4}' --header "Content-Type: application\/json" http:\/\/192.168.64.4:8080\/function\/helloworld\n{\n "a": 2,\n "b": 4,\n "c": 3,\n "z": 1\n}\n<\/code><\/pre>\n
Lets write a function that calls a 3rd party API<\/h2>\n
International Space Station Location<\/h3>\n
\u256d ~\/Learning\/faasd [20:23:45]\n\u2570 $ faas-cli new --lang node12 iss-location\n<\/code><\/pre>\n
$ mkdir temp\n$ mv iss* temp\n$ mv template temp\n$ mv temp iss-location\n$ ls\nhelloworld\/ iss-location\/ password.txt\n$ cd iss-location\n<\/code><\/pre>\n