Skip to content

Commit 46c2947

Browse files
committed
Add realtime option to get links
1 parent 3bcb0bc commit 46c2947

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

coc/http.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,18 @@ class HTTPClient:
166166

167167
# pylint: disable=too-many-arguments, missing-docstring, protected-access, too-many-branches
168168
def __init__(
169-
self,
170-
client,
171-
loop,
172-
email,
173-
password,
174-
key_names,
175-
key_count,
176-
key_scopes,
177-
throttle_limit,
178-
throttler=BasicThrottler,
179-
cache_max_size=10000,
180-
stats_max_size=1000,
169+
self,
170+
client,
171+
loop,
172+
email,
173+
password,
174+
key_names,
175+
key_count,
176+
key_scopes,
177+
throttle_limit,
178+
throttler=BasicThrottler,
179+
cache_max_size=10000,
180+
stats_max_size=1000
181181
):
182182
self.client = client
183183
self.loop = loop
@@ -187,7 +187,6 @@ def __init__(
187187
self.key_count = key_count
188188
self.key_scopes = key_scopes
189189
self.throttle_limit = throttle_limit
190-
191190
per_second = key_count * throttle_limit
192191

193192
self.__session = None
@@ -226,7 +225,7 @@ async def request(self, route, **kwargs):
226225
url = route.url
227226

228227
headers = {
229-
"Accept": "application/json",
228+
"Accept" : "application/json",
230229
"authorization": "Bearer {}".format(next(self.keys)),
231230
}
232231
kwargs["headers"] = headers
@@ -237,7 +236,7 @@ async def request(self, route, **kwargs):
237236
cache_control_key = route.url
238237
cache = self.cache
239238
# the cache will be cleaned once it becomes stale / a new object is available from the api.
240-
if cache is not None:
239+
if cache is not None and not self.client.realtime:
241240
try:
242241
return cache[cache_control_key]
243242
except KeyError:
@@ -261,7 +260,7 @@ async def request(self, route, **kwargs):
261260
# set a callback to remove the item from cache once it's stale.
262261
delta = int(response.headers["Cache-Control"].strip("max-age="))
263262
data["_response_retry"] = delta
264-
if cache is not None:
263+
if cache is not None and not self.client.realtime:
265264
self.cache[cache_control_key] = data
266265
LOG.debug("Cache-Control max age: %s seconds, key: %s", delta, cache_control_key)
267266
self.loop.call_later(delta, self._cache_remove, cache_control_key)
@@ -294,8 +293,8 @@ async def request(self, route, **kwargs):
294293
raise NotFound(response, data)
295294
if response.status == 429:
296295
LOG.error(
297-
"We have been rate-limited by the API. "
298-
"Reconsider the number of requests you are allowing per second."
296+
"We have been rate-limited by the API. "
297+
"Reconsider the number of requests you are allowing per second."
299298
)
300299
raise HTTPException(response, data)
301300

@@ -347,13 +346,16 @@ def get_clan_warlog(self, tag):
347346
return self.request(Route("GET", "/clans/{}/warlog".format(tag)))
348347

349348
def get_clan_current_war(self, tag):
350-
return self.request(Route("GET", "/clans/{}/currentwar".format(tag)))
349+
return self.request(Route("GET", "/clans/{}/currentwar".format(tag) +
350+
'?realtime=true' if self.client.realtime else ''))
351351

352352
def get_clan_war_league_group(self, tag):
353-
return self.request(Route("GET", "/clans/{}/currentwar/leaguegroup".format(tag)))
353+
return self.request(Route("GET", "/clans/{}/currentwar/leaguegroup".format(tag) +
354+
'?realtime=true' if self.client.realtime else ''))
354355

355356
def get_cwl_wars(self, war_tag):
356-
return self.request(Route("GET", "/clanwarleagues/wars/{}".format(war_tag)))
357+
return self.request(Route("GET", "/clanwarleagues/wars/{}".format(war_tag) +
358+
'?realtime=true' if self.client.realtime else ''))
357359

358360
# locations
359361

@@ -420,7 +422,8 @@ async def initialise_keys(self):
420422
LOG.info("Successfully logged into the developer site.")
421423

422424
resp_paylaod = await resp.json()
423-
ip = json_loads(base64_b64decode(resp_paylaod["temporaryAPIToken"].split(".")[1] + "====").decode("utf-8"))["limits"][1]["cidrs"][0].split("/")[0]
425+
ip = json_loads(base64_b64decode(resp_paylaod["temporaryAPIToken"].split(".")[1] + "====").decode("utf-8"))[
426+
"limits"][1]["cidrs"][0].split("/")[0]
424427

425428
LOG.info("Found IP address to be %s", ip)
426429

@@ -433,17 +436,17 @@ async def initialise_keys(self):
433436
if len(self._keys) < self.key_count:
434437
for key in (k for k in keys if k["name"] == self.key_names and ip not in k["cidrRanges"]):
435438
LOG.info(
436-
"Deleting key with the name %s and IP %s (not matching our current IP address).",
437-
self.key_names, key["cidrRanges"],
439+
"Deleting key with the name %s and IP %s (not matching our current IP address).",
440+
self.key_names, key["cidrRanges"],
438441
)
439442
await session.post("https://developer.clashofclans.com/api/apikey/revoke", json={"id": key["id"]})
440443

441444
while len(self._keys) < self.key_count and len(keys) < KEY_MAXIMUM:
442445
data = {
443-
"name": self.key_names,
446+
"name" : self.key_names,
444447
"description": "Created on {}".format(datetime.now().strftime("%c")),
445-
"cidrRanges": [ip],
446-
"scopes": [self.key_scopes],
448+
"cidrRanges" : [ip],
449+
"scopes" : [self.key_scopes],
447450
}
448451

449452
LOG.info("Creating key with data %s.", str(data))
@@ -462,9 +465,9 @@ async def initialise_keys(self):
462465
if len(self._keys) == 0:
463466
await self.close()
464467
raise RuntimeError(
465-
"There are {} API keys already created and none match a key_name of '{}'."
466-
"Please specify a key_name kwarg, or go to 'https://developer.clashofclans.com' to delete "
467-
"unused keys.".format(len(keys), self.key_names)
468+
"There are {} API keys already created and none match a key_name of '{}'."
469+
"Please specify a key_name kwarg, or go to 'https://developer.clashofclans.com' to delete "
470+
"unused keys.".format(len(keys), self.key_names)
468471
)
469472

470473
await session.close()

0 commit comments

Comments
 (0)