Skip to main content

A Python client for the Canada Post AddressComplete API

Project description

AddressComplete

A Python client library for the Canada Post AddressComplete API. This library provides a simple interface for address autocomplete and validation using the Canada Post AddressComplete service.

Features

  • Address Search: Find address suggestions based on search terms
  • Address Retrieval: Retrieve detailed address information using address IDs
  • Error Handling: Comprehensive error handling with specific exception types for different API error codes
  • Easy to Use: Simple, intuitive API design

Installation

Install from PyPI (when available):

pip install AddressComplete

Or install from source:

git clone https://github.com/dellwood546/AddressComplete.git
cd AddressComplete
pip install .

Requirements

  • Python 3.7 or higher
  • requests library

Getting Started

To use AddressComplete, you'll need a Canada Post AddressComplete API key. You can obtain one from Canada Post.

Basic Usage

from addresscomplete import AddressComplete

# Initialize the client with your API key
client = AddressComplete("your-api-key-here")

# Search for addresses
results = client.find("123 Main St, Toronto")
print(results)

# Retrieve detailed address information
address_id = "CA|CP|ENG|3X1-R2J"
details = client.retrieve(address_id)
print(details)

Advanced Usage

from addresscomplete import AddressComplete, FindError, RetrieveError

client = AddressComplete("your-api-key-here")

try:
    # Search with custom parameters
    results = client.find(
        search_term="123 Main Street",
        country="CAN",
        max_suggestions=5,
        language_preference="en"
    )
    
    # Process results
    if results.get("Items"):
        for item in results["Items"]:
            print(f"ID: {item.get('Id')}, Description: {item.get('Description')}")
            
            # Retrieve full address details
            if item.get("Id"):
                try:
                    details = client.retrieve(item["Id"])
                    print(f"Full address: {details}")
                except RetrieveError as e:
                    print(f"Error retrieving address: {e}")
                    
except FindError as e:
    print(f"Error finding addresses: {e}")

API Reference

AddressComplete(api_key)

Initialize an AddressComplete client.

Parameters:

  • api_key (str): Your Canada Post AddressComplete API key

find(search_term, country="CAN", max_suggestions=10, language_preference="en")

Find address suggestions based on a search term.

Parameters:

  • search_term (str): The address search term
  • country (str, optional): The country code (default: "CAN")
  • max_suggestions (int, optional): Maximum number of suggestions to return (default: 10)
  • language_preference (str, optional): Language preference code, 2 or 4 digits (default: "en")

Returns:

  • dict: JSON response containing address suggestions

Raises:

  • FindError: If the API returns an error

retrieve(id)

Retrieve detailed address information based on an address ID.

Parameters:

  • id (str): The unique identifier for the address (obtained from find())

Returns:

  • dict: JSON response containing detailed address information

Raises:

  • RetrieveError: If the API returns an error

Error Handling

The library provides specific exception types for different error scenarios:

  • FindError: Raised when the find() method encounters an error
  • RetrieveError: Raised when the retrieve() method encounters an error

Both exceptions are subclasses of the base ResponseError class and provide detailed error information based on the API error codes.

License

This project is licensed under the GNU General Public License v3.0 (GPL-3.0). See the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues

If you encounter any issues or have questions, please open an issue on the GitHub repository.

Author

Darian Elwood (dellwood546@gmail.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

addresscomplete-1.0.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

addresscomplete-1.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file addresscomplete-1.0.0.tar.gz.

File metadata

  • Download URL: addresscomplete-1.0.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for addresscomplete-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3a7311a7927396207a96c18d2b778b0fc0247f83ea376af3e16b8c75177426c0
MD5 ec9805eb7e982d7dfb648fc5afb3678b
BLAKE2b-256 23ee4e196ed2e49711ca86411922777e9ec3e01fdb46100e6a1016b6e5a4baba

See more details on using hashes here.

File details

Details for the file addresscomplete-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for addresscomplete-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 528c927d4f56e2df3583625844fd3a0354b5085eb8bed6655e7aa2829e00d1e8
MD5 0517e3a4d144e33d24cad3cfd9e810da
BLAKE2b-256 14cc8996d0d88bf788bd5b957c115ebb845c4b401895f2cd2922861f772e0b67

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page