rcastberg / sure_petcare

Python library for accessing sure connect petflap
GNU General Public License v3.0
55 stars 15 forks source link

Substantial rewrite for efficiency and flexibilty; support for some new API calls #3

Closed strix-technica closed 6 years ago

strix-technica commented 6 years ago

This is a substantial rewrite with two objectives in mind: to make as few back-end REST requests as possible (which is crucial in terms of avoiding sanctions against your account for abuse of Sure's infrastructure) and to make the API better-suited to programmatic applications.

A number of feature enhancements and additions have also been made. The principal contributions of this PR are:

The code in this PR is not 100% backwards compatible. One or two methods have been renamed, but chances are not ones you may use directly.

The main change required by any user of this API is to enclose calls to any of the update methods inside with ...: context block. When the block exits, the cache is flushed back to disc. The cache is only locked for the duration of the context block, so you can query it as much as you like from as many instances as you like provided only one instance at any one time is performing an update. Example:

api = sure_petcare.SurePetFlap()
pet_id = # appropriate ID or code here
with api:
    # You are **strongly** encouraged to update only things you need to update.
    # `update_timeline()` is particularly expensive so, if you don't need it, don't call it.
    api.update_pet_status()
print( api.pets[pet_id]['name'], api.get_current_status api.get_current_status( pet_id ) )
strix-technica commented 6 years ago

@jmvedrine, you might be interested in this PR — especially if you plan to do any significant development of your own.

Thanks for the thumbs-up, @Timx85!

@rcastberg is a bit preoccupied with other stuff right now so I don't know when he might be able to deal with this PR. I don't know if it's possible to issue a PR against a different fork, but you each can manually merge these changes via the git command line if you want (ask if you need any pointers).

N/m, it is now merged so do update your forks. The REST API has changed a bit and I haven't updated the requests accordingly (yet), and if the old-style requests quit working you'll need to pull/merge to get the updates.

Good luck, and I hope this is of some use!