Skip to content

Commit

Permalink
Fix asyncio parse_apple_response
Browse files Browse the repository at this point in the history
  • Loading branch information
Kylmakalle committed May 3, 2024
1 parent 8d9295c commit 3e34e77
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions devicecheck/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
https://github.com/Kylmakalle/devicecheck
"""
__version__ = "1.3.0"
__version__ = "1.3.1"
__author__ = 'Sergey Akentev (@Kylmakalle)'
__license__ = 'MIT'
__copyright__ = 'Copyright 2024 Sergey Akentev'
Expand All @@ -14,6 +14,7 @@
import sys
import typing
import uuid
import json
from time import time

import jwt
Expand Down Expand Up @@ -162,15 +163,14 @@ def __dict__(self) -> dict:
}


def parse_apple_response(response: requests.Response, raise_on_error: bool):
response_text = response.text
def parse_apple_response(response_text: str, response_status_code: int, raise_on_error: bool):
if response_text:
try:
response_json = response.json()
return DataAppleResponse(response.status_code, response_json, raise_on_error)
response_json = json.loads(response_text)
return DataAppleResponse(response_status_code, response_json, raise_on_error)
except:
pass
return HttpAppleResponse(response.status_code, response_text, raise_on_error)
return HttpAppleResponse(response_status_code, response_text, raise_on_error)


class DeviceCheck:
Expand Down Expand Up @@ -271,7 +271,7 @@ def validate_device_token(self, token: str, *args, **kwargs) -> HttpAppleRespons
}

result = self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result, self.raise_on_error)
return parse_apple_response(result.text, result.status_code, self.raise_on_error)

def query_two_bits(self, token: str, *args, **kwargs) -> DataAppleResponse:
"""
Expand All @@ -289,7 +289,7 @@ def query_two_bits(self, token: str, *args, **kwargs) -> DataAppleResponse:
'device_token': token
}
result = self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result, self.raise_on_error)
return parse_apple_response(result.text, result.status_code, self.raise_on_error)

def update_two_bits(self, token: str, bit_0: [bool, int] = None, bit_1: [bool, int] = None, *args,
**kwargs) -> HttpAppleResponse:
Expand Down Expand Up @@ -317,7 +317,7 @@ def update_two_bits(self, token: str, bit_0: [bool, int] = None, bit_1: [bool, i
payload['bit1'] = bool(bit_1)

result = self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result, self.raise_on_error)
return parse_apple_response(result.text, result.status_code, self.raise_on_error)

def get_request_url(self, is_dev_env: bool = None):
"""
Expand Down
12 changes: 6 additions & 6 deletions devicecheck/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ async def validate_device_token(self, token: str, *args, **kwargs) -> HttpAppleR
'device_token': token
}

result = await self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result, self.raise_on_error)
result_status_code, result_text = await self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result_text, result_status_code, self.raise_on_error)

async def query_two_bits(self, token: str, *args, **kwargs) -> DataAppleResponse:
"""
Expand All @@ -48,8 +48,8 @@ async def query_two_bits(self, token: str, *args, **kwargs) -> DataAppleResponse
'transaction_id': get_transaction_id(),
'device_token': token
}
result = await self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result, self.raise_on_error)
result_status_code, result_text = await self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result_text, result_status_code, self.raise_on_error)

async def update_two_bits(self, token: str, bit_0: [bool, int] = None, bit_1: [bool, int] = None, *args,
**kwargs) -> HttpAppleResponse:
Expand All @@ -76,8 +76,8 @@ async def update_two_bits(self, token: str, bit_0: [bool, int] = None, bit_1: [b
if bit_1 is not None:
payload['bit1'] = bool(bit_1)

result = await self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result, self.raise_on_error)
result_status_code, result_text = await self._request(self.get_request_url(), endpoint, payload, *args, **kwargs)
return parse_apple_response(result_text, result_status_code, self.raise_on_error)

async def _request(self, url, endpoint, payload, retrying_env=False, *args, **kwargs):
log.debug(f'Sending request to {url}/{endpoint} with data {payload}')
Expand Down

0 comments on commit 3e34e77

Please sign in to comment.