Skip to content

Commit

Permalink
Allowed url to not include api path, add message about missing protoc…
Browse files Browse the repository at this point in the history
…ol, and change HTTPError to requests lib
  • Loading branch information
tjleach98 committed Dec 15, 2023
1 parent 8803393 commit 74a2cd6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
10 changes: 7 additions & 3 deletions custom_components/zammad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

import asyncio
import logging
from urllib.error import HTTPError
import json
from requests.exceptions import HTTPError
from zammad_py import ZammadAPI

from .const import (
DOMAIN,
PLATFORMS,
STARTUP_MESSAGE,
HUMAN_ERR_MSG_FIELD,
)
from .coordinator import ZammadUpdateCoordinator
from .utils import get_url_from_options

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand All @@ -28,15 +31,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data.setdefault(DOMAIN, {})
_LOGGER.info(STARTUP_MESSAGE)

url = entry.data.get(CONF_URL)
url = get_url_from_options(entry.data.get(CONF_URL))
username = entry.data.get(CONF_USERNAME)
password = entry.data.get(CONF_PASSWORD)

try:
client = ZammadAPI(url=url, username=username, password=password)
await hass.async_add_executor_job(client.user.me)
except HTTPError as exc:
raise ConfigEntryAuthFailed("Credentials expired!") from exception
error_json = json.loads(exc.args[0])
raise ConfigEntryAuthFailed(error_json[HUMAN_ERR_MSG_FIELD])
except ClientConnectorError as exception:
raise ConfigEntryNotReady from exception

Expand Down
9 changes: 7 additions & 2 deletions custom_components/zammad/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import logging
from typing import Any, Mapping, Optional
import voluptuous as vol
from urllib.error import HTTPError
from requests.exceptions import HTTPError
from zammad_py import ZammadAPI

from .const import DOMAIN
from .utils import get_url_from_options

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -36,8 +37,12 @@ async def _check_creds_input(
self, user_input: Mapping[str, Any]
) -> Optional[dict[str, str]]:
"""Check to see if provided creds are accepted by Zammad"""
if not user_input[CONF_URL].startswith("http"):
_LOGGER.error(f"Invalid url missing protocol: {user_input[CONF_URL]}")
return {"base": "invalid_url_protocol"}

try:
url = user_input[CONF_URL]
url = get_url_from_options(user_input[CONF_URL])
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]

Expand Down
4 changes: 4 additions & 0 deletions custom_components/zammad/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@

STARTUP_MESSAGE = f"Starting setup for {DOMAIN}"
INITIAL_USER_ID = -1

API_URL_PATH = "/api/v1"

HUMAN_ERR_MSG_FIELD = "error_human"
7 changes: 4 additions & 3 deletions custom_components/zammad/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "Configure Zammad",
"description": "Enter your Zammad credentials",
"data": {
"url": "URL (include /api/v1)",
"url": "URL",
"username": "Email",
"password": "Password"
}
Expand All @@ -14,14 +14,15 @@
"title": "Configure Zammad",
"description": "Enter your Zammad credentials",
"data": {
"url": "URL (include /api/v1)",
"url": "URL",
"username": "Email/Username",
"password": "Password"
}
}
},
"error": {
"invalid_auth": "Auth Failed"
"invalid_auth": "Auth Failed",
"invalid_url_protocol": "URL must include http/https"
},
"abort": {
"already_configured": "Already Configured Account",
Expand Down
7 changes: 4 additions & 3 deletions custom_components/zammad/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "Configure Zammad",
"description": "Enter your Zammad credentials",
"data": {
"url": "URL (include /api/v1)",
"url": "URL",
"username": "Email",
"password": "Password"
}
Expand All @@ -14,14 +14,15 @@
"title": "Configure Zammad",
"description": "Enter your Zammad credentials",
"data": {
"url": "URL (include /api/v1)",
"url": "URL",
"username": "Email/Username",
"password": "Password"
}
}
},
"error": {
"invalid_auth": "Auth Failed"
"invalid_auth": "Auth Failed",
"invalid_url_protocol": "URL must include http/https"
},
"abort": {
"already_configured": "Already Configured Account",
Expand Down
8 changes: 8 additions & 0 deletions custom_components/zammad/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .const import API_URL_PATH


def get_url_from_options(url: str) -> str:
if url.endswith(API_URL_PATH):
return url
else:
return url + API_URL_PATH

0 comments on commit 74a2cd6

Please sign in to comment.