A Python library for interacting with farmOS over API.
Project description
farmOS.py
farmOS.py is a Python library for interacting with farmOS over API.
For more information on farmOS, visit farmOS.org.
Installation
To install using pip:
$ pip install farmOS
Usage
Authentication
The farmOS.py client authenticates with the farmOS server via OAuth Bearer
tokens. Before authenticating with the server, a farmOS client must be
created and an OAuth Authorization flow must be completed (unless an optional
token
was provided when creating the client).
Authorizing with Password Credentials (most common)
from farmOS import farmOS
hostname = "myfarm.farmos.net"
username = "username"
password = "password"
# Create the client.
farm_client = farmOS(
hostname=hostname,
client_id = "farm", # Optional. The default oauth client_id "farm" is enabled on all farmOS servers.
scope="user_access", # Optional. The default scope is "user_access". Only needed if authorizing with a different scope.
version=1 # Optional. The major version of the farmOS server, 1 or 2. Defaults to 1.
)
# Authorize the client, save the token.
token = farm_client.authorize(username, password, scope="user_access")
Running from a Python Console, the username
and password
can also be
omitted and entered at runtime. This allows testing without saving
credentials in plaintext:
>>> from farmOS import farmOS
>>> farm_client = farmOS(hostname="myfarm.farmos.net", client_id="farm", scope="user_access")
>>> farm_client.authorize()
Warning: Password input may be echoed.
Enter username: >? username
Warning: Password input may be echoed.
Enter password: >? password
>>> farm_client.info()
'name': 'server-name', 'url': 'http://localhost', 'api_version': '1.2', 'user': ....
Authorizing with existing OAuth Token (advanced)
An existing token can be provided when creating the farmOS client. This is useful for advanced use cases where an OAuth token may be persisted.
from farmOS import farmOS
hostname = "myfarm.farmos.net"
token = {
"access_token": "abcd",
"refresh_token": "abcd",
"expires_at": "timestamp",
}
# Create the client with existing token.
farm_client = farmOS(
hostname=hostname,
token=token,
)
Saving OAuth Tokens
By default, access_tokens
expire in 1 hour. This means that requests sent
1 hour after authorization will trigger a refresh
flow, providing the
client with a new access_token
to use. A token_updater
utility must be
provided to save tokens when automatic refreshing occurs.
from farmOS import farmOS
hostname = "myfarm.farmos.net"
username = "username"
password = "password"
# Maintain an external state of the token.
current_token = None
# Callback function to save new tokens.
def token_updater(new_token):
print(f"Got a new token! {new_token}")
# Update state.
current_token = new_token
# Create the client.
farm_client = farmOS(
hostname=hostname,
token_updater=token_updater, # Provide the token updater callback.
)
# Authorize the client.
# Save the initial token that is created.
current_token = farm_client.authorize(username, password, scope="user_access")
Server Info
info = farm_client.info()
{
'name': 'farmos-test',
'url': 'http://localhost',
'api_version': '1.2',
'user': {
'uid': '4',
'name': 'paul',
'mail': 'paul.weidner+2@gmail.com'
},
'google_maps_api_key': 'AIzaSyCCHTbAGC_gHegwepMxBu_AKd_RmP54mDg',
'metrics': {
'equipment': {'label': 'Equipment', 'value': '7', 'link': 'farm/assets/equipment/list', 'weight': 0},
'areas': {'label': 'Areas', 'value': '20', 'link': 'farm/areas', 'weight': 100},
'field': {'label': 'Field area', 'value': '532 hectares', 'link': 'farm/areas', 'weight': 101}
},
'system_of_measurement': 'metric',
}
Client methods
farmOS.py can connect to farmOS servers running version ^1.6 or 2.x. The version should be specified when instantiating the farmOS client, see Authentication.
Because of API changes in farmOS 2.x, the client provides different methods depending on the server version:
Logging
You can configure how farmOS
logs are displayed with the following:
import logging
# Required to init a config on the ROOT logger, that all other inherit from
logging.basicConfig()
# Configure all loggers under farmOS (farmOS.client, famrOS.session) to desired level
logging.getLogger("farmOS").setLevel(logging.DEBUG)
# Hide debug logging from the farmOS.session module
logging.getLogger("farmOS.session").setLevel(logging.WARNING)
More info on logging in Python here.
TESTING
Functional tests require a live instance of farmOS to communicate with. Configure credentials for the farmOS instance to test against by setting the following environment variables:
For farmOS Drupal Authentication:
FARMOS_HOSTNAME
, FARMOS_RESTWS_USERNAME
, and FARMOS_RESTWS_PASSWORD
For farmOS OAuth Authentication (Password Flow):
FARMOS_HOSTNAME
, FARMOS_OAUTH_USERNAME
, FARMOS_OAUTH_PASSWORD
, FARMOS_OAUTH_CLIENT_ID
, FARMOS_OAUTH_CLIENT_SECRET
Automated tests are run with pytest
python setup.py test
MAINTAINERS
- Paul Weidner (paul121) - https://github.com/paul121
- Michael Stenta (m.stenta) - https://github.com/mstenta
This project has been sponsored by:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file farmOS-1.0.0a2.tar.gz
.
File metadata
- Download URL: farmOS-1.0.0a2.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d4402f9ef43f8377382a09ecc61b045702c5629cd2b02b5566252173d11b633 |
|
MD5 | 850be57b5938fdd20c056b9b84b09359 |
|
BLAKE2b-256 | 7454b3a7f53675fc1988a99e40df71711d8903ab8c0403c05bd3e0abb14db546 |
File details
Details for the file farmOS-1.0.0a2-py3-none-any.whl
.
File metadata
- Download URL: farmOS-1.0.0a2-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da2290374efcb8882794027f08bb722bef841e8faa251b31931d6652e23fee48 |
|
MD5 | 1bc44ecf2cf988839af5ba603b1584bd |
|
BLAKE2b-256 | 1888270a45975cd6b03e3b0a42d6f33bb5197b6edfc8791207e0b58ea46616f7 |