Skip to content

Commit c97c1a6

Browse files
committed
feat: add type comments
1 parent 1a549f2 commit c97c1a6

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

gamejoltapi.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ class GameJoltDataRequired(Exception):
1818
"""
1919

2020
def __init__(self, key):
21+
# type: (object) -> None
22+
2123
self.key = key
2224
self.message = "Value is required, cannot be None: " + repr(key)
2325
super().__init__(self.message)
2426

27+
2528
class GameJoltDataCollision(Exception):
2629
""" Exception raised when a value cannot be provided along with another.
2730
@@ -30,10 +33,13 @@ class GameJoltDataCollision(Exception):
3033
"""
3134

3235
def __init__(self, keys):
36+
# type: (list) -> None
37+
3338
self.keys = keys
3439
self.message = "Values cannot be used together: " + ", ".join([repr(k) for k in self.keys])
3540
super().__init__(self.message)
3641

42+
3743
class GameJoltAPI:
3844
""" The main Game Jolt API class. Aside from the required arguments, most of the
3945
optional arguments are provided to avoid asking for them in every single method.
@@ -87,6 +93,8 @@ class GameJoltAPI:
8793
If submit the requests or just get the generated URLs from the method calls. Useful to generate URLs for batch requests. Optional, defaults to ``True``."""
8894

8995
def __init__(self, gameId, privateKey, username=None, userToken=None, responseFormat="json", submitRequests=True):
96+
# type: (int, str, str, str, str, bool) -> None
97+
9098
self.__API_URL = "https://api.gamejolt.com/api/game/v1_2"
9199
self.__RETURN_FORMATS = ["json", "keypair", "dump", "xml"]
92100

@@ -121,6 +129,8 @@ def __init__(self, gameId, privateKey, username=None, userToken=None, responseFo
121129
}
122130

123131
def _submit(self, operationUrl, data):
132+
# type: (str, dict) -> dict
133+
124134
orderedData = _OrderedDict()
125135
isBatch = "batch" in operationUrl
126136

@@ -153,12 +163,16 @@ def _submit(self, operationUrl, data):
153163
return finalUrl
154164

155165
def _validateRequiredData(self, data):
166+
# type: (dict) -> bool
167+
156168
for key in data.keys():
157169
if data[key] is None:
158170
raise GameJoltDataRequired(key)
159171
return True
160172

161173
def _getValidData(self, data):
174+
# type: (dict) -> dict
175+
162176
validatedData = {}
163177
if self.responseFormat != "json":
164178
validatedData["format"] = self.responseFormat
@@ -169,11 +183,15 @@ def _getValidData(self, data):
169183
return validatedData
170184

171185
def _processBoolean(self, value):
186+
# type: (bool) -> str
187+
172188
if value is not None:
173189
return str(value).lower()
174190

175191
# Users
176192
def usersFetch(self, username=None, userId=None):
193+
# type: (str, str | int | list) -> dict
194+
177195
"""Returns a user's data.
178196
179197
:param username: The username of the user whose data you'd like to fetch.
@@ -208,6 +226,8 @@ def usersFetch(self, username=None, userId=None):
208226
return self._submit(self.operations["users/fetch"], data)
209227

210228
def usersAuth(self):
229+
# type: () -> dict
230+
211231
"""Authenticates the user's information. This should be done before you make
212232
any calls for the user, to make sure the user's credentials (username and
213233
token) are valid."""
@@ -224,6 +244,8 @@ def usersAuth(self):
224244

225245
# Sessions
226246
def sessionsOpen(self):
247+
# type: () -> dict
248+
227249
"""Opens a game session for a particular user and allows you to tell Game Jolt
228250
that a user is playing your game. You must ping the session to keep it active
229251
and you must close it when you're done with it.
@@ -244,6 +266,8 @@ def sessionsOpen(self):
244266
return self._submit(self.operations["sessions/open"], data)
245267

246268
def sessionsPing(self, status=None):
269+
# type: (str) -> dict
270+
247271
"""Pings an open session to tell the system that it's still active. If the session
248272
hasn't been pinged within 120 seconds, the system will close the session and you
249273
will have to open another one. It's recommended that you ping about every 30
@@ -279,6 +303,8 @@ def sessionsPing(self, status=None):
279303
return self._submit(self.operations["sessions/ping"], data)
280304

