11from __future__ import annotations
2- from typing import Any , Callable , Dict , Optional
2+ from typing import Any , Dict , Optional
33
44from dataclasses import dataclass
5+ from datetime import datetime
6+ from .utils import try_get_from_dict
7+
8+
9+ class GeocachingSettings :
10+ """Class to hold the Geocaching Api settings"""
11+ fetch_trackables : bool = False
12+ def __init__ (self , fetch_trackables :bool = False ) -> None :
13+ """Initialize settings"""
14+ self .fetch_trackables = fetch_trackables
15+
16+
17+
518
619@dataclass
720class GeocachingUser :
21+ """Class to hold the Geocaching user information"""
822 reference_code : Optional [str ] = None
923 username : Optional [str ] = None
1024 find_count : Optional [int ] = None
@@ -14,27 +28,63 @@ class GeocachingUser:
1428 awarded_favorite_points : Optional [int ] = None
1529
1630 def update_from_dict (self , data : Dict [str , Any ]) -> None :
17- if "username" in data :
18- self .username = data ["username" ]
19- if "referenceCode" in data :
20- self .reference_code = data ["referenceCode" ]
21- if "findCount" in data :
22- self .find_count = data ["findCount" ]
23- if "hideCount" in data :
24- self .hide_count = data ["hideCount" ]
25- if "favoritePoints" in data :
26- self .favorite_points = data ["favoritePoints" ]
27- if "souvenirCount" in data :
28- self .souvenir_count = data ["souvenirCount" ]
29- if "awardedFavoritePoints" in data :
30- self .awarded_favorite_points = data ["awardedFavoritePoints" ]
31-
32- pass
31+ """Update user from the API result"""
32+ self .reference_code = try_get_from_dict (data , "referenceCode" , self .reference_code )
33+ self .username = try_get_from_dict (data , "username" , self .username )
34+ self .find_count = try_get_from_dict (data , "findCount" , self .find_count )
35+ self .hide_count = try_get_from_dict (data , "hideCount" , self .hide_count )
36+ self .favorite_points = try_get_from_dict (data , "favoritePoints" , self .favorite_points )
37+ self .souvenir_count = try_get_from_dict (data , "souvenirCount" , self .souvenir_count )
38+ self .awarded_favorite_points = try_get_from_dict (data , "awardedFavoritePoints" , self .awarded_favorite_points )
39+
40+ @dataclass
41+ class GeocachingTrackable :
42+ """Class to hold the Geocaching trackable information"""
43+ reference_code : Optional [str ] = None
44+ name : Optional [str ] = None
45+ holder : GeocachingUser = None
46+ tracking_number : Optional [str ] = None
47+ kilometers_traveled : Optional [datetime ] = None
48+ current_geocache_code : Optional [str ] = None
49+ current_geocache_name : Optional [str ] = None
50+
51+ def update_from_dict (self , data : Dict [str , Any ]) -> None :
52+ """Update trackble from the API"""
53+ self .reference_code = try_get_from_dict (data , "referenceCode" , self .reference_code )
54+ self .name = try_get_from_dict (data , "name" , self .name )
55+ if data ["holder" ] is not None :
56+ if self .holder is None :
57+ holder = GeocachingUser ()
58+ holder .update_from_dict (data ["holder" ])
59+ else :
60+ holder = None
61+
62+ self .tracking_number = try_get_from_dict (data , "trackingNumber" , self .tracking_number )
63+ self .kilometers_traveled = try_get_from_dict (data , "kilometersTraveled" , self .kilometers_traveled )
64+ self .current_geocache_code = try_get_from_dict (data , "currectGeocacheCode" , self .current_geocache_code )
65+ self .current_geocache_name = try_get_from_dict (data , "currentGeocacheName" , self .current_geocache_name )
3366
3467class GeocachingStatus :
35- user : GeocachingUser = GeocachingUser ()
36- def __init__ (self ) -> None :
37- pass
68+ """Class to hold all account status information"""
69+ user : GeocachingUser = None
70+ trackables : Dict [str , GeocachingTrackable ] = None
71+
72+ def __init__ (self ):
73+ """Initialize GeocachingStatus"""
74+ self .user = GeocachingUser ()
75+ self .trackables = {}
3876
39- def update_from_dict (self , data : Dict [str , Any ]) -> GeocachingStatus :
40- return self
77+ def update_user_from_dict (self , data : Dict [str , Any ]) -> None :
78+ """Update user from the API result"""
79+ self .user .update_from_dict (data )
80+
81+ def update_trackables_from_dict (self , data : Any ) -> None :
82+ """Update trackables from the API result"""
83+ if not any (data ):
84+ pass
85+ for trackable in data :
86+ reference_code = trackable ["referenceCode" ]
87+ if not reference_code in self .trackables .keys ():
88+ self .trackables [reference_code ] = GeocachingTrackable ()
89+ self .trackables [reference_code ].update_from_dict (trackable )
90+
0 commit comments