Skip to content

Commit

Permalink
bulkDetails: handle case when app doesn't exist
Browse files Browse the repository at this point in the history
Now the function returns None if an app doesnt't exists. This does not
apply for details() function, which instead return a RequestError if the
app doesn't exist.
  • Loading branch information
Domenico Iezzi committed Nov 2, 2017
1 parent 7678402 commit 25bfb4a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
26 changes: 21 additions & 5 deletions gpapi/googleplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ def getAuthSubToken(self, email, passwd):
raise LoginError("Auth token not found.")

def _check_response_integrity(self, apps):
"""Like described in issue #18, after some time it seems
that google invalidates the token. And the strange thing is that when
sending requests with an invalid token, it won't throw an error but
it returns empty responses. This is a function used to check if the
content returned is valid (usually a docId field is always present)"""
if any([a['docId'] == '' for a in apps]):
raise LoginError('Unexpected behaviour, probably expired '
'token')
Expand Down Expand Up @@ -295,9 +300,15 @@ def bulkDetails(self, packageNames):
"""Get several apps details from a list of package names.
This is much more efficient than calling N times details() since it
requires only one request.
requires only one request. If an item is not found it returns an empty object
instead of throwing a RequestError('Item not found') like the details() function
packageNames is a list of app ID (usually starting with 'com.')."""
Args:
packageNames (list): a list of app IDs (usually starting with 'com.').
Returns:
a list of dictionaries containing docv1 data, or None
if the app doesn't exist"""

path = "bulkDetails"
req = googleplay_pb2.BulkDetailsRequest()
Expand All @@ -307,9 +318,14 @@ def bulkDetails(self, packageNames):
data.decode("utf-8"),
"application/x-protobuf")
response = message.payload.bulkDetailsResponse
detailsList = [entry.doc for entry in response.entry]
result = list(map(utils.fromDocToDictionary, detailsList))
self._check_response_integrity(result)
result = []
for entry in response.entry:
if not entry.HasField('doc'):
result.append(None)
else:
appDetails = utils.fromDocToDictionary(entry.doc)
self._check_response_integrity([appDetails])
result.append(appDetails)
return result

def browse(self, cat=None, subCat=None):
Expand Down
12 changes: 9 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

args = ap.parse_args()

testApps = ['org.mozilla.firefox']
server = GooglePlayAPI(debug=True)

# LOGIN
Expand Down Expand Up @@ -69,9 +68,16 @@

# BULK DETAILS

print('\nGetting bulkDetails for %s\n' % testApps[0])
testApps = ['org.mozilla.firefox', 'com.non.existing.app']
bulk = server.bulkDetails(testApps)
print(bulk)

print('\nTesting behaviour for non-existing apps\n')
if bulk[1] is not None:
print('bulkDetails should return None for non-existing apps')
sys.exit(1)

print('\nResult from bulkDetails for %s\n' % testApps[0])
print(bulk[0])

# DETAILS
print('\nGetting details for %s\n' % testApps[0])
Expand Down

0 comments on commit 25bfb4a

Please sign in to comment.