FileMaker and Python Micro-Services

Note to readers:  As a followup to the FileMaker and Java Micro-Services post from last Friday, today's post from FMP Dude shows how to use Python and Flask to create micro-services.  This post also appeared  first on the FileMaker community website, and is reposted here with his permission.  So, if Java isn't for you, the same micro-services are available using Python.  Enjoy!

The Simplest Micro-Service!  (Python and Flask)

If you want to start using micro-services in FMP, consider Python and the super simple framework: Flask. If you're unsure what benefits micro-services give you, check out this list below:

Key benefits of micro-services include:

  • Uses industry standard protocols (HTTP verbs!).
  • Enables you to tap into powerful modern languages.
  • Easy to Learn.
  • No need to "re-invent the wheel" in a proprietary FMP-only CF to duplicate existing tested/free library routines.
  • Cross platform so the service can reside ... anywhere (Linux, Mac, Win). Local/WAN, Server, you name it.
  • Allows any application that can do a GET to use the service (Terminal, FMP, Firefox, Opera, Chrome, ....)
  • Is FREE. No expensive FMP-ONLY plug-in(s) needed. Install anywhere or everywhere. 1 user or 10,000 users...FREE.
  • NO FMP plug-in installation and configuration for every copy of FMP you might have.
  • NO restrictive plug-in license agreements.
  • Extremely powerful (get the full power of free third-party libraries often written in fast C language)
  • Puts you in charge of the service code. Not a third party.
  • Service Code Runs .... FAST!
  • Externalize loops to keep loops out of slow FMP scripts.
  • Service programming tools manage dependencies --- automatically!
  • Offers high abstraction. The caller of the micro-service doesn't need to know any details how the micro-service code works. Parameters can almost always. be passed simply letting the micro-service handle translations or other messy details.

(Note: Most modern programming languages, including Java snd Python, support micro-services.)

------------

Below is about the simplest micro-service you can write.

NOTE: this service example below isn't intended to be "production ready"-- as you'd certainly have much more error handling and other code. Instead, the example code below is just a "see how simple it is to get started and see all you can do..." kind of thing.

(You will need to have Python installed, of course and you will need to install Flask. Check out "pip" and other simple installers for python add-ons like Flask.)

Python micro-service starting point code below:

from flask import Flask, request

import hashlib

app = Flask(__name__)

@app.route('/')

def hello_world():

   return 'Hello World!'

if __name__ == '__main__':

  app.run()

If you save the code above into a file called, for example: "simple.py", and then run it from the command line, you have a Python (starting point) web service you can call from FMP!

Running this code gives you this output (you can change the port if you need to:

$ python3 simple.py

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

(the service is up!)

Let's give this micro-service a workout:

From Terminal: $ curl localhost:5000

Output: >>> Hello World!

----

From POSTMAN:

 

From the IDE built-in REST Client:

The browser works fine, too.

 

From FileMaker:

 

INSERT FROM URL only needs one line since we're not sending data: "http://localhost:5000"

 

 

And, in the IDE where the service is currently running (can also start from command line and other options), we see our call to the web service from FMP:

 

--------

Let's add a method to do a hash.

 

Just create a simple method in your web service code and run it again:

 

@app.route('/HashMe', methods=['GET'])

def hashMe():

stringToHash = request.args.get("password")

m = hashlib.sha3_512()

m.update(str.encode(stringToHash))

m.update(b" Some Salt here for more security!")

return m.hexdigest()

----

Note: if we had also imported the pyperclip library, we could also, with one line of code, copy our hash to the clipboard:

pyperclip.copy(m.hexdigest())

----

From POSTMAN:

So, in just a few lines of code, we added a hash method we can call from any application that can do a GET!

Conclusion:

Using, in this case, Python, and the super simple framework Flask, in just a few lines of code you can have a working web micro-service.

My advice: Start easy. Start small. Build from there.





Education is not preparation for life; education is life itself.

John Dewey

Thanks! You've already liked this
Need FileMaker Development Help? Or to purchase FileMaker Software?
Contact FM Pro Gurus for help