Fi Collar / GraphQL Question

I just received my Tidbyt and love it so far. I got up and running with pixlet pretty quickly, and I’m fairly comfortable with Python.

I’m trying to write a simple app that uses the Fi Collar API to do something simple like display the step count of our dog. There’s a nice Python wrapper here - pytryfi · PyPI), but this is my first experience of Starlark. I see most of the libraries in Starlib are written in go - would I have to basically recreate the bits of the pytryfi library in go, or is there a graphql library for Starlark I could use?

I’d love to know how your Spotify app works… :slight_smile:

Alright, I figured it out. I’ll push everything to git at some point, but just in case anyone is checking here.

I’ll work on the render part next, but basically some experimenting in Postman and trying to recreate it in Starlark got me to this point - I now get my dog’s step count for the day!

load("render.star", "render")
load("http.star", "http")

VAR_USERNAME = "[email protected]"
VAR_PASSWORD = "password"
VAR_PET_ID = "petid"

API_BASE_URL = "https://api.tryfi.com"
API_LOGIN_URL = API_BASE_URL + "/auth/login"
API_GRAPHQL = API_BASE_URL + "/graphql"
QUERY_PET_DEVICE_DETAILS = "{pet (id: \"" + VAR_PET_ID + "\") { name dailyStat: currentActivitySummary (period: DAILY) { totalSteps stepGoal } } }"

def main():
    resp = http.post(API_LOGIN_URL, json_body={"email":VAR_USERNAME, "password":VAR_PASSWORD})
    session_id = resp.json()["sessionId"]
    cookie = resp.headers["Set-Cookie"]

    resp = http.post(API_GRAPHQL,headers={"sessionId":session_id, "Cookie":cookie}, json_body={"query":QUERY_PET_DEVICE_DETAILS})

    pet_name = resp.json()["data"]["pet"]["name"]
    daily_steps = resp.json()["data"]["pet"]["dailyStat"]["totalSteps"]
    step_goal = resp.json()["data"]["pet"]["dailyStat"]["stepGoal"]

    return render.Root(
        child = render.Text(str(pet_name + "\n" + str(int(daily_steps)) + "/" + str(int(step_goal))))
    )

Definitely needs some more work, but not bad for an evening of mucking around.

And yes, Freya needs a walk.