邮政编码api v2的非官方python包装
postcodeapi的Python项目详细描述
postcodeapi是邮政编码api的非官方python包装器 第2版。
安装和使用
安装
postcodeapi可以通过运行pip install postcodeapi来安装。
使用量
下面是一个如何使用api客户端的示例。首先初始化 使用api密钥的客户端,然后使用四个getter中的一个 方法获取所需的数据。它们都返回实际的json 已转换为python字典的响应。
# Import the PostcodeAPIClientfrompostcodeapi.clientimportPostcodeAPIClient# Initialize a client with your API keyclient=PostcodeAPIClient(api_key="YOUR_API_KEY")# Fetch a list of addresses (for a given postal_code and number)# The postal_code and number parameters are optional# The number parameter only works together with postal_codedata=client.get_all_addresses(postal_code="5038EA",number=19)addresses=data["results"]# List of addressesnext_id=data["next"]# Next ID to search from (used for pagination)# Fetch a single addressaddress=client.get_address(address_id="0855200000046355")# Fetch a list of postal codes (within a specific area)# The area parameter is optionaldata=client.get_all_postal_codes(area="5038")postal_codes=data["results"]# List of postal codesnext_postal_code=data["next"]# Next postal code to search from (used for pagination)# Fetch a single postal codepostal_code=client.get_postal_code("5038EA")
异常
可能会发生5种异常:
- NoAccessException,当当前帐户没有 具有执行操作所需的权限;
- ResourceNotFoundException,当返回 状态代码是404。仅限于get_地址和 get_邮政编码方法;
- HouseNumberRequiresPostalCodeException,当 房屋号码已给出,但没有邮政编码。仅限于 获取所有地址方法;
- InvalidPostalCodeException,当一封无效的邮件 给出代码;
- LimitExceededException,当太多时发生 网络请求或已超过限制
文档
有关返回的数据的更多信息,请参阅 是official API documentation。它是用荷兰语写的。
运行测试
要运行测试,请确保通过运行 pipenv install --dev,然后运行