11package aklapi
22
33import (
4+ "context"
45 "encoding/json"
56 "errors"
6- "log"
7+ "log/slog "
78 "net/http"
89 "strconv"
910 "time"
@@ -36,19 +37,19 @@ func (s Address) String() string {
3637}
3738
3839// AddressLookup is a convenience function to get addresses.
39- func AddressLookup (addr string ) (* AddrResponse , error ) {
40- return MatchingPropertyAddresses (& AddrRequest {SearchText : addr , PageSize : 10 })
40+ func AddressLookup (ctx context. Context , addr string ) (* AddrResponse , error ) {
41+ return MatchingPropertyAddresses (ctx , & AddrRequest {SearchText : addr , PageSize : 10 })
4142}
4243
4344// MatchingPropertyAddresses wrapper around the AKL Council API.
44- func MatchingPropertyAddresses (addrReq * AddrRequest ) (* AddrResponse , error ) {
45+ func MatchingPropertyAddresses (ctx context. Context , addrReq * AddrRequest ) (* AddrResponse , error ) {
4546 cachedAr , ok := addrCache .Lookup (addrReq .SearchText )
4647 if ok {
47- log . Printf ( " cached address result: %q " , cachedAr )
48+ slog . DebugContext ( ctx , "found cached address result" , "addr " , cachedAr )
4849 return cachedAr , nil
4950 }
5051
51- req , err := http .NewRequest ( "GET" , addrURI , nil )
52+ req , err := http .NewRequestWithContext ( ctx , http . MethodGet , addrURI , nil )
5253 if err != nil {
5354 return nil , err
5455 }
@@ -66,7 +67,7 @@ func MatchingPropertyAddresses(addrReq *AddrRequest) (*AddrResponse, error) {
6667 return nil , err
6768 }
6869 defer resp .Body .Close ()
69- log . Printf ( "address call complete in %s " , time .Since (start ))
70+ slog . DebugContext ( ctx , "address call complete" , "duration " , time .Since (start ))
7071
7172 if resp .StatusCode != http .StatusOK {
7273 return nil , errors .New ("address API returned status code: " + strconv .Itoa (resp .StatusCode ))
@@ -82,8 +83,8 @@ func MatchingPropertyAddresses(addrReq *AddrRequest) (*AddrResponse, error) {
8283 return & apiResp , nil
8384}
8485
85- func oneAddress (addr string ) (* Address , error ) {
86- resp , err := AddressLookup (addr )
86+ func oneAddress (ctx context. Context , addr string ) (* Address , error ) {
87+ resp , err := AddressLookup (ctx , addr )
8788 if err != nil {
8889 return nil , err
8990 }
0 commit comments