281305
def sessionsCheck(self):
306+
# type: () -> dict
307+
282308
"""Checks to see if there is an open session for the user. Can be used to see
283309
if a particular user account is active in the game.
284310
@@ -299,6 +325,8 @@ def sessionsCheck(self):
299325
return self._submit(self.operations["sessions/check"], data)
300326

301327
def sessionsClose(self):
328+
# type: () -> dict
329+
302330
"""Closes the active session."""
303331

304332
# Required data
@@ -313,6 +341,8 @@ def sessionsClose(self):
313341

314342
# Scores
315343
def scoresFetch(self, limit=None, tableId=None, guest=None, betterThan=None, worseThan=None, thisUser=False):
344+
# type: (int, int, str, int, int, bool) -> dict
345+
316346
"""Returns a list of scores either for a user or globally for a game.
317347
318348
:param limit: The number of scores you'd like to return.
@@ -364,6 +394,8 @@ def scoresFetch(self, limit=None, tableId=None, guest=None, betterThan=None, wor
364394
return self._submit(self.operations["scores/fetch"], data)
365395

366396
def scoresTables(self):
397+
# type: () -> dict
398+
367399
"""Returns a list of high score tables for a game."""
368400

369401
# Required data
@@ -375,6 +407,8 @@ def scoresTables(self):
375407
return self._submit(self.operations["scores/tables"], data)
376408

377409
def scoresAdd(self, score, sort, tableId=None, guest=None, extraData=None):
410+
# type: (str, int, int, str, str) -> dict
411+
378412
"""Adds a score for a user or guest.
379413
380414
:param score: This is a string value associated with the score. Example: ``"500 Points"``
@@ -421,6 +455,8 @@ def scoresAdd(self, score, sort, tableId=None, guest=None, extraData=None):
421455
return self._submit(self.operations["scores/add"], data)
422456

423457
def scoresGetRank(self, sort, tableId=None):
458+
# type: (int, int) -> dict
459+
424460
"""Returns the rank of a particular score on a score table.
425461
426462
:param sort: This is a numerical sorting value that is represented by a rank on the score table.
@@ -452,6 +488,8 @@ def scoresGetRank(self, sort, tableId=None):
452488

453489
# Trophies
454490
def trophiesFetch(self, achieved=None, trophyId=None):
491+
# type: (bool, str | int | list) -> dict
492+
455493
"""Returns one trophy or multiple trophies, depending on the parameters passed in.
456494
457495
:param achieved: Pass in ``True`` to return only the achieved trophies for a user. Pass in ``False`` to return only trophies the user hasn't achieved. Leave blank to retrieve all trophies.
@@ -484,6 +522,8 @@ def trophiesFetch(self, achieved=None, trophyId=None):
484522
return self._submit(self.operations["trophies/fetch"], data)
485523

486524
def trophiesAddAchieved(self, trophyId):
525+
# type: (int) -> dict
526+
487527
"""Sets a trophy as achieved for a particular user.
488528
489529
:param trophyId: The ID of the trophy to add for the user.
@@ -503,6 +543,8 @@ def trophiesAddAchieved(self, trophyId):
503543
return self._submit(self.operations["trophies/add-achieved"], data)
504544

505545
def trophiesRemoveAchieved(self, trophyId):
546+
# type: (int) -> dict
547+
506548
"""Remove a previously achieved trophy for a particular user.
507549
508550
:param trophyId: The ID of the trophy to remove from the user.
@@ -523,6 +565,8 @@ def trophiesRemoveAchieved(self, trophyId):
523565

524566
# Data Storage
525567
def dataStoreSet(self, key, data, globalData=False):
568+
# type: (str, str, bool) -> dict
569+
526570
"""Sets data in the data store.
527571
528572
:param key: The key of the data item you'd like to set.
@@ -569,6 +613,8 @@ def dataStoreSet(self, key, data, globalData=False):
569613
return self._submit(self.operations["data-store/set"], data)
570614

571615
def dataStoreUpdate(self, key, operation, value, globalData=False):
616+
# type: (str, str, str, bool) -> dict
617+
572618
"""Updates data in the data store.
573619
574620
:param key: The key of the data item you'd like to update.
@@ -630,6 +676,8 @@ def dataStoreUpdate(self, key, operation, value, globalData=False):
630676
return self._submit(self.operations["data-store/update"], data)
631677

632678
def dataStoreRemove(self, key, globalData=False):
679+
# type: (str, bool) -> dict
680+
633681
"""Removes data from the data store.
634682
635683
:param key: The key of the data item you'd like to remove.
@@ -668,6 +716,8 @@ def dataStoreRemove(self, key, globalData=False):
668716
return self._submit(self.operations["data-store/remove"], data)
669717

670718
def dataStoreFetch(self, key, globalData=False):
719+
# type: (str, bool) -> dict
720+
671721
"""Returns data from the data store.
672722
673723
:param key: The key of the data item you'd like to fetch.
@@ -706,6 +756,8 @@ def dataStoreFetch(self, key, globalData=False):
706756
return self._submit(self.operations["data-store/fetch"], data)
707757

708758
def dataStoreGetKeys(self, pattern=None, globalData=False):
759+
# type: (str, bool) -> dict
760+
709761
"""Returns either all the keys in the game's global data store, or all the keys in a user's data store.
710762
711763
:param pattern: The pattern to apply to the key names in the data store.
@@ -750,6 +802,8 @@ def dataStoreGetKeys(self, pattern=None, globalData=False):
750802

751803
# Friends
752804
def friends(self):
805+
# type: () -> dict
806+
753807
"""Returns the list of a user's friends."""
754808

755809
# Required data
@@ -765,6 +819,8 @@ def friends(self):
765819

766820
# Time
767821
def time(self):
822+
# type: () -> dict
823+
768824
"""Returns the time of the Game Jolt server."""
769825

770826
# Required data
@@ -777,6 +833,8 @@ def time(self):
777833

778834
# Batch Calls
779835
def batch(self, requests=[], parallel=None, breakOnError=None):
836+
# type: (list[str], bool, bool) -> dict
837+
780838
"""A batch request is a collection of sub-requests that enables developers to send multiple API calls with one HTTP request.
781839
782840
:param requests: An list of sub-request URLs. Each request will be executed and the responses of each one will be returned in the payload.

0 commit comments

Comments
 (0)