Adding python modules to jython

The heart of the script is making GET requests with authentication. Is there a better way to do this?

    jsonTokenResponse = requests.get('https://data.tankutility.com/api/getToken', auth=HTTPBasicAuth(TankUser, TankPassword)).json()
    jsonDeviceResponse = requests.get('https://data.tankutility.com/api/devices?token=' + jsonTokenResponse["token"]).json()
    # below is querying the first tank in the account - adjust the [0] if you want other tanks
    jsonTankDataResponse = requests.get('https://data.tankutility.com/api/devices/' + jsonDeviceResponse["devices"][0] + '?token=' + jsonTokenResponse["token"]).json()

thanks

GET requests can be done through HTTP Actions. Work is progressing on a 2.x HTTP binding which will (hopefully) incorporate these Actions. There are also several Java libraries. If these are better is subjective… but they work. If you are having difficulty installing requests, you may find these easier to get going.

The Actions are okay, but the authentication is a bit painful with them as you have to do it yourself with encoding it into a header string (unless I missed something). I was looking forward to easily porting Python scripts as it makes this much easier. I’ve already got the script running externally as a Python script, so I can take some time to debug this.

If you have requests installed, you just need to make them accessible in the classpath (python.path) to be able to use it within Jython. Based on your post, you were installing to

/etc/openhab2/automation/lib/python

Is it in this directory? If not, you can add your Python2 module directory to the python.path or create a symlink to it.

I installed and using request, this my instruction to install:

sudo pip install --upgrade --target=/datavol/openhab-2.5.1/openhab_conf/automation/lib/python requests

/datavol/openhab-2.5.1/openhab_conf maps to the OH conf directory, which here is mapped to my docker container.

putting the module to automation/lib/python you do not need to set classpath

1 Like

That appears to almost work for me. The automation/lib/python directory does get populated with a requests directory that has the files in it. And the “import requests” line works. Progress.

But the next line fails:

from requests.auth import HTTPBasicAuth

Error during evaluation of script ‘file:/etc/openhab2/automation/jsr223/python/personal/tankUtil.py’: ImportError: No module named auth in at line number 8

There is an auth.py and auth.pyc in the requests directory. Any idea why that can’t be found?

thanks

EDIT: happened to reboot, and now it appeared to load. Not sure why that is needed, but it looks better. Let me test this a bit further. Thanks

EDIT #2: onward to the next problem. This does indeed load requests, but then it breaks exactly as seen here. That’s the new challenge. I was hoping that Jython would drop in for Python, but I guess not.

Try:

import requests
from requests.auth import HTTPBasicAuth

together

1 Like

That works - thank you. I originally didn’t read your post carefully enough, but it does indeed install. But I hit this problem now as seen in an another thread, so I’ll try to follow that. I don’t think it is related to importing - that’s fine with your pip install line.

I had the same problem. The problem is not that I cannot extend the system path or configure my PYTHONPATH in /etc/default/openhab. The problem is that the openhab user needs to access it and this is not the user I work with on my operating system. I am using Linux in a virtual machine. Maybe this even works with openhabian, I can’t guarantee that.

You can install the package as follows, for example:

sudo -u openhab pip install --user requests

As you can see:

root@OpenHABVM:/var/lib/openhab/.local/lib/python2.7/site-packages# ls -la
total 48
drwx------ 12 openhab openhab 4096 Jul  5 16:08 .
drwx------  3 openhab openhab 4096 Jul  5 16:08 ..
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 certifi
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 certifi-2021.10.8.dist-info
drwxrwxr-x  4 openhab openhab 4096 Jul  5 16:08 chardet
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 chardet-4.0.0.dist-info
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 idna
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 idna-2.10.dist-info
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 requests
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 requests-2.27.1.dist-info
drwxrwxr-x  5 openhab openhab 4096 Jul  5 16:08 urllib3
drwxrwxr-x  2 openhab openhab 4096 Jul  5 16:08 urllib3-1.26.16.dist-info

In /etc/default/openhab I have following line:

EXTRA_JAVA_OPTS="-Xms9830m -Xmx9830m -Dpython.path=/var/lib/openhab/.local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages"