Skip to content

Commit 2ba078d

Browse files
committed
refactor: rename mixin classes for clarity
- Updated ApiModelMixin to inherit from ExtrasMixin. - Cleaned up AddressComponents and other models to just inherit from ApiModelMixin. - removing unnecessary print statements.
1 parent cce9350 commit 2ba078d

File tree

4 files changed

+183
-124
lines changed

4 files changed

+183
-124
lines changed

examples/list_api_example.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
Example script demonstrating the List API functionality.
3+
4+
This script shows how to use the List API to manage lists of addresses
5+
and geocode them in batch.
6+
"""
7+
8+
from geocodio import GeocodioClient
9+
from dotenv import load_dotenv
10+
import os
11+
import json
12+
from dataclasses import asdict
13+
14+
# Load environment variables from .env file
15+
load_dotenv()
16+
17+
# Initialize the client with your API key
18+
client = GeocodioClient(os.getenv("GEOCODIO_API_KEY"))
19+
20+
# Create a new list
21+
print("\nCreating a new list...")
22+
new_list = client.create_list(format_="{{A}}")
23+
print(f"Created list with ID: {new_list.list.id}")
24+
print(f"List name: {new_list.list.name}")
25+
print(f"List description: {new_list.list.description}")
26+
print(f"Number of items: {new_list.list.item_count}")
27+
28+
# Get all lists
29+
print("\nRetrieving all lists...")
30+
lists = client.get_lists()
31+
print(f"Found {len(lists.lists)} lists:")
32+
for list_obj in lists.lists:
33+
print(f" - {list_obj.name} (ID: {list_obj.id}, Items: {list_obj.item_count})")
34+
35+
# Get a specific list
36+
list_id = new_list.list.id
37+
print(f"\nRetrieving list with ID: {list_id}...")
38+
list_details = client.get_list(list_id)
39+
print(f"List name: {list_details.list.name}")
40+
print(f"List description: {list_details.list.description}")
41+
print(f"Number of items: {list_details.list.item_count}")
42+
print("Items:")
43+
for item in list_details.list.items:
44+
print(f" - {item.query} (ID: {item.id})")
45+
46+
# Update the list
47+
print("\nUpdating the list...")
48+
updated_list = client.update_list(
49+
list_id=list_id,
50+
name="Updated Example Addresses",
51+
description="An updated list of example addresses"
52+
)
53+
print(f"Updated list name: {updated_list.list.name}")
54+
print(f"Updated list description: {updated_list.list.description}")
55+
56+
# Add more items to the list
57+
print("\nAdding more items to the list...")
58+
added_items = client.add_items_to_list(
59+
list_id=list_id,
60+
items=[
61+
"123 Main St, Chicago, IL",
62+
"456 Oak St, Los Angeles, CA"
63+
]
64+
)
65+
print(f"Added {len(added_items.items)} items:")
66+
for item in added_items.items:
67+
print(f" - {item.query} (ID: {item.id})")
68+
69+
# Geocode all items in the list
70+
print("\nGeocoding all items in the list...")
71+
geocoded_list = client.geocode_list(
72+
list_id=list_id,
73+
fields=["timezone", "cd"]
74+
)
75+
print(f"Geocoded {len(geocoded_list.list.items)} items:")
76+
for item in geocoded_list.list.items:
77+
if item.result:
78+
print(f" - {item.query} => {item.result.formatted_address}")
79+
print(f" Location: {item.result.location.lat}, {item.result.location.lng}")
80+
if item.result.fields and item.result.fields.timezone:
81+
print(f" Timezone: {item.result.fields.timezone.name}")
82+
if item.result.fields and item.result.fields.congressional_districts:
83+
districts = item.result.fields.congressional_districts
84+
for district in districts:
85+
print(f" Congressional District: {district.name}")
86+
else:
87+
print(f" - {item.query} => Not geocoded")
88+
89+
# Remove some items from the list
90+
print("\nRemoving items from the list...")
91+
item_ids = [added_items.items[0].id] # Remove the first added item
92+
client.remove_items_from_list(
93+
list_id=list_id,
94+
item_ids=item_ids
95+
)
96+
print(f"Removed {len(item_ids)} items")
97+
98+
# Get the updated list
99+
print("\nRetrieving the updated list...")
100+
updated_list = client.get_list(list_id)
101+
print(f"Number of items: {updated_list.list.item_count}")
102+
print("Items:")
103+
for item in updated_list.list.items:
104+
print(f" - {item.query} (ID: {item.id})")
105+
106+
# Clean up by deleting the list
107+
print("\nDeleting the list...")
108+
client.delete_list(list_id)
109+
print(f"Deleted list with ID: {list_id}")
110+
111+
# Verify the list was deleted
112+
print("\nVerifying the list was deleted...")
113+
lists = client.get_lists()
114+
list_exists = any(list_obj.id == list_id for list_obj in lists.lists)
115+
print(f"List still exists: {list_exists}")
116+
117+
print("\nList API example completed successfully!")

0 commit comments

Comments
 (0)