Solved

Tutorials

Comments: 2 - Last Comment by knelson on May 12, 2017   Comment Filter:
Posted on May 12, 2017
 
 

 

I have built some open source WSDL and RESTful APIs ​that I am testing out to extend the fuctionality of EdgeFrontier. Anyone interested in a write up on how to do it?

What I have tried so far, and would be willing to do a write up on:

WSDL(SOAP) with Ubuntu, and Python.

Parsing NIST file headers and returning the results in JSON format with a RESTful interface.

Writing Python code inside of EdgeFrontier and using another server to process the code and return the results in JSON format.

 

 

 

 

 

Point your RSS reader here for a feed of the latest messages in this topic
RE: Tutorials
Posted on May 12, 2017 in response to: bwhitezel
 
 
(5.0)
   
 

Great glad to see some interest. I am new to EF and this forum so hopefully I don't defy conventions. If anyone has any suggestions or questions about any content, how I am formating things please feel free to ask or correct where I am going wrong.

Background:

So this notion came from sitting through EF training. The module where you built an EF system to calculate Fahrenheit from Celsius, pointed out to me that EF scripting could use some help. I am lazy and building all of the objects necessary just to convert those values was bothering me. Simply put EF by itself had an issue with easily calculating (9/5) * x + 32. It took 4 or 5 steps.

So what are the options?

1. Write that into your EF system 5 steps. Ok if you force me to. 2. Write a C# dll and expose those methods. This will work, I suspect it is what most everyone is doing in instances like this. I think this is brilliant. However; I was looking for something that I could prototype in faster, and wanted to keep the source files open to modify by anyone. Not just those with Visual Studio (software and understanding), and C# programming skills. Plus who likes compiling and copying DLLs every time you want to test a change? 3 See if someone else built something I can use, (dll or WSDL page?)  Not a sure thing. 4. See if I can make a scripting language available to EF? This is what I am exploring.  

I like to code in Python (started learning last November) and since it is: Easy to learn, will run on almost any system (Windows, Mac, Linux, ChromeOS, etc...), and is easy to extend. This is where I turned to, I figured this could help out the whole EF community. I hope that there is a thriving one.

Solution:

I am going to build up to what I have been working on most recently since there are applications that can benefit from the intermediate steps.  For this step we will use a prebuilt library in Python for formatting SOAP and use and EF webmethod to get the answer. So if you are interested in building SOAP interfaces for other reasons this may be your lucky day.

First for those without Python knowledge. I will try to make this where you don't need it (or much of it), but I would highly suggest you learn some. You won't regret it.

Let’s look at the python code to have a function that converts Fahrenheit from Celsius.

   def CelsiusToFahrenheit(Celsius):

       Fahrenheit = 9.0/5.0 * Celsius + 32

       return float(Fahrenheit)

This should be fairly straight forward. I am trying to keep this from being a Python tutorial, so I won’t explain too much here. Takes Celsius and returns Fahrenheit as a float. The inverse function below. Why not be able to convert both ways?

   def FahrenheitToCelsius(Fahrenheit):

       Celsius = (Fahrenheit - 32) * 5.0/9.0

       return Celsius

I left the explicit conversion to a float in the return statement off because I was playing around with what will happen when the response makes it back to EF. As you will see later, I haven’t figured out all of the implications but would love to discuss them with you guys.

Alright. I built my system on a VM, running Ubuntu. Though for this tutorial I tested it out and works just fine with Python 3.6 running on Windows 10. This solution is NOT ready for production, but for testing out things and seeing if you want to scale it up you can’t beat how fast you will be up in running.  In 5 parts:

First Install Python on something. I would not suggest your EF server, I personally am trying to keep the server clean, which is why I am using web methods and api for this stuff. Plus if you like this you can scale up easy.

Second create a virtual environment in python and install some modules. Useful in our case if stuff goes bad. We can just delete the Python venv and try again.

Third create file with Python code in it.

Fourth test our WSDL service

Last test the WSDL service from EF.

Ok... 

First:

You may be able to skip the first step if you are not running windows, you probably have Python installed already.

Windows

https://www.python.org/downloads/ I would suggest you install for all users, add it to the environment (look for check box),  and name the directory you install into based on what version of python you are installing. You can run multiple versions side by side and this will make it easier later on if you decide to rev versions. In my case (C:\Python36) is for the 64bit version and (C:\Python36-32) for the 32bit version. Oracle database access is easier to set up on the 32bit version of Python for me.

Second:

Open a command prompt.

Make a directory where you are going to keep your source files.

c:\Users\Ken>mkdir c:\python_projects

c:\Users\Ken>cd c:\python_projects

Create a Python virtual environment called wsdltest (name doesn’t matter)

c:\python_projects>c:\Python36\python.exe -m venv wsdltest

Install the spyne module into that virtual environment using pip. (Pip will automatically download any files it needs) linux/mac users substitute the Scripts directory for bin so: ( python_projects>wsdltest\bin\pip install spyne ) for the first step etc....

 

c:\python_projects>wsdltest\Scripts\pip install spyne

Collecting spyne

 Downloading spyne-2.12.14.tar.gz (435kB)

   100% |████████████████████████████████| 440kB 1.1MB/s

Collecting pytz (from spyne)

 Downloading pytz-2017.2-py2.py3-none-any.whl (484kB)

   100% |████████████████████████████████| 491kB 1.2MB/s

