Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active April 25, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quonic/77bd9f78dc45456ba002f5ab7dffdb82 to your computer and use it in GitHub Desktop.
Save quonic/77bd9f78dc45456ba002f5ab7dffdb82 to your computer and use it in GitHub Desktop.
Untested: This should update the exchange rates of the Currency entities. Does require an AutoTask account to have access to multi-currencies.
# MIT license
# Created by Quonic on 4/23/2019
import atws
import getpass
import pprint
import keyring
import requests
def save_creds(creds_username, creds_password):
keyring.set_password("Autotask_RateUpdate", creds_username, creds_password)
def read_creds(creds_username, creds_password=None):
if creds_password is not None:
try:
creds_password = keyring.get_password('Autotask_RateUpdate', creds_username)
except:
creds_password = getpass.getpass()
save_creds(creds_username, creds_password)
else:
creds_password = read_creds(creds_username)
return creds_password
def update_currency(fts_username, fts_password=None):
if fts_password is None:
fts_password = read_creds(fts_username)
pp = pprint.PrettyPrinter(indent=4)
# Connect to AutoTask
at = atws.connect(username=fts_username, password=fts_password)
# Get the resource from the login email address
query_currencies = atws.Query('Currency')
query_currencies.WHERE('Active', query_currencies.Equals, 1)
currencies = at.query(query_currencies).fetch_all()
base_name = [currency for currency in currencies if currency.IsInternalCurrency]
url = "https://api.exchangeratesapi.io/latest?base=" + base_name[0]
exchange_rates = requests.get(url=url).json()
rates_to_update = [setattr(currency, 'ExchangeRate', exchange_rates['rates'][currency.ExchangeRate])
for currency in currencies
if not currency.IsInternalCurrency]
updated_rates = at.update(rates_to_update)
print("### Updated Exchange Rates ###")
pp.pprint(updated_rates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment