Omnis Studio and Python

Lou Picciano loupicciano at comcast.net
Tue Dec 23 08:14:11 EST 2014


Greg, 

Far from suggesting, I'm pounding on a drum! The drumbeat being: 'I'm happy to see more ODs using Python'. (There are smaller tom-toms pounding out messages about node.js, JavaScript and HTML5, but... Later.) 

First: Serban's point about avoiding variable declarations like 'request': Also a good idea in terms of Best Practices. Note that many code snippet examples use 'r' for the request object, as if to point this up. It's a great feature of Python, btw, that its variable declarations are so simple... 

A successful 'requests' (POST or GET?) returns a python 'object' - in this case, 'r': 

>>> r = requests.post(url=url, data=payload) 

--- How do I trap the "data stuff" in Omnis? 

The 'data stuff' is contained in - more accurately, 'addressed by' - that response object 'r'. That python object is then 'acted upon' by various methods in requests: 

>>> r.text # gets you a dump of the whole result 
>>> gregsVariable = r.text # probably more useful 
>>> gregsVariable = r.json() # you even get json for free - need it? 

You can then pass that variable around as you wish. 

However, rather than consuming even more memory, returning a variable, then trying to manage that whole blob (where, in Omnis?), why not use some of the many tools python offers you to do this(1). Check out the XML and etree stuff in python. Why try to do any of this in Omnis, when the python modules offer you direct access to libxml2, libxslt, many others... 

Even better, treat the whole object as a stream, and manage timeouts at the same time: 



>>> # this is better. Managing connection as a 'stream' is a good idea... 
>>> r = requests.post(url=url, data=payload, auth=auth, stream=True, timeout=5) 

What to send in a given request? If you already know the fully-formed URL you wish to send, you'll be able to synthesize it pretty easily using requests. If you don't, view a page's source to reverse engineer what it expects to hear from you... 

--- What would be the purpose of "auth=(USERNAME,PASSORD)" since I only want to run method in the library? 
(there is no database connection needed, just run test method in the instantiated object class) 

Nothing to do with database access; this relates to authentication against the (mini) webserver the Omnis Lib/page is presenting to you. (Now, of course, if your http(s) auth were architected within a greater context of deeper access to other resources within the system - such as, though not exclusive to, the database; well, even your authentication may interact with a database - well, that's yet another story!) 

As long as we've brought up databases (don't know that one is in your mix here): Have you looked at PostgreSQL's excellent XML Xpath functionality? 

I had originally mentioned BasicAuth - one of many auth mechanisms requests supports. If you don't need auth right now, it's likely you will at some point. And, built-in, requests supports BasicAuth, SSL, certs, OAuth; a lot! 

This simple auth saved me recently. Note it's not quite the same as sending user/pass 'straight up' in the URL: 

>>> from requests.auth import HTTPBasicAuth 
>>> auth = HTTPBasicAuth(uname, pw) 
>>> r = requests.post(url=url, data=payload, auth=auth) 

* Snippets above from a block of code we're working on; nothing to do with Omnis. 

Regards, and Happy Christmas Season to All, 

Lou Picciano 


(1) Serban's point about 'there's a library for everything' should be spray painted on the office wall of every OD... 
(2) Requests is great stuff. One guy plans to tattoo the source code onto his body. Seems excessive. 

----- Original Message -----

From: "Grzegorz (Greg) Pasternak" <grzegorz at niagara.com> 
To: "OmnisDev List - English" <omnisdev-en at lists.omnis-dev.com> 
Sent: Monday, December 22, 2014 8:39:40 PM 
Subject: Re: Omnis Studio and Python 

Lou; 

> Serban's point on data=[array] first, plus requisite authentication (Basic Auth?), is spot on. 

What are you suggesting? 

I have posted in my previous email the script that works. However,… 
- assuming that I would like to keep in script this line: 

resp = requests.post(url=URL,data=request, auth=(USERNAME,PASSORD)) 

How do I trap the "data stuff" in Omnis? 
Let's assume that "request" is simply XML formatted string. 

What would be the purpose of "auth=(USERNAME,PASSORD)" since I only want to run method in the library? 
(there is no database connection needed, just run test method in the instantiated object class) 

Can you elaborate on this? 
I am sure this may be helpful for not just for me. 

Thanks, 
Greg 


On Dec 22, 2014, at 7:15 PM, Lou Picciano <loupicciano at comcast.net> wrote: 

> Serban, Derek, Greg et al, 
> 
> Funny you guys happen to be on this, today. 
> 
> Couple of thoughts: 
> 1) +++ on Serban's point: requests is the New Way Forward here. Use it. httplib likely the also-ran at this point... 
> 2) Serban, who are you - and why are you not at EurOmnis? 
> 3) Derek, glad to have you on this Train of Thought. 
> 
> Serban's point on data=[array] first, plus requisite authentication (Basic Auth?), is spot on. 
> 
> Lou Picciano* 
> 
> *(hesitant to call himself 'experienced' in this, has spent the last few days trying to get the whole lxml/etree/(etc) foodchain built - for use by one of the Omnis Luminaries - with static dependencies, of course, on multicore UNIX, using the Solaris linker. Head spinning.) 
> 
> ----- Original Message ----- 
> 
> From: serban21 at gmail.com 
> To: "OmnisDev List - English" <omnisdev-en at lists.omnis-dev.com> 
> Sent: Monday, December 22, 2014 1:05:41 PM 
> Subject: Re: Omnis Studio and Python 
> 
> Hi 
> 
> I'm working as a Python developer now, so I should be able to help... 
> 
> The additional data should be passed in the request variable (in 
> data=request). That is supposed to be a dictionary, and it should be 
> populated before that call. Do you have that code? 
> 
> See 
> http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests 
> . 
> 
> Derek example is ok, but it's using another library than yours (httplib vs 
> requests). For this purpose, requests is much better. 
> 
> 
> Regards, 
> Serban Teodorescu 
> 
> 
> 2014-12-22 19:53 GMT+02:00 Derek Delpero <derek.delpero at gmail.com>: 
> 
>> Hi, 
>> 
>> Here's an example using a get request: 
>> 
>> import httplib 
>> conn = httplib.HTTPConnection("172.16.25.133:5921") 
>> 
>> conn.request("GET","/Ultra?OmnisLibrary=TestLibrary&OmnisClass=TestRemoteTask&test_parameter=1") 
>> res = conn.getresponse() 
>> data = res.read() 
>> conn.close() 
>> 
>> Hope that helps, 
>> 
>> Derek 
>> 
>> 
>> On Mon, Dec 22, 2014 at 10:54 AM, Grzegorz (Greg) Pasternak < 
>> grzegorz at niagara.com> wrote: 
>>> 
>>> Hello; 
>>> I am looking for some help re: Omnis Studio 4.x and up with Python. 
>>> Specifically I would like to run Python script against Omnis library 
>> using 
>>> HTTP ultra approach. 
>>> In archives I found reference to 
>>> Integrating Python With Omnis 
>>> http://omniscentral.com/integrating-python-with-omnis 
>>> but I can't seem to get more information. 
>>> I think this is the line in Python script that needs additional info 
>> about 
>>> Omnis library and Remote task class name: 
>>> 
>>> resp = requests.post(url=URL,data=request, auth=(USERNAME,PASSORD)) 
>>> 
>>> Is anybody out there who has some experience with this and can help? 
>>> Please let me know, 
>>> Regards; 
>>> Greg 
>>> 
>>> _____________________________________________________________ 
>>> Manage your list subscriptions at http://lists.omnis-dev.com 
>>> 
>> _____________________________________________________________ 
>> Manage your list subscriptions at http://lists.omnis-dev.com 
>> 
> _____________________________________________________________ 
> Manage your list subscriptions at http://lists.omnis-dev.com 
> 
> _____________________________________________________________ 
> Manage your list subscriptions at http://lists.omnis-dev.com 

_____________________________________________________________ 
Manage your list subscriptions at http://lists.omnis-dev.com 




More information about the omnisdev-en mailing list