dahlb / kia_hyundai_api

python api wrapper for kia and hyundai apis
MIT License
5 stars 5 forks source link

Add response samples for USA Hyundai #2

Open dahlb opened 2 years ago

dahlb commented 2 years ago
pip3 install kia-hyundai-api==0.0.3

from a python console run

import logging
import asyncio
from kia_hyundai_api import UsHyundai
logger = logging.getLogger("kia_hyundai_api.us_hyundai")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
async def testing():
  api = UsHyundai()
  username = "USER"
  pin = "PIN"
  access_token, = await api.login(username=username, password="pass", pin=pin)
  vehicles = await api.get_vehicles(username=username, pin=pin, access_token=access_token)
  vin = vehicles["enrolledVehicleDetails"][0]["vin"]
  regid = vehicles["enrolledVehicleDetails"][0]["regid"]
  await api.get_cached_vehicle_status(username=username, pin=pin, access_token=access_token, vehicle_vin=vin)
  await api.get_location(username=username, pin=pin, access_token=access_token, vehicle_vin=vin)

asyncio.run(testing())

but with real username, password, and PIN. the console should show all the responses raw json; if you can post the responses I'll add them to the API files for reference like I did for the us kia version https://github.com/dahlb/kia_hyundai_api/blob/master/src/kia_hyundai_api/us_kia.py#L140

BE SURE TO REMOVE ANY PII like vin, but I'm not sure which PII will be in the responses, you could email it to me at dahl.brendan@gmail.com if you want a second pair of eyes to check for PII before it's online. Feel free to add the documentation in a PR if you'd like :).

dahlb commented 2 years ago

I also checked in a simple stub file for each region/brand to generate responses if you want to checkout the repo instead

dahlb commented 2 years ago

@rappazzo it would be great if you could contribute some sample responses here to harden up the uses of this implementation for the community

rappazzo commented 2 years ago

There's an error somewhere in there

Traceback (most recent call last):
  File "kia_hyundai_api/src/kia_hyundai_api/us_hyundai_stub.py", line 33, in <module>
    asyncio.run(testing())
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "kia_hyundai_api/src/kia_hyundai_api/us_hyundai_stub.py", line 26, in testing
    access_token, = await api.login(username=username, password=password, pin=pin)
  File "kia_hyundai_api/src/kia_hyundai_api/us_hyundai.py", line 154, in login
    response_json = await response.json()
AttributeError: 'NoneType' object has no attribute 'json'

I did adjust the stub to get the credentials interactively, so the line numbers won't be the same.

dahlb commented 2 years ago

@rappazzo thanks a lot for testing it out, I found the first US Hyundai error https://github.com/dahlb/kia_hyundai_api/blob/master/src/kia_hyundai_api/us_hyundai.py#L44, I was only checking the error case when I ran it I fixed the stubs logging and aiohttp cleanups, look forward to that interactive version if you can include that in your PR :)

rappazzo commented 2 years ago
Traceback (most recent call last):
  File "src/kia_hyundai_api/us_hyundai_stub.py", line 36, in <module>
    asyncio.run(testing())
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "src/kia_hyundai_api/us_hyundai_stub.py", line 27, in testing
    access_token, = await api.login(username=username, password=password, pin=pin)
  File "src/kia_hyundai_api/us_hyundai.py", line 162, in login
    expires_in = float(response["expires_in"])
TypeError: 'ClientResponse' object is not subscriptable

Authentication seemed to work though

dahlb commented 2 years ago

@rappazzo sorry for what feels like the second dunce mistake on my part. I fixed it here https://github.com/dahlb/kia_hyundai_api/commit/cb1f9936ade41a6b169068632fb2fb07fdc6ad67#diff-29e75d022b3edafdec384ced521a8176a2c3db83508de1639c92230be46e51d9R162 and cleaned up some other things after a full visual walk through. I hope that was the last error, I also added in the lock command for an example of an action api call. If you don't mind adding the response headers from the logs here too could be very helpful. Currently US Hyundai is the only api that doesn't track the status of the actions asynchronously but the response headers, particularly the lock's response headers could offer clues to tracking it.

rappazzo commented 2 years ago

I've finally had time to rerun this, and it does work. Here are my scrubbed results: us-hyundai-response.json.txt

Here is a diff of the changes that I made to that file to ask for the password: usa_kia_api.diff.txt

I did notice that the password was printed in the output in plain text, so you should probably clean that up.

dahlb commented 2 years ago

@rappazzo thanks I added that response to the get vehicles documentation and incorporated the user input into the stub files. I also sanitized the logs to try and prevent sensitive details from entering the logs. if you have the responses from the other api calls in the stub, get_cached_vehicle_status/get_location/lock it would be great to add those also, for lock if you can include the response headers that would be great as it might shed light on a feature us hyundai is missing that other regions/brands already have.