# farmOS.py v2 - async support

**URL:** <https://farmos.discourse.group/t/farmos-py-v2-async-support/2197>\
**Category:** Development\
**Created:** [January 8, 2025, 11:42pm UTC](https://farmos.discourse.group/t/farmos-py-v2-async-support/2197 "2025-01-08T23:42:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul121](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@paul121](https://farmos.discourse.group/u/paul121)\
**Post date:** [January 8, 2025, 11:42pm UTC](https://farmos.discourse.group/t/farmos-py-v2-async-support/2197/1 "2025-01-08T23:42:30Z")

</div>

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](https://github.com/farmOS/farmOS.py/pull/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](https://github.com/farmOS/farmOS.py/pull/67))

> - Remove support for farmOS v1
> - Update to pydantic v2
> - Use [HTTPX](https://www.python-httpx.org/) for requests library
> - Use [HTTPX-Auth](https://github.com/Colin-b/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:
> 
> - [Pluggable authentication support #63](https://github.com/farmOS/farmOS.py/issues/63) via the [`auth` parameter](https://www.python-httpx.org/advanced/authentication/)
> - Most of [Pluggable transport backend #64](https://github.com/farmOS/farmOS.py/issues/64) via HTTPX [async support](https://www.python-httpx.org/async/)

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.

```python
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)

```

---

<div class="post-metadata">

**Author:** ![paul121](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@paul121](https://farmos.discourse.group/u/paul121)\
**Post date:** [January 8, 2025, 11:45pm UTC](https://farmos.discourse.group/t/farmos-py-v2-async-support/2197/2 "2025-01-08T23:45:49Z")

</div>

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:

```auto
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](https://gist.github.com/paul121/4ba3ae9005a93e1f50410adc756f9b28)