Installing collected packages: pytz, spyne

 Running setup.py install for spyne ... done

Successfully installed pytz-2017.2 spyne-2.12.14

c:\python_projects>

Install lxml

c:\python_projects>wsdltest\Scripts\pip install lxml

Collecting lxml

 Using cached lxml-3.7.3-cp36-cp36m-win_amd64.whl

Installing collected packages: lxml

Successfully installed lxml-3.7.3

c:\python_projects>

Ok what did we just do?

The virtual environment bit makes a copy of Python in a directory named after the venv in this case it is wsdltest. Since it is a copy it is easily removed if something happens, and venvs are very useful making sure you have the right versions of libraries in use by your code.

pip install spyne

This installed the spyne module. This is what is doing the heaving lifting for you. These guys have written a python based RPC that handles all of the SOAP concerns for you. Check it out at: http://spyne.io/#inprot=HttpRpc&outprot=JsonDocument&s=rpc&tpt=WsgiApplication&validator=true. More that just SOAP is possible with this library. Json, Yaml, Httprpc, SOAP, to name a few.  In our case we will be using SOAP.

pip install lxml

This installed a XML handling module. The way we are using spyne will make use of this library.

Third:

Make a file called tempservice.py and put it in the c:\python_scripts directory.

Put this code into it and save it:

 

from spyne import Application, srpc, ServiceBase, Decimal, Float

from spyne.protocol.soap import Soap11

from spyne.server.wsgi import WsgiApplication

 

class TempService(ServiceBase):

   @srpc(Float, _returns=Float, _body_style='bare')

   def CelsiusToFahrenheit(Celsius):

       Fahrenheit = 9.0/5.0 * Celsius + 32

       return float(Fahrenheit)

   @srpc(Decimal, _returns=Decimal, _body_style='bare')

   def FahrenheitToCelsius(Fahrenheit):

       Celsius = (Fahrenheit - 32) * 5.0/9.0

       return Celsius

 

application = Application([TempService], 'temp.soap',

                         in_protocol=Soap11(validator='lxml'),

                         out_protocol=Soap11())

wsgi_application = WsgiApplication(application)

 

if __name__ == '__main__':

   import logging

   from wsgiref.simple_server import make_server

   logging.basicConfig(level=logging.DEBUG)

   logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

   logging.info("listening to http://localhost:8000")

   logging.info("wsdl is at: http://localhost:8000/?wsdl")

   server = make_server('localhost', 8000, wsgi_application)

   server.serve_forever()

Fourth:

Run the code using our venv (virtual environment).

c:\python_projects>wsdltest\Scripts\python.exe tempservice.py

INFO:root:listening to http://localhost:8000

INFO:root:wsdl is at: http://localhost:8000/?wsdl

 

Now open a web browser and go to http://localhost:8000/?wsdl

You should get an XML document with 2 methods on it. (Abbreviated below:)

<wsdl:definitions xmlns:plink="http://schemas.xmlsoap.org/ws/2003/05/partner-link/"

...

<wsdl:types>

<xs:schema targetNamespace="temp.soap" elementFormDefault="qualified">

<xs:element name="CelsiusToFahrenheit" type="xs:float"/>

<xs:element name="CelsiusToFahrenheitResponse" type="xs:float"/>

<xs:element name="FahrenheitToCelsius" type="xs:decimal"/>

<xs:element name="FahrenheitToCelsiusResponse" type="xs:decimal"/>

</xs:schema>

</wsdl:types>

<wsdl:message name="CelsiusToFahrenheit">

Lastly:

Test it out in EF.

Change all of the “localhosts” in your tempservice.py to the ip address of where your code is running. The important one is the server = make_server(‘localhost’...) line. This will make the simple wsgi server from spyne that python is using answer other hosts.

Now Create an EF system, add a webmethod change the url to point to our WSDL service, click on “Generate Methods”

At this point you should see the methods exposed by our microservice.

Set up some parameters and test it out.

Next steps:

Use a real web server like apache or NGIX. This would be for production. The spyne wsgi server is good for testing would not recommend running in production. 

Examing type errors. If you noticed I was returning a float from the python code, EF read it as a single. Because of this I am curious if I can try to pass a double (from EF) to this web method and how it is treated. I suspect that EF has helper DLLs' that are handling the web request. If those dlls are strongly typed, it may the case that there is truncation between EF and its' helper. Spyne supports Decimal, and Double types so this concern may be unique to Floats vs Singles or Doubles.  I will need to do some testing to prove this out.  It is counter intuitive that a Python float doesn’t match a .Net float. Languages like C#, C++, C, and Java use float(or Single) for single precision numbers. Python is the odd one here, float is any number with a floating point. So really be aware when you create your functions and decorators in Python to account for other systems interpretations of those values. To avoid all of this concern I would suggest using double in your decorators when dealing with any floating point numbers you plan on exposing with in these SOAP interfaces.

Ok thats it for now. Next tutorial will be on building RESTful interfaces for use with EF faster and more modern. SOAP is nice but very heavy in terms of response. Look at the XML to generate the web methods. Also look at the return XML. In this example we are getting scalar data. A single (float/single). SOAP is sending a lot of data back just for this. Hope that this was helpful for someone. 

EF_SOAP.PNG

Add a Comment

Please sign in on the right menu to comment.  If you do not have an account, please Join now.