A client for the iMetos FieldClimate API.
Project description
python-fieldclimate
A client for the iMetos FieldClimate API: https://api.fieldclimate.com/v1/docs/
To use this, you’ll need HMAC credentials provided by iMetos. See their docs for more info.
Requires Python 3.5 or better. Tested on Python 3.6. Depends on asks and pycryptodome.
Installation
Use pip to install the current release, version 1.3, from PyPI:
pip install python-fieldclimate
Usage
Here’s a simple example that returns the associated user’s account info:
from asyncio import run
from fieldclimate import FieldClimateClient
async def main():
async with FieldClimateClient(private_key="YOUR", public_key="KEYS") as client:
return await client.get_user()
if __name__ == "__main__":
run(main)
Event Loops
New in version 1.3.
The same FieldClimateClient class can be used to make asynchronous API requests under any modern event loop. This is thanks to asks being written with anyio, which currently supports asyncio, curio, and trio.
Authentication
HMAC credentials can be provided in several ways:
Via the init constructor:
>>> FieldClimateClient(public_key="YOUR", private_key="KEYS")
Environment variables FIELDCLIMATE_PUBLIC_KEY and FIELDCLIMATE_PRIVATE_KEY.
Subclassing FieldClimateClient:
>>> class MyClient(FieldClimateClient): ... private_key = "YOUR" ... public_key = "KEYS"
If you use Django, you can use fieldclimate.django.DjangoFieldClimateClient in place of FieldClimateClient. This subclass will grab FIELDCLIMATE_PUBLIC_KEY and FIELDCLIMATE_PRIVATE_KEY from django’s settings.
Methods
The client has methods for each of the corresponding routes listed in the api docs. There’s a lot of them, so see the full list of methods in fieldclimate/__init__.py for more details. Every method returns a JSON-like python object upon being awaited, like a dictionary or a list.
Some methods will clean up their arguments in order to make working with the API in python easier. Here are some examples:
get_data_last() accepts the time_period parameter. The API docs specify this to be a string like '6h' or '7d', meaning 6 hours or 7 days. FieldClimateClient additionally accepts timedelta objects for this parameter, and will convert them to their equivalent strings for the API (i.e. timedelta(hours=6) is converted to '21600' seconds).
Many methods require a station parameter, like get_data_range() does in the examples above. This can be a raw Station ID string, which you can dig out of a station dictionary returned by get_user_stations(). Or, you can pass that dictionary directly in as the station parameter, and the ID will be extracted.
These methods do not all have test coverage (testing delete_user() might be a bad idea). However, the underlying connection and cleaning utilities they use are all tested.
Connection Limits
New in version 1.3.
The connection limit can be raised by setting the connections argument when calling the FieldClimateClient constructor.
From asks’ docs:
You will want to change the number of connections to a value that suits your needs and the server’s limitations. If no data is publicly available to guide you here, err on the low side.
The default number of connections in the pool for a Session is a measly ONE.
Example:
async with FieldClimateClient(connections=10) as client:
...
According to FieldClimate’s docs, they do not yet enforce rate limiting server-side. Using FieldClimateClient with a high connection limit allows you to create a lot of requests at once. During my testing, I noticed the API starting to raise 502 errors when I overloaded it too much.
Please be courteous with your resource consumption!
Advanced Example
This function asks for some user data and gets the list of all user stations, at the same time. As soon as the stations come back, it counts them and sends off another request for each of the first 10 stations. Then each of those 10 station responses is printed, sorted by server reply time.
from asyncio import gather, run
from fieldclimate import FieldClimateClient
async def main():
async with FieldClimateClient(
private_key="YOUR",
public_key="KEYS",
connections=20
) as client:
async def print_user_json():
print(await client.get_user())
async def print_station_dates(station):
print(await client.get_data_range(station))
async def count_stations_then_print_ranges():
stations = await client.get_user_stations()
print(len(stations))
await gather(*[
print_station_dates(station)
for station in stations[:10]
])
await gather(
print_user_json(),
count_stations_then_print_ranges(),
)
if __name__ == "__main__":
run(main())
Alternate curio and trio implementations are the tests directory, if you want to see how to use FieldClimateClient in those event loops (it’s much of the same).
Synchronous Usage
Removed in version 1.3.
In version 1.2, FieldClimateClient would automatically set up an asyncio event loop when methods were being called outside of an async with block. This way, callers could use the library without having to write any scary async/await code.
Having this mix of syntax ended up being confusing and unnecessary, in addition to leading to messy code here. So, with the switch to the asks backend, support for the old synchronous use case was removed.
If you were using FieldClimateClient’s older ‘synchronous usage’ mode, you were already using a version of Python that allowed for async/await. The difference is that now you have to set up an event loop yourself.
If you still really don’t want to write any coroutines, the simplest way to make your code compatible with version 1.3 is to just wrap each method call with asyncio.run():
import asyncio
from fieldclimate import FieldClimateClient
def main():
client = FieldClimateClient(private_key="YOUR", public_key="KEYS")
# print user json
print(asyncio.run(client.get_user()))
# count stations
stations = asyncio.run(client.get_user_stations())
print(len(stations))
# print ranges
for station in stations[:10]:
print(asyncio.run(client.get_data_range(station)))
if __name__ == "__main__":
main()
This ‘synchronous’ example takes 3 times longer to complete than the equivalent “Advanced Example” above, because the main() function is blocked during each request sent to the server. The asynchronous code, on the other hand, only blocks when there’s nothing to do but wait for the server. Consider this when deciding whether or not to convert your code to use coroutine functions.
Contributing
Pull requests are welcome. Please clean your code with black, write tests, and document.
Ideas for PRs:
Exhaustive mocking to achieve full method test coverage.
OAuth 2.0 authentication.
Changes
TODO
Add support for Metos’ API v2: https://api.fieldclimate.com/v2/docs/ - How should we best support both users of v2 and v1, which should still be supported? - Need to assess how different the new API is before deciding on how to tackle this. - Increment major version to track with upstream.
1.3 (2019-09-23)
High-level changes:
Dropped aiohttp library in favor of using asks.
This adds support for asyncio, trio, and curio async loops.
Dropped synchronous interface on FieldClimateClient. This means all client methods must now be awaited.
Implementation changes:
Moved url validation functions from fieldclimate.utils to fieldclimate.clean. These functions now raise AssertionError explicitly, as assert statements can be switched off.
FieldClimateClient now inherits from asks.Session, which provides async context manager usage and connection rate limiting.
Removed BaseClient and HmacClient classes, unifying their functionality in FieldClimateClient.
Added tests for trio and curio event loops.
Bonus changes:
Added DjangoFieldClimateClient. This subclass gets your HMAC authentication keys from django’s settings, which can save you a few lines of code if you already use django.
1.2 (2018-10-26)
Dropped requests library in favor of using aiohttp for both sync and async interfaces.
1.1 (2018-10-25)
Renamed all station_id method parameters to station, possibly breaking your code.
This argument can now handle an entire station dictionary, and will extract the station_id automatically.
1.0 (2018-10-24)
Initial PyPI release. 🎉
Project details
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 python-fieldclimate-1.3.tar.gz
.
File metadata
- Download URL: python-fieldclimate-1.3.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d21a3b6b501ce029ff50c562384798e646e5a68840d600187af0ffed723439a |
|
MD5 | 726f61483761a4496396a8ddc605bac6 |
|
BLAKE2b-256 | e690f5f11bd34f9b12263e60e58ee0d8f4e459896a619b0480d17d22a999d000 |
File details
Details for the file python_fieldclimate-1.3-py3-none-any.whl
.
File metadata
- Download URL: python_fieldclimate-1.3-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b78aed419413339a1c65a595335d7c9227d4c87faaf64063c1849e6d8c5d0300 |
|
MD5 | d83b61234923bc9f02218357c55f060b |
|
BLAKE2b-256 | b166ee1045624c21184892d4c971f866f60e4cbe78eea7e56a096abfddcd3b80 |