matin / garth

Garmin SSO auth + Connect Python client
MIT License
305 stars 22 forks source link

fix put/delete request #35

Closed bugficks closed 11 months ago

bugficks commented 11 months ago

This PR fixes PUT and DELETE requests. From browser logs those are actually POST requests with method defined in _X-Http-Method-Override header.

matin commented 11 months ago

Can you try testing this?

I say that because I don't believe this code will work. Garth is based on the mobile and not the web API.

bugficks commented 11 months ago

It does work but now I am trying to figure out why I had to add it :) Maybe it was required using 'connect.' instead 'connectapi.'. I am pretty sure I didn't add it just for good looks... ah well.

if method in ["PUT", "DELETE"]:
    headers["X-Http-Method-Override"] = method
    method = "POST"
    import pathlib
    print(f"{pathlib.Path(__file__).relative_to(pathlib.Path().absolute())}: {method} Override: {headers.get("X-Http-Method-Override", "-")} url:{url}")

main.py:

exerciseSets = garmin.get_activity_exercise_sets(12660624335)
exerciseSetWeight = exerciseSets["exerciseSets"][0]["weight"]
print(f"current exerciseSetWeight: {exerciseSetWeight}")

exerciseSets["exerciseSets"][0]["weight"] *= 1.3
garmin.set_activity_exercise_sets(12660624335, exerciseSets)

exerciseSets = garmin.get_activity_exercise_sets(12660624335)
exerciseSetWeight = exerciseSets["exerciseSets"][0]["weight"]
print(f"new exerciseSetWeight: {exerciseSetWeight}")
❯ python main.py
current exerciseSetWeight: 12851.0
.venv\Lib\site-packages\garth\http.py: POST Override: PUT url:https://connectapi.garmin.com/activity-service/activity/12660624335/exerciseSets
new exerciseSetWeight: 16706.0

\ \ I would like to propose another change. Right now 'connectapi' only allows for 'GET'. How about something like this:

def connectapi(self, path: str, **kwargs):
    method = kwargs.get("method", "GET").lower()
    garthReq = getattr(self.garth, method)
    kwargs.pop("method", None)

    resp = garthReq("connectapi", path, api=True, **kwargs)
    if resp.status_code == 204:
        rv = None
    else:
        rv = resp.json()
    return rv

this would allow for e.g. self.connectapi('/api/endpoint", json=payload, method="PUT")

matin commented 11 months ago

That seems like a reasonable idea. Do you want to close out this PR and open an issue for your suggestion?

bugficks commented 11 months ago

sure PR #37