Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional optimization to reduce API calls for certain scenarios #455

Open
tainium opened this issue Jul 7, 2024 · 0 comments
Open

Optional optimization to reduce API calls for certain scenarios #455

tainium opened this issue Jul 7, 2024 · 0 comments

Comments

@tainium
Copy link

tainium commented Jul 7, 2024

The problem

While the current PyiCloud library behavior is suitable for most use cases, it triggers multiple API requests when calling specific device information methods. This can be optimal to ensure you get the freshest data, but can lead to excessive API usage in certain scenarios, such as frequent polling or when all of the data you need is present in the initial response. Frequent API calls usually correspond to an impact on battery state as well.

Environment

  • pyiCloud release with the issue (pip show pyicloud): Version: 1.0.0
  • Last working pyiCloud release (if known): Version: 1.0.0
  • Service causing this issue: FindMyiPhone
  • Python version (python -V): Python 3.10.12
  • Operating environment (project deps/Docker/Windows/etc.): Ubuntu

Traceback/Error logs

This is not an error, but an optimization suggestion. With debug enabled or network sniffing, you will see a 'userInfo' response to each refresh request. There may be more than you expect.

Additional information

In many cases the first call contains all of the data that is needed and the other calls are redundant. Here's an optional optimization that can significantly reduce API calls. You can monkey patch the AppleDevice Class:

from pyicloud.services.findmyiphone import AppleDevice

# Simple patch to prevent refresh_client from doing anything in AppleDevice methods
def do_nothing(self, *args, **kwargs):
    pass

# Patch the methods in AppleDevice that call refresh_client
AppleDevice.status = do_nothing
# AppleDevice Class has most of the refresh calls.  Use this for any troublesome methods.

This patch:

  • Prevents refresh_client() from being called in the patched methods.
  • Reduces API calls by ~60% when fetching device data of more than one type.
@tainium tainium changed the title Optional optimization to reduce API calls for polling scenarios Optional optimization to reduce API calls for certain scenarios Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant