app: fixed bug and better error message

This commit is contained in:
Swapnil 2024-01-30 21:10:04 +05:30
parent bf8b33edde
commit 327896abcc
Signed by: swapnil
GPG Key ID: 58029C48BB100574

View File

@ -28,7 +28,7 @@ def home():
if is_logged_in():
token_id = session.get('token_id')
resp = requests.get(f'{database_url}/token/{token_id}')
resp.raise_for_status()
raise_for_status(resp)
resp_json = resp.json()
token = resp_json['token']
@ -89,7 +89,7 @@ def oauth_redirect():
}
)
response.raise_for_status()
raise_for_status(response)
token = response.json()
@ -97,9 +97,13 @@ def oauth_redirect():
token['refresh_token'] = 'N/A'
token['expires_in'] = 3600
user_info = requests.get('https://api.github.com/user', headers={
'Authorization': f'Bearer {token.get("access_token")}'
}).json()
# Save tokens for later use
token_id = save_token(
token.get('email'), # for inoreader it's userEmail
user_info.get('email'), # for inoreader it's userEmail
token.get('access_token'),
token.get('refresh_token'),
token.get('expires_in')
@ -140,7 +144,7 @@ def submit_readwise_api():
}, json={
'readwise_api_key': request.form.get('readwise_api_key')
})
response.raise_for_status()
raise_for_status(response)
return redirect(url_for('home'))
@ -150,7 +154,7 @@ def is_logged_in():
return False
response = requests.get(f'{database_url}/token/{token_id}')
response.raise_for_status()
raise_for_status(response)
resp_json = response.json()
token = resp_json['token']
@ -169,14 +173,16 @@ def save_token(email, access_token, refresh_token, expiration_seconds):
'expiration_seconds': expiration_seconds
}
)
# response.raise_for_status()
if response.status_code not in range(200, 300):
raise Exception(f'HTTPError: {response.status_code} \n Message: {response.json()}')
raise_for_status(response)
return response.json().get('id')
def set_session_token_id(token_id):
session['token_id'] = token_id
def raise_for_status(response):
if response.status_code not in range(200, 300):
raise Exception(f'HTTPError: {response.status_code} \n Message: {response.json()}')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5000)