Skip to content

Commit

Permalink
updated get_base_url impl
Browse files Browse the repository at this point in the history
  • Loading branch information
somethingmorerelevant committed Oct 21, 2024
1 parent a6351bc commit 0bd1e45
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions tap_zendesk_chat/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@
class RateLimitException(Exception):
pass

class InvalidConfigurationError(Exception):
pass


class Client:
def __init__(self, config):
self.access_token = config["access_token"]
self.user_agent = config.get("user_agent", "tap-zendesk-chat")
self.headers = {}
self.subdomain = config.get("subdomain")
self.headers["Authorization"] = f"Bearer {self.access_token}"
self.headers["User-Agent"] = self.user_agent
if "subdomain" in config:
self.base_url = f"https://{config['subdomain']}.zendesk.com"
else:
self.base_url = BASE_URL
LOGGER.warning("Missing Subdomain, please recheck the configuration")
self.base_url = self.get_base_url()
self.session = requests.Session()

def get_base_url(self):
"""
Determines the base URL to use for Zendesk API requests.
Checks the availability of zendesk chat endpoints
and returns the available one
Returns:
str: The base URL to use for subsequent API requests.
Raises:
InvalidConfigurationError: If neither endpoint is accessible.
"""
urls = [
(f"https://{self.subdomain}.zendesk.com" , "/api/v2/chat/agents"),
(BASE_URL , "/api/v2/agents")
]
for domain, endpoint in urls:
resp = requests.get(f"{domain}{endpoint}", headers=self.headers)
LOGGER.info("API CHECK %s %s", resp.url, resp.status_code)
if resp.status_code == 200:
return domain
raise InvalidConfigurationError("Please check the URL or reauthenticate")

@backoff.on_exception(backoff.expo, RateLimitException, max_tries=10, factor=2)
def request(self, tap_stream_id, params=None, url=None, url_extra=""):
with metrics.http_request_timer(tap_stream_id) as timer:
Expand Down

0 comments on commit 0bd1e45

Please sign in to comment.