From 621c9c3d86c87da0625061f57d88936f7a4f7d56 Mon Sep 17 00:00:00 2001 From: JP Cassil <94997997+jpcassil-6rs@users.noreply.github.com> Date: Thu, 2 Jun 2022 16:42:03 -0400 Subject: [PATCH] Add example monday python google cloud function --- monday/google_cloud/README | 9 ++++ monday/google_cloud/lambda_function.py | 58 ++++++++++++++++++++++++++ monday/google_cloud/requirements.txt | 3 ++ 3 files changed, 70 insertions(+) create mode 100644 monday/google_cloud/README create mode 100644 monday/google_cloud/lambda_function.py create mode 100644 monday/google_cloud/requirements.txt diff --git a/monday/google_cloud/README b/monday/google_cloud/README new file mode 100644 index 0000000..85c2645 --- /dev/null +++ b/monday/google_cloud/README @@ -0,0 +1,9 @@ +This lambda_function is a simple / minimal example of how to integrate with the monday.com graphQL API to get back a table of boards. + +It can be used by following the fivetran setup guide for google cloud functions. Don't forget to add the accompanying requirements.txt and select a Python 3.x runtime. Python 3.8 was used to create this function. + +This is the structure of the data that accompanies this function in the fivetran connections "secrets" field: +{ + "apiKey": "your monday.com api key here", + "limit": 1000 +} \ No newline at end of file diff --git a/monday/google_cloud/lambda_function.py b/monday/google_cloud/lambda_function.py new file mode 100644 index 0000000..006caba --- /dev/null +++ b/monday/google_cloud/lambda_function.py @@ -0,0 +1,58 @@ +import requests +from datetime import datetime,timezone + +def get_request(query, apiKey, apiUrl): + headers = {"Authorization" : apiKey} + response = requests.post(url=apiUrl, json={'query' : query}, headers=headers) + if response.status_code == 200: + response = response.json() + return response["data"] + else: + return {} + +def build_response(data, limit, cursor_value): + response = dict() + + response['insert'] = {"boards": []} # If we had more endpoints we could add them here + # and we would need to add them to the 'insert' section of the response below + for row in data: + response['insert']["boards"].append(row) + + response['state'] = {'cursor': cursor_value} if len(data) == 0 else {'cursor': datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")} + response['schema'] = {"boards": {'primary_key': ["id"]}} # If we had more endpoints, we would need to add the schemas / primary keys here as well + response['hasMore'] = False if len(data) < limit else True + return response + +def lambda_handler(request): + # Get json from request + request = request.get_json() + # Note that get_json above is a flask function, which seems to work because google cloud functions are run + # in a flask based environment, even though we didn't import it explicitly or add it to requirements.txt + + # Import credentials + # These and other parameters should be wrapped up in 'request,' which is relayed from the connector's 'secrets' + apiKey = request['secrets']['apiKey'] + + # Set state + try: + cursor_value = request['state']['cursor'] + except KeyError: + cursor_value = '1970-01-01T00:00:00' + + # Set the 'limit' according to your estimates of the table's size and row count + # Again, these can also be stored in 'request' + limit = request['secrets']['limit'] + + # Get data + apiUrl = "https://api.monday.com/v2" + boards_query = '{ boards {id name state board_folder_id } }' + boards_data = get_request(boards_query, apiKey=apiKey, apiUrl=apiUrl) + + if len(boards_data) == 0: + return {} + + response = build_response(boards_data["boards"], limit, cursor_value) + + print(response) + + return response diff --git a/monday/google_cloud/requirements.txt b/monday/google_cloud/requirements.txt new file mode 100644 index 0000000..b32e87d --- /dev/null +++ b/monday/google_cloud/requirements.txt @@ -0,0 +1,3 @@ +# Function dependencies, for example: +datetime >= 3.0.0 +requests >= 2.0.0 \ No newline at end of file