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

[Library Bug] Bug from #99: methods with join call broken #101

Closed
kizill opened this issue Jun 13, 2022 · 7 comments · Fixed by #102
Closed

[Library Bug] Bug from #99: methods with join call broken #101

kizill opened this issue Jun 13, 2022 · 7 comments · Fixed by #102
Assignees
Labels
bug Something isn't working

Comments

@kizill
Copy link
Contributor

kizill commented Jun 13, 2022

Describe the bug
Methods like get_state, set_state, trigger_service & fire_event are broken since #99

To Reproduce
This simple code snippet fails:

api_url = "https://somemy.domain.com/api"
token = 'MYTOKEN'
with Client(
    api_url,
    token,
) as ha_client:
    temperature = ha_client.get_state(entity_id='sensor.temperature')

Stack trace:

Traceback (most recent call last):
  File "/Users/kizill/repo/bolotov_info_bot/playground.py", line 29, in <module>
    ha_client.get_state(entity_id='sensor.temperature')
  File "/opt/homebrew/Caskroom/miniconda/base/envs/tg_bot/lib/python3.10/site-packages/homeassistant_api/rawclient.py", line 233, in get_state
    data = self.request(join("states", entity_id))
  File "/opt/homebrew/Caskroom/miniconda/base/envs/tg_bot/lib/python3.10/site-packages/homeassistant_api/rawclient.py", line 76, in request
    return self.response_logic(resp)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/tg_bot/lib/python3.10/site-packages/homeassistant_api/rawclient.py", line 81, in response_logic
    return Processing(response=response).process()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/tg_bot/lib/python3.10/site-packages/homeassistant_api/processing.py", line 82, in process
    raise EndpointNotFoundError(self.response.url)
homeassistant_api.errors.EndpointNotFoundError: Cannot make request to the endpoint 'https://somemy.domain.com/api/sensor.temperature'

Reason
urljoin works not like path join:
urljoin('xyz', 'abc') - > 'abc'
urljoin('xyz/', 'abc') - > 'xyz/abc'

@kizill kizill added the bug Something isn't working label Jun 13, 2022
@GrandMoff100
Copy link
Owner

Thanks for letting me know! Figures something like this should happen. I'll just program my own join method that works on all OSes. Should be fixed in the next half hour.

@GrandMoff100
Copy link
Owner

from posixpath import join as urljoin

This should work exactly like os.path.join does on linux/macOS, but will also work on windows the same way. And removes the complexity and mysteriousness of urllib.parse.urljoin.

@kizill
Copy link
Contributor Author

kizill commented Jun 13, 2022

Thank you! I've monkeypatched with

def hacky_join(a, b):
    from urllib.parse import urljoin
    return urljoin(a + '/' if not a.endswith('/') else '', b)


homeassistant_api.rawclient.join = hacky_join
homeassistant_api._async.asyncclient.join = hacky_join

@GrandMoff100
Copy link
Owner

GrandMoff100 commented Jun 13, 2022

@GrandMoff100
Copy link
Owner

Thank you! I've monkeypatched with

def hacky_join(a, b):
    from urllib.parse import urljoin
    return urljoin(a + '/' if not a.endswith('/') else '', b)


homeassistant_api.rawclient.join = hacky_join
homeassistant_api._async.asyncclient.join = hacky_join

I guess that works!

@GrandMoff100
Copy link
Owner

Gimme five minutes and I can have a new PR

@GrandMoff100
Copy link
Owner

New version out! Update to 3.0.3.post4 to receive the fixed feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants