farmOS.py v2 - async support

Hi all - opening this topic to request some feedback on some changes + improvements to the farmOS.py library.

We’ve had this on our list for some time now. I actually opened a PR #67 with most of the implementation many months ago. I’ve just dusted this off today, made sure tests are still passing (yay!) and also updated documentation (including async docs - yay!).

There still are a few small things to do, but I think this is ready for a proper review now!

An overview of the proposed changes: (quoted from the PR #67)

  • Remove support for farmOS v1
  • Update to pydantic v2
  • Use HTTPX for requests library
  • Use HTTPX-Auth for OAuth2
  • Expose async functions
  • Auto-generate sync code from async code (and async tests!)

Using the HTTPX library gives us a few things:

What is really great about these changes is that it should have very minimal impact on existing Python scripts using farmOS.py. All you need to change is how you instantiate the client and configure authentication. The resource helper methods all behave the same as before, largely thanks to how the HTTPX library’s request methods are nearly identical to Python Requests.

from httpx_auth import OAuth2ResourceOwnerPasswordCredentials
from farmOS import FarmClient

FARMOS_HOSTNAME="https://myfarm.farmos.net"
auth = OAuth2ResourceOwnerPasswordCredentials(
    token_url=f"{FARMOS_HOSTNAME}/oauth/token",
    username=USERNAME,
    password=PASSWORD,
    client_id="farm",
    scope="farm_manager",
)
farm_client = FarmClient(hostname=FARMOS_HOSTNAME, auth=auth)
3 Likes

If anyone is willing to review the code/PR changes or even just test these changes in their existing scripts that would be very helpful. cc @Symbioquine @Farmer-Ed @Fosten @mattman0123 !

You can install this dev version of farmOS.py from my personal branch using pip:

pip install git+https://github.com/paul121/farmOS.py.git@1.x-2.x-async

We’ve actually been using these changes for async support in a custom script for Rothamsted and async is proving very useful! (10x improvement when making 100s of requests) farmOS.py async Rothamsted example · GitHub

1 Like