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

Fix use of settings #215

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading