什么是在Twilio Lookup api中提取电话号码类型的正确python查询,为什么?

问题描述 投票:0回答:1

基于Twilio查找文档https://www.twilio.com/docs/lookup/api?code-sample=code-carrier-lookup-with-e164-formatted-number&code-language=Python&code-sdk-version=6.x我假设以下内容:

phone_number = client.lookups.phone_numbers('+19234567890').fetch(type=['carrier'])

返回:

{
  "caller_name": null,
  "carrier": {
    "error_code": null,
    "mobile_country_code": "123",
    "mobile_network_code": "987",
    "name": "verizon",
    "type": "mobile"
  },
  "country_code": "US",
  "national_format": "(923) 456-7890",
  "phone_number": "+19234567890",
  "add_ons": null,
  "url": "https://lookups.twilio.com/v1/PhoneNumbers/+19234567890?Type=carrier"
}

为什么

print(phone_number['carrier']['type'])

返回错误TypeError:'PhoneNumberInstance'对象不可下标

但是:

print(phone_number.carrier['type'])

正确返回“手机”

python-3.x twilio twilio-api
1个回答
0
投票

Twilio开发人员推广人员在这里。

文档显示了API返回的完整JSON响应。但是,我们构建了库来返回可以调用常规方法的对象。因此,phone_number对象返回到此处:

phone_number = client.lookups.phone_numbers('+19234567890').fetch(type=['carrier'])

已经根据我们的API定义为carriercountry_codenational_format之类的方法定义了。

另一方面,我认为我们不必为该载体对象定义一个定义,因此它作为字典返回。因此,要查看载体上的字段,您需要使用索引([])运算符。

您可以检查the definition of the phone_number object on GitHub here

© www.soinside.com 2019 - 2024. All rights reserved.