Refresh Xero OAuth token (#16)

* WIP - sending refreshed token back

* Small cleanup
This commit is contained in:
jbirddog 2022-10-25 14:10:50 -04:00 committed by GitHub
parent f790e3eae8
commit a6b31a8442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 3 deletions

View File

@ -164,8 +164,13 @@ class CreateInvoice:
client_id = config["XERO_CLIENT_ID"]
client_secret = config["XERO_CLIENT_SECRET"]
# this should be called token_set to match the docs
access_token = json.loads(self.access_token)
# need a mutable "store" to appease the libs
token_store = {}
token_store_key = "token"
api_client = ApiClient(
Configuration(
debug=True,
@ -179,12 +184,16 @@ class CreateInvoice:
@api_client.oauth2_token_getter
def obtain_xero_oauth2_token():
"""Obtain_xero_oauth2_token."""
return access_token
return token_store[token_store_key]
@api_client.oauth2_token_saver
def store_xero_oauth2_token(token):
"""Store_xero_oauth2_token."""
access_token = token # noqa
token_store[token_store_key] = token # noqa
store_xero_oauth2_token(access_token)
api_client.refresh_oauth2_token()
api_instance = AccountingApi(api_client)
summarize_errors = "True"
@ -223,7 +232,11 @@ class CreateInvoice:
created_invoices = api_instance.create_invoices(
xero_tenant_id, invoices, summarize_errors, unitdp
)
response = json.dumps(serialize(created_invoices))
response = json.dumps({
"api_response": serialize(created_invoices),
"refreshed_token_set": obtain_xero_oauth2_token(),
"auth": "xero/OAuth",
})
status = 200
except Exception as e:
# TODO better error logging/reporting in debug