Responses
Every method returns a pointer to a struct decoded from the API's JSON. Fields are exported in Studly case: the API's line_1 is Line1 and zip_plus_4_code is ZipPlus4Code. The JSON tag on each field gives the API's own name. KeyLogs() is the exception: it returns the CSV as a string.
Absent and null fields
A field the API may leave out or send as null is a pointer, nil when absent. Slices, maps and any fields are nil instead. Every other field is a plain value.
package main
import (
"context"
"fmt"
"log"
"os"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test",
addresszen.WithManagementKey(os.Getenv("AZN_MANAGEMENT_KEY")))
res, err := client.KeyDetails(context.Background(), "ak_test")
if err != nil {
log.Fatal(err)
}
if limit := res.Result.DailyLimit.Limit; limit != nil {
fmt.Println("daily limit", *limit)
} else {
fmt.Println("no daily limit")
}
}
Request bodies follow the same rule: an optional body field is a pointer, set with new(value), and nil sends nothing. Optional query parameters are plain values in a *Params struct, and a zero value is not sent.
Raw JSON
Call RawJSON() on any response to get the body as the API sent it. Use it to read a field the SDK does not have yet, or to log the response.
package main
import (
"context"
"fmt"
"log"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test")
res, err := client.RetrieveAddress(context.Background(), "usps_Z108703633|123||1119", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(res.RawJSON()))
}
Fields with more than one shape
Some results come in one of several forms. The SDK decodes the one that matches, and the field's type has an As<Type>() method per form, which returns nil for the others. Variant() returns the decoded form for a type switch, and Raw() the JSON as received.
| Field | Type | As methods |
|---|---|---|
AddressVerify() result | VerifyResponseResult | AsUsaVerifyMatch(), or AsUsaVerifyNoMatch() when nothing matches: Match is nil, Count is 0 and the address fields are empty strings |
UsaVerifyMatch Match | UsaVerifyMatchMatch | AsUsaCassVerifiedAddress() for a CASS match in the United States, or AsAddress() for a match elsewhere, such as with Context: "FRA". An Address has a Dataset and a Native record |
Address Native | *NativeRecord | One per dataset, such as AsUspsAddress(), AsBanAddress() or AsMoisAddress() |
FindPlace() hits | PlaceResultHit | AsPlaceSuggestion() |
Place Native | *PlaceNative | AsGeonamesPlace() |
EmailValidation() result | EmailResponseResult | AsEmail(), or AsUnknownEmail() when deliverability cannot be checked |
PhoneNumberValidation() result | PhoneNumberResponseResult | AsPhoneNumber(), or AsInvalidPhoneNumber() |
FindAddress() hits have one shape, so they are plain AddressSuggestion structs.
package main
import (
"context"
"fmt"
"log"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test")
res, err := client.AddressVerify(context.Background(),
addresszen.AddressVerifyBody{Query: "1010 Cauthen Ln, Alamogordo, NM 88310"}, nil)
if err != nil {
log.Fatal(err)
}
switch result := res.Result.Variant().(type) {
case *addresszen.UsaVerifyMatch:
if cass := result.Match.AsUsaCassVerifiedAddress(); cass != nil {
fmt.Println(result.ZipCode, "with confidence", result.Confidence)
}
case *addresszen.UsaVerifyNoMatch:
fmt.Println("no match:", result.MatchInformation)
}
}
Strings or numbers
A few fields arrive as a string from one dataset and a number from another, such as FinanceNumber, CongressionalDistrictNumber and CountyNumber on an Address, and Latitude and Longitude on a Place. These are addresszen.Scalar. String() returns the value as text, Float64() and Int64() parse it, and IsNull() reports a missing value.
New API values
Your integration keeps working. Fields with a fixed set of values, such as Dpv on a CASS match or Result on an email, are plain strings, and their doc comments list the values known when the SDK was built. A value added later arrives unchanged, so compare against the values you handle and fall through for the rest.
Decoding never fails on shape. A field the API adds is ignored but kept in RawJSON(). A field of the wrong type, or a missing one, is left at its zero value. Errors come from HTTP and the network only. See error handling.
Native records
Native on an Address is the raw record from the address's source dataset, with a type named for the dataset, such as BanAddress for the French national address base or MoisAddress for South Korea. The Dataset field on the address says which. A United States (usps) address has no native record, since the address already is the USPS record, so Native is nil. A dataset the SDK does not know yet has a nil Variant(), and Raw() still holds the record, so a new dataset never breaks your integration.
package main
import (
"context"
"fmt"
"log"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test")
res, err := client.RetrieveAddress(context.Background(), "usps_Z108703633|123||1119", nil)
if err != nil {
log.Fatal(err)
}
address := res.Result
switch {
case address.Native == nil:
fmt.Println("No native record for", address.Dataset)
case address.Native.Variant() == nil:
fmt.Println("New dataset:", address.Dataset, string(address.Native.Raw()))
default:
fmt.Printf("%T\n", address.Native.Variant())
}
}
A few native fields have names that start with something other than a Latin letter, such as the Korean 법정읍면동명 on MoisAddress. Go only exports a name that starts with an upper-case letter, so these take an X prefix: X법정읍면동명.
The data sources pages document every dataset's fields.