Skip to content

Commit

Permalink
Misc fixes + bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Poshy163 committed Jul 19, 2024
1 parent d5113fc commit d7c777f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
20 changes: 8 additions & 12 deletions SharesightAPI/SharesightAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def __init__(self, client_id, client_secret, authorization_code, redirect_uri, t
self.token_expiry = 1800
self.access_token = None
self.refresh_token = None
self.load_auth_code = None
self.token_expiry = None
self.debugging = debugging
self.load_auth_constuctor = authorization_code

async def get_token_data(self):
if self.debugging:
logging.basicConfig(level=logging.DEBUG)
self.access_token, self.refresh_token, self.token_expiry, self.authorization_code = await self.load_tokens()
self.access_token, self.refresh_token, self.token_expiry, self.load_auth_code = await self.load_tokens()

async def validate_token(self):

if self.authorization_code is None:
self.authorization_code = self.load_auth_constuctor
if self.authorization_code is None or self.authorization_code == "":
self.authorization_code = self.load_auth_code

current_time = time.time()

Expand All @@ -57,7 +57,7 @@ async def validate_token(self):
return self.access_token

async def refresh_access_token(self):
await self.get_token_data() # Get current token data when refreshing
await self.get_token_data()
payload = {
'grant_type': 'refresh_token',
'refresh_token': self.refresh_token,
Expand All @@ -72,7 +72,7 @@ async def refresh_access_token(self):
if response.status == 200:
token_data = await response.json()
if self.debugging:
logger.info(token_data)
logger.info(f"Refresh_access_token response: {token_data}")
self.access_token = token_data['access_token']
self.refresh_token = token_data['refresh_token']
self.token_expiry = time.time() + token_data.get('expires_in', 1800)
Expand All @@ -85,10 +85,6 @@ async def refresh_access_token(self):

async def get_access_token(self):
current_time = time.time()
logger.info(f"code:{self.authorization_code}")
logger.info(f"code:{self.redirect_uri}")
logger.info(f"code:{self.client_id}")
logger.info(f"code:{self.client_secret}")
payload = {
'grant_type': 'authorization_code',
'code': self.authorization_code,
Expand All @@ -103,10 +99,10 @@ async def get_access_token(self):
async with session.post(self.token_url, data=json.dumps(payload), headers=headers) as response:
if response.status == 200:
token_data = await response.json()
logger.info(f"TOKEN DATA {token_data}")
if self.debugging:
logger.info(token_data)
logger.info(f"get_access_token response: {token_data}")
self.access_token = token_data['access_token']
self.refresh_token = token_data['refresh_token']
self.token_expiry = current_time + token_data.get('expires_in', 1800)
await self.save_tokens()
return self.access_token
Expand Down
47 changes: 24 additions & 23 deletions pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,37 @@ async def main():

sharesight = SharesightAPI.SharesightAPI(client_id, client_secret, authorization_code, redirect_uri, token_url,
api_url_base, token_file, True)
while True:
await sharesight.get_token_data()
access_token = await sharesight.validate_token()

await sharesight.get_token_data()
access_token = await sharesight.validate_token()
# Choose endpoint version
endpoint_list_version = "v2"

# Choose endpoint version
endpoint_list_version = "v2"
combined_dict = {}

combined_dict = {}
if endpoint_list_version == "v2":
for endpoint in v2_endpoint_list:
print(f"\nCalling {endpoint}")
response = await sharesight.get_api_request(endpoint, endpoint_list_version, access_token)
combined_dict = await merge_dicts(combined_dict, response)
elif endpoint_list_version == "v3":
for endpoint in v3_endpoint_list:
print(f"\nCalling {endpoint}")
response = await sharesight.get_api_request(endpoint, endpoint_list_version, access_token)
combined_dict = await merge_dicts(combined_dict, response)

if endpoint_list_version == "v2":
for endpoint in v2_endpoint_list:
print(f"\nCalling {endpoint}")
response = await sharesight.get_api_request(endpoint, endpoint_list_version, access_token)
combined_dict = await merge_dicts(combined_dict, response)
elif endpoint_list_version == "v3":
for endpoint in v3_endpoint_list:
print(f"\nCalling {endpoint}")
response = await sharesight.get_api_request(endpoint, endpoint_list_version, access_token)
combined_dict = await merge_dicts(combined_dict, response)
# Write the combined dictionary to an output.json file which is saved to the current directory
async with aiofiles.open('output.json', 'w') as outfile:
await outfile.write(json.dumps(combined_dict, indent=1))

# Write the combined dictionary to an output.json file which is saved to the current directory
async with aiofiles.open('output.json', 'w') as outfile:
await outfile.write(json.dumps(combined_dict, indent=1))
# Do something with the response json
print(f"\nYour name is " + combined_dict.get("user", {}).get("name"))

# Do something with the response json
print(f"\nYour name is " + combined_dict.get("user", {}).get("name"))
value = combined_dict.get("value")

value = combined_dict.get("value")

print(f"\nProfile Value is ${value} AUD")
print(f"\nProfile Value is ${value} AUD")
await asyncio.sleep(60)


asyncio.run(main())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="SharesightAPI",
version="1.1.7.3",
version="1.1.8",
author="Joshua Leaper",
author_email="poshernater163@gmail.com",
description="A Python library to access your sharesight portfolio information",
Expand Down

0 comments on commit d7c777f

Please sign in to comment.