Can't query daily usage records for subaccounts
oleg-agapov opened this issue · comments
Issue Summary
Using this SDK gives me 404 error when I try to query UsageRecords for subaccounts. Interestingly, when I try to use pre-defined methods, like last_month
it works, but it doesn't for daily
or generic list
.
I checked that the mentioned subaccount is under my main account, so I'm using the correct credentials.
Code Snippet
account_sid = <TWILIO_ACCOUNT_SID> # main account SID
auth_token = <TWILIO_AUTH_TOKEN> # main account token
subaccount_sid = 'ACxxxxx' # target subaccount
client = Client(
username=account_sid,
password=auth_token,
account_sid=subaccount_sid,
)
# this piece works fine
last_month = client.usage.records.last_month.list(limit=3)
for record in last_month:
print(record.start_date, record.end_date, record.category)
# output:
# 2024-03-01 2024-03-31 wireless-usage
# 2024-03-01 2024-03-31 pv-basic-rooms
# 2024-03-01 2024-03-31 ip-messaging-data-storage
# however, the same timeframe for pure `list` method gives me an error:
usage_data = client.usage.records.list(
start_date=date(2024, 3, 1),
end_date=date(2024, 3, 31),
limit=3
)
for record in usage_data:
print(record.start_date, record.end_date, record.category)
Exception/Log
TwilioException Traceback (most recent call last)
/tmp/ipykernel_12/2713748063.py in <cell line: 30>()
28 #print(UsageRecord(record, subaccount_sid).to_dict())
29
---> 30 usage_data = client.usage.records.list(
31 start_date=date(2024, 3, 1),
32 end_date=date(2024, 3, 31),
~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/twilio/rest/api/v2010/account/usage/record/__init__.py in list(self, category, start_date, end_date, include_subaccounts, limit, page_size)
569 """
570 return list(
--> 571 self.stream(
572 category=category,
573 start_date=start_date,
~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/twilio/rest/api/v2010/account/usage/record/__init__.py in stream(self, category, start_date, end_date, include_subaccounts, limit, page_size)
492 """
493 limits = self._version.read_limits(limit, page_size)
--> 494 page = self.page(
495 category=category,
496 start_date=start_date,
~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/twilio/rest/api/v2010/account/usage/record/__init__.py in page(self, category, start_date, end_date, include_subaccounts, page_token, page_number, page_size)
655
656 response = self._version.page(method="GET", uri=self._uri, params=data)
--> 657 return RecordPage(self._version, response, self._solution)
658
659 async def page_async(
~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/twilio/base/page.py in __init__(self, version, response, solution)
29
30 def __init__(self, version, response: Response, solution={}):
---> 31 payload = self.process_response(response)
32
33 self._version = version
~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/twilio/base/page.py in process_response(cls, response)
60 """
61 if response.status_code != 200:
---> 62 raise TwilioException("Unable to fetch page", response)
63
64 return json.loads(response.text)
TwilioException: ('Unable to fetch page', HTTP 404 {"code": 20404, "message": "The requested resource /2010-04-01/Accounts/ACxxx/Usage/Records.json was not found", "more_info": "https://www.twilio.com/docs/errors/20404", "status": 404})
Technical details:
- twilio-python version: 9.0.4
- python version: 3.9
Update.
After talking to Twilio's tech support I figured out the error.
Basically, you only can request statistics for the timeframe after the subaccount was created. In my case, the subaccount in the example was created in April, so requesting data for March gave me an error.
I adjusted my code to request data only where start_date >= date_created
.
I hope this is resolved now
Yup, resolved by specifying only dates when subaccount was created. Closing the issue!