#
# --------------
#
# fmPy is a set of functions and example code to leverage the FileMaker (Claris) Data API
#
# At present the function expect a parameter passed as "payloadData", a URLencoded JSON array with the following:
#  NOTE!! This has changes from fmPy 1.5 and below! We now pass 2 arrays within payloadData, "fmWhere" and "fmWhat"
#  This makes it a bit easier to understand
#  fmWhere is the serverName, databaseName, layoutName and action
#  fmWhat is the data for the relevant action
#
#  **** NOTE The old parameters still work, we just reassemble them. However this may change.
#
#   fmWhere:
#   action (see actions below)
#   serverName -> FQDN or IP of your server, however it needs to be. Ex: https://myserver.example.com
#   databaseName -> the solution name eg Inventory
#   layoutName -> Data API needs a layout.
#   OPTIONAL for GetRecord: keyField. If keyField is specified, the returned array will use the value of that field for each
#     record as the key, like this: {"keyFieldValue": {"FileName": "FileMaker18Mac.zip", "Description": "FileMaker 18 Mac Installer", "zf_Enabled": 1, "recordId": "1", "modId": "4"}}
#     OTHERWISE the array uses numbers from 0 for the key
#
#   fmWhat:
#   scriptName - required for runScript
#   beyond that you can include anything - generally speaking it's the fields you want to set or query or pass to the script on the FM side. This is passed right through.
# 
# 
#
# It has a number of actions "pre packaged" - these are:
#
# a. getRecord - returns a JSON array w/ fields and values for the given query - query passed in the 'data' array. You can specify 'keyField' in the fmWhere array and it will use this for the key for the results
#   returns a JSON array w/ results OR an error code
#
# b. createRecord - creates a record in the specified table and sets fields based on the 'data' array sent
#   returns "000 OK" if successful or the FileMaker error if not
#
# c. updateRecord - updates a record specified by a field:value pair with the data in 'data' - requires a primary key and value pair in fmWhat called "pk" and "uuid"
#   returns "000 OK" if successful or the FileMaker error if not
#
# c. runScript - runs a script with script name and parameters to be passed in 'data'
#
#
# d. saveFile - not strictly a FileMaker Data API "thing" but I found it useful - pass it a file as Base64 and it'll save to a path. Doesn't seem to like files > 3MB tho
# 
#
# Some non-data related functions to verify that things are working on the webserver:
# aa. version - returns the version specified in the "version" variable
# bb. test - returns some basic HTML text with the server name & OS
#
# This file needs a file called fmInfo.py from which it will get :
#   the credentials for the server
#   some custom functions
# All the user-specific stuff is in fmInfo.py so you hopefully shouldn't need to edit anything here
#
# The idea is to invoke this script on your web server by using Insert From URL.
# The result will be "OK" if successful or an error if not.
#
# See FMPython.fmp12 for example code
#
# It's always a good idea to make sure your Data API user (the one in fmInfo.py) has as few rights as possible
#
# This uses the python-fmrest package by David Hamann
#   https://github.com/davidhamann/python-fmrest
#
# The github repo for this is https://github.com/gibbsjoh/fmPy
#
########################