Skip to content

Commit 38781e5

Browse files
authored
Merge pull request #11 from Sholofly/features/logging
Add logging to GeocachingApi.
2 parents ce25be4 + 343f5bd commit 38781e5

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

geocachingapi/geocachingapi.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,23 @@ def __init__(
5656
self.token = token
5757
self.token_refresh_method = token_refresh_method
5858

59-
@backoff.on_exception(backoff.expo, GeocachingApiConnectionError, max_tries=3, logger=None)
59+
@backoff.on_exception(backoff.expo, GeocachingApiConnectionError, max_tries=3, logger=_LOGGER)
6060
@backoff.on_exception(
61-
backoff.expo, GeocachingApiRateLimitError, base=60, max_tries=6, logger=None
61+
backoff.expo, GeocachingApiRateLimitError, base=60, max_tries=6, logger=_LOGGER
6262
)
6363
async def _request(self, method, uri, **kwargs) -> ClientResponse:
6464
"""Make a request."""
6565
if self.token_refresh_method is not None:
6666
self.token = await self.token_refresh_method()
67-
_LOGGER.debug(f'Received API request with token {self.token}')
67+
_LOGGER.debug(f'Token refresh method called.')
68+
6869
url = URL.build(
6970
scheme=GEOCACHING_API_SCHEME,
7071
host=GEOCACHING_API_HOST,
7172
port=GEOCACHING_API_PORT,
7273
path=GEOCACHING_API_BASE_PATH,
7374
).join(URL(uri))
74-
_LOGGER.debug(f'URL: {url}')
75+
_LOGGER.debug(f'Executing {method} API request to {url}.')
7576
headers = kwargs.get("headers")
7677

7778
if headers is None:
@@ -80,9 +81,11 @@ async def _request(self, method, uri, **kwargs) -> ClientResponse:
8081
headers = dict(headers)
8182

8283
headers["Authorization"] = f"Bearer {self.token}"
83-
84+
_LOGGER.debug(f'With headers:')
85+
_LOGGER.debug(f'{str(headers)}')
8486
if self._session is None:
8587
self._session = ClientSession()
88+
_LOGGER.debug(f'New session created.')
8689
self._close_session = True
8790

8891
try:
@@ -119,20 +122,24 @@ async def _request(self, method, uri, **kwargs) -> ClientResponse:
119122

120123
# Handle empty response
121124
if response.status == 204:
125+
_LOGGER.warning(f'Request to {url} resulted in status 204. Your dataset could be out of date.')
122126
return
123127

124128
if "application/json" in content_type:
125129
result = await response.json()
126-
_LOGGER.debug(f'response: {str(result)}')
130+
_LOGGER.debug(f'Response:')
131+
_LOGGER.debug(f'{str(result)}')
127132
return result
128133
result = await response.text()
129-
_LOGGER.debug(f'response: {str(result)}')
134+
_LOGGER.debug(f'Response:')
135+
_LOGGER.debug(f'{str(result)}')
130136
return result
131137

132138
async def update(self) -> GeocachingStatus:
133139
await self._update_user(None)
134140
if self._settings.fetch_trackables:
135141
await self._update_trackables()
142+
_LOGGER.info(f'Status updated.')
136143
return self._status
137144

138145
async def _update_user(self, data: Dict[str, Any] = None) -> None:
@@ -150,6 +157,7 @@ async def _update_user(self, data: Dict[str, Any] = None) -> None:
150157
])
151158
data = await self._request("GET", f"/{GEOCACHING_API_VERSION}/users/me?fields={fields}")
152159
self._status.update_user_from_dict(data)
160+
_LOGGER.debug(f'User updated.')
153161

154162
async def _update_trackables(self, data: Dict[str, Any] = None) -> None:
155163
assert self._status
@@ -165,11 +173,13 @@ async def _update_trackables(self, data: Dict[str, Any] = None) -> None:
165173
])
166174
data = await self._request("GET", f"/{GEOCACHING_API_VERSION}/trackables?fields={fields}&type=3")
167175
self._status.update_trackables_from_dict(data)
176+
_LOGGER.debug(f'Trackables updated.')
168177

169178
async def close(self) -> None:
170179
"""Close open client session."""
171180
if self._session and self._close_session:
172181
await self._session.close()
182+
_LOGGER.debug(f'Session closed.')
173183

174184
async def __aenter__(self) -> GeocachingApi:
175185
"""Async enter."""

0 commit comments

Comments
 (0)