Skip to content

Commit

Permalink
Merge pull request #60 from peermuellerxw/fix-billing-api
Browse files Browse the repository at this point in the history
fix: adjust api to latest aws billing changes
  • Loading branch information
costastf authored Apr 8, 2024
2 parents 2fe6532 + 5225020 commit bf7ca93
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions awsapilib/billing/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import logging
import time
import json
from datetime import datetime, timedelta

from bs4 import BeautifulSoup as Bfs
Expand Down Expand Up @@ -69,6 +70,7 @@ class Tax(LoggerMixin):
def __init__(self, billing):
self._billing = billing
self._endpoint = f'{self._billing.rest_api}/taxexemption/heritage'
self._endpointV2 = f'{self._billing.rest_api}/taxexemption/heritage.v2'
self._available_country_codes = None

@property
Expand Down Expand Up @@ -117,8 +119,8 @@ def inheritance(self, value: bool):
if self.inheritance == value:
return
self._is_editable()
parameters = {'heritageStatus': 'OptIn' if value else 'OptOut'}
response = self._billing.session.post(self._endpoint, params=parameters)
payload = {'heritageStatus': 'OptIn' if value else 'OptOut'}
response = self._billing.session.post(self._endpointV2, json=payload)
if not response.ok:
self.logger.error(f'Failed to retrieve inheritance state, response: {response.text}')

Expand All @@ -127,6 +129,8 @@ def _is_editable(self):
response = self._billing.session.get(self._endpoint, params=parameters)
if not response.json().get('heritageStatusEditable'):
timestamp = response.json().get('effectiveTimestamp')
if not timestamp:
raise NonEditableSetting(f'API is not enabled: {response.json()}')
unlock_time = (datetime.fromtimestamp(timestamp / 1000) + timedelta(minutes=15))
wait_time = unlock_time - datetime.now()
raise NonEditableSetting(f'API is not enabled for {wait_time} more.')
Expand All @@ -144,21 +148,28 @@ def set_information(self, address, city, postal_code, legal_name, vat_number, co
if country_code not in self.available_country_codes_eu:
raise InvalidCountryCode(f'{country_code} provided is not valid. '
f'Valid ones are {self.available_country_codes_eu}')
payload = {'address': {'addressLine1': address,
'addressLine2': None,
'city': city,
'countryCode': country_code,
'postalCode': postal_code,
'state': state,
},
'authority': {'country': country_code,
'state': None},
'legalName': legal_name,
'localTaxRegistration': False,
'registrationId': vat_number,
}
url = f'{self._billing.rest_api}/taxexemption/eu/vat/information'
response = self._billing.session.put(url, json=payload)
payload = {'linkedAccounts': [ {self.account_id} ],
'taxRegistration': {'address': {'addressLine1': address,
'addressLine2': None,
'city': city,
'countryCode': country_code,
'postalCode': postal_code,
'state': state,
},
'authority': {'country': country_code,
'state': None},
'legalName': legal_name,
'localTaxRegistration': False,
'registrationId': vat_number,
}
}

files = {
'details': ('blob', json.dumps(payload), 'application/json'),
}

url = f'{self._billing.rest_api}/taxexemption/taxregistration.v2'
response = self._billing.session.put(url, files=files)
if not response.ok:
self.logger.error(f'Failed to set information, response: {response.text}')
return response.ok
Expand Down Expand Up @@ -230,7 +241,7 @@ def credit_sharing(self, value: bool):
if self.credit_sharing == value:
return
endpoint = f'{self._billing.rest_api}/sharingpreferences/setcreditsharing'
payload = {'creditEnabled': bool(value)}
payload = {'creditEnabled': bool(value), 'creditShareAccountExceptions': []}
response = self._billing.session.put(endpoint, json=payload)
if not response.ok:
self.logger.error(f'Failed to retrieve credit sharing state, response: {response.text}')
Expand All @@ -243,7 +254,7 @@ def __init__(self, arn, region=None):
self.aws_authenticator = Authenticator(arn)
self.session = self._get_authenticated_session()
self.region = region or self.aws_authenticator.region
self.rest_api = 'https://console.aws.amazon.com/billing/rest/v1.0'
self.rest_api = 'https://us-east-1.console.aws.amazon.com/billing/rest/v1.0'
self._sor_info_ = None
self._payment_instrument_ids = None
self._marketplace_id = None
Expand Down

0 comments on commit bf7ca93

Please sign in to comment.