Package: python-fieldclimate
python-fieldclimateA 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 onasksandpycryptodome.InstallationUsepipto install the current release, version 1.3, fromPyPI:pip install python-fieldclimateUsageHere’s a simple example that returns the associated user’s account info:fromasyncioimportrunfromfieldclimateimportFieldClimateClientasyncdefmain():asyncwithFieldClimateClient(private_key="YOUR",public_key="KEYS")asclient:returnawaitclient.get_user()if__name__=="__main__":run(main)Event LoopsNew 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 withanyio, which currently supportsasyncio,curio, andtrio.AuthenticationHMAC credentials can be provided in several ways:Via the init constructor:>>> FieldClimateClient(public_key="YOUR", private_key="KEYS")Environment variablesFIELDCLIMATE_PUBLIC_KEYandFIELDCLIMATE_PRIVATE_KEY.Subclassing FieldClimateClient:>>> class MyClient(FieldClimateClient):
...     private_key = "YOUR"
...     public_key = "KEYS"If you use Django, you can usefieldclimate.django.DjangoFieldClimateClientin place of FieldClimateClient.
This subclass will grabFIELDCLIMATE_PUBLIC_KEYandFIELDCLIMATE_PRIVATE_KEYfrom django’s settings.MethodsThe 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 infieldclimate/__init__.pyfor 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 thetime_periodparameter.
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 astationparameter, likeget_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 byget_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 (testingdelete_user()might be a bad idea).
However, the underlying connection and cleaning utilities they use are all tested.Connection LimitsNew in version 1.3.The connection limit can be raised by setting the connections argument when calling the FieldClimateClient constructor.Fromasks’ docs:Youwillwant 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:asyncwithFieldClimateClient(connections=10)asclient:...According to FieldClimate’s docs, they do not yet enforce rate limiting server-side.
Using FieldClimateClient with a high connection limit allows you to createa lotof 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 ExampleThis 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.fromasyncioimportgather,runfromfieldclimateimportFieldClimateClientasyncdefmain():asyncwithFieldClimateClient(private_key="YOUR",public_key="KEYS",connections=20)asclient:asyncdefprint_user_json():print(awaitclient.get_user())asyncdefprint_station_dates(station):print(awaitclient.get_data_range(station))asyncdefcount_stations_then_print_ranges():stations=awaitclient.get_user_stations()print(len(stations))awaitgather(*[print_station_dates(station)forstationinstations[:10]])awaitgather(print_user_json(),count_stations_then_print_ranges(),)if__name__=="__main__":run(main())Alternate curio and trio implementations are thetestsdirectory,
if you want to see how to use FieldClimateClient in those event loops (it’s much of the same).Synchronous UsageRemoved in version 1.3.In version 1.2, FieldClimateClient would automatically set up an asyncio event loop when methods were
being called outside of anasync withblock.
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 theasksbackend, 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 stillreallydon’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 withasyncio.run():importasynciofromfieldclimateimportFieldClimateClientdefmain():client=FieldClimateClient(private_key="YOUR",public_key="KEYS")# print user jsonprint(asyncio.run(client.get_user()))# count stationsstations=asyncio.run(client.get_user_stations())print(len(stations))# print rangesforstationinstations[: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 dobutwait for the server.
Consider this when deciding whether or not to convert your code to use coroutine functions.ContributingPull requests are welcome. Please clean your code withblack, write tests, and document.Ideas for PRs:Exhaustive mocking to achieve full method test coverage.OAuth 2.0 authentication.ChangesTODOAdd 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:Droppedaiohttplibrary in favor of usingasks.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 fromfieldclimate.utilstofieldclimate.clean.
These functions now raiseAssertionErrorexplicitly, asassertstatements can be switched off.FieldClimateClient now inherits fromasks.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)Droppedrequestslibrary in favor of usingaiohttpfor both sync and async interfaces.1.1 (2018-10-25)Renamed allstation_idmethod parameters tostation, 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. 🎉AuthorsPhillip Marshall <pmarshall@agrimgt.com>