farmOS / farmOS.py

A Python library for interacting with farmOS over API.
GNU General Public License v3.0
27 stars 12 forks source link

2.x changes #67

Open paul121 opened 7 months ago

paul121 commented 7 months ago

This PR includes proposed (breaking) changes for a 2.x version of farmOS.py. Likely will not merge these changes here but into a new 2.x or main branch.

An overview:

Using the HTTPX library gives us a few things:

This library has been restructured so it could be easier to create your own FarmClient class using a different library/transport instead of HTTPX but still leverage the Resource helper methods provided here.

paul121 commented 7 months ago

I still need to update documentation and fix some linting errors caused by async -> sync code generation. Existing code should only need to change how the client is instantiated and authorized, all other resource methods should continue to behave as before.

Before:

from farmOS import farmOS

farm = farmOS(
    hostname=FARMOS_HOSTNAME,
    scope="farm_manager",
    client_id="farm",
    version=2,
)
farm.authorize(USERNAME, PASSWORD)

After:

from httpx_auth import OAuth2ResourceOwnerPasswordCredentials
from farmOS import FarmClient

auth = OAuth2ResourceOwnerPasswordCredentials(
    token_url=f"{FARMOS_HOSTNAME}/oauth/token",
    username=USERNAME,
    password=PASSWORD,
    client_id="farm",
    scope="farm_manager",
)
farm = FarmClient(hostname=FARMOS_HOSTNAME, auth=auth)
paul121 commented 7 months ago

@symbioquine this async -> sync code generation is using the unasync approach implemented here: https://github.com/encode/httpcore/blob/master/scripts/unasync.py

Curious what you think. I know we had talked about async maybe "isn't worth it" but this is a pretty easy add and includes both sync and async tests (with automation to make sure async->sync changes are committed). I do think async is overkill for most use-cases but its nice to make it available.