Skip to content

Commit

Permalink
Merge pull request #215 from KerkhoffTechnologies/3384-more-djautotas…
Browse files Browse the repository at this point in the history
…k-use-of-settings

Fix use of settings
  • Loading branch information
kti-sam authored Feb 8, 2024
2 parents 23e2ff7 + 92ad928 commit d821e93
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion djautotask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (1, 5, 3, 'final')
VERSION = (1, 5, 4, 'final')

# pragma: no cover
if VERSION[-1] != "final":
Expand Down
23 changes: 13 additions & 10 deletions djautotask/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def update_cache(json_obj):
f'zone_{AT_WEB_KEY}', json_obj[AT_WEB_KEY], timeout=CACHE_TIMEOUT)


def get_api_connection_url(force_fetch=False):
def get_api_connection_url(username, force_fetch=False):
try:
return _get_connection_url(AT_URL_KEY, force_fetch)
return _get_connection_url(AT_URL_KEY, username, force_fetch)
except AutotaskAPIError as e:
# Save a log even when the get_zone_info request fails.
SyncJob = apps.get_model('djautotask', 'SyncJob')
Expand All @@ -107,16 +107,16 @@ def get_api_connection_url(force_fetch=False):
sync_job.save()


def get_web_connection_url(force_fetch=False):
return _get_connection_url(AT_WEB_KEY, force_fetch)
def get_web_connection_url(username, force_fetch=False):
return _get_connection_url(AT_WEB_KEY, username, force_fetch)


def _get_connection_url(field, force_fetch=False):
def _get_connection_url(field, username, force_fetch=False):
api_url_from_cache = get_cached_url(field)

if not api_url_from_cache or force_fetch:
try:
json_obj = get_zone_info(settings.AUTOTASK_CREDENTIALS['username'])
json_obj = get_zone_info(username)
# Update cache if empty or forced
update_cache(json_obj)

Expand All @@ -130,8 +130,9 @@ def _get_connection_url(field, force_fetch=False):


def get_zone_info(username):
endpoint_url = settings.AUTOTASK_SERVER_URL + 'v1.0/zoneInformation?user='\
+ username
endpoint_url = '{}v1.0/zoneInformation?user={}'.format(
settings.AUTOTASK_SERVER_URL, username
)

try:
logger.debug('Making GET request to {}'.format(endpoint_url))
Expand Down Expand Up @@ -298,7 +299,7 @@ def __init__(
rest_api_version = \
settings.AUTOTASK_CREDENTIALS['rest_api_version']
if not server_url:
server_url = get_api_connection_url()
server_url = get_api_connection_url(username)

if not self.API:
raise ValueError('API not specified')
Expand Down Expand Up @@ -497,7 +498,9 @@ def _fetch_resource(endpoint_url, request_retry_counter=None,
logger.warning(msg)
if request_retry_counter['count'] <= self.MAX_401_ATTEMPTS:
cached_url = get_cached_url(AT_URL_KEY)
if cached_url != get_api_connection_url(force_fetch=True):
if cached_url != get_api_connection_url(
self.username, force_fetch=True
):
logger.info('Zone information has been changed, '
'so this request will be retried.')
raise AutotaskAPIError(response.content)
Expand Down

0 comments on commit d821e93

Please sign in to comment.