diff --git a/examples/us-enrichment-api/secondary-count/main.go b/examples/us-enrichment-api/secondary-count/main.go index 0b07c37..bab2ec8 100644 --- a/examples/us-enrichment-api/secondary-count/main.go +++ b/examples/us-enrichment-api/secondary-count/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" usenrich "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api" "github.com/smartystreets/smartystreets-go-sdk/wireup" @@ -39,7 +40,11 @@ func main() { } fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "secondary") - for s, response := range results { - fmt.Printf("#%d: %+v\n", s, response) + for i, response := range results { + prettyPrinted, err := json.MarshalIndent(response, "", " ") + if err != nil { + panic(err) + } + fmt.Printf("Response %d: %s", i, prettyPrinted) } } diff --git a/examples/us-enrichment-api/secondary/main.go b/examples/us-enrichment-api/secondary/main.go index f508432..6bfbb2b 100644 --- a/examples/us-enrichment-api/secondary/main.go +++ b/examples/us-enrichment-api/secondary/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" usenrich "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api" "github.com/smartystreets/smartystreets-go-sdk/wireup" @@ -39,7 +40,11 @@ func main() { } fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "secondary") - for s, response := range results { - fmt.Printf("#%d: %+v\n", s, response) + for i, response := range results { + prettyPrinted, err := json.MarshalIndent(response, "", " ") + if err != nil { + panic(err) + } + fmt.Printf("Response %d: %s", i, prettyPrinted) } } diff --git a/us-enrichment-api/client_test.go b/us-enrichment-api/client_test.go index 05acf08..eddd24c 100644 --- a/us-enrichment-api/client_test.go +++ b/us-enrichment-api/client_test.go @@ -2,6 +2,7 @@ package us_enrichment import ( "context" + "encoding/json" "errors" "net/http" "testing" @@ -116,8 +117,57 @@ func (f *ClientFixture) TestUniversalLookupUnmarshallingWithNoEtag() { f.So(lookup.Response, should.Equal, []byte(validPrincipalResponse)) } +func (f *ClientFixture) TestSecondaryLookup() { + smartyKey := "123" + f.sender.response = validSecondaryResponse + f.input = &secondaryLookup{Lookup: &Lookup{SmartyKey: smartyKey}} + + ctx := context.WithValue(context.Background(), "key", "value") + err := f.client.sendLookupWithContext(ctx, f.input) + + f.So(err, should.BeNil) + f.So(f.sender.request, should.NotBeNil) + f.So(f.sender.request.Method, should.Equal, "GET") + f.So(f.sender.request.URL.Path, should.Equal, "/lookup/"+smartyKey+"/"+f.input.getDataSet()) + f.So(f.sender.request.Context(), should.Resemble, ctx) + + response := f.input.(*secondaryLookup).Response + var secondaryResponse []*SecondaryResponse + err = json.Unmarshal([]byte(validSecondaryResponse), &secondaryResponse) + secondaryResponse[0].Etag = "ABCDEFG" + f.So(err, should.BeNil) + f.So(response, should.Resemble, secondaryResponse) +} + +func (f *ClientFixture) FocusTestSecondaryCount() { + smartyKey := "123" + f.sender.response = validSecondaryCountResponse + f.input = &secondaryCountLookup{Lookup: &Lookup{SmartyKey: smartyKey}} + + ctx := context.WithValue(context.Background(), "key", "value") + err := f.client.sendLookupWithContext(ctx, f.input) + + f.So(err, should.BeNil) + f.So(f.sender.request, should.NotBeNil) + f.So(f.sender.request.Method, should.Equal, "GET") + f.So(f.sender.request.URL.Path, should.Equal, "/lookup/"+smartyKey+"/"+f.input.getDataSet()+"/"+f.input.getDataSubset()) + f.So(f.sender.request.Context(), should.Resemble, ctx) + + response := f.input.(*secondaryCountLookup).Response + secondaryCountResponse := []*SecondaryCountResponse{ + { + SmartyKey: smartyKey, + Count: 3, + Etag: "ABCDEFG", + }, + } + f.So(response, should.Resemble, secondaryCountResponse) +} + var validFinancialResponse = `[{"smarty_key":"123","data_set_name":"property","data_subset_name":"financial","attributes":{"assessed_improvement_percent":"Assessed_Improvement_Percent","veteran_tax_exemption":"Veteran_Tax_Exemption","widow_tax_exemption":"Widow_Tax_Exemption"}}]` var validPrincipalResponse = `[{"smarty_key":"123","data_set_name":"property","data_subset_name":"principal","attributes":{"1st_floor_sqft":"1st_Floor_Sqft",lender_name_2":"Lender_Name_2","lender_seller_carry_back":"Lender_Seller_Carry_Back","year_built":"Year_Built","zoning":"Zoning"}}]` +var validSecondaryResponse = `[{"smarty_key":"123","root_address":{"secondary_count":10,"smarty_key":"123","primary_number":"3105","street_name":"National Park Service","street_suffix":"Rd","city_name":"Juneau","state_abbreviation":"AK","zipcode":"99801","plus4_code":"8437"},"aliases":[{"smarty_key":"1882749021","primary_number":"3105","street_name":"National Park","street_suffix":"Rd","city_name":"Juneau","state_abbreviation":"AK","zipcode":"99801","plus4_code":"8437"}],"secondaries":[{"smarty_key":"1785903890","secondary_designator":"Apt","secondary_number":"A5","plus4_code":"8437"},{"smarty_key":"696702050","secondary_designator":"Apt","secondary_number":"B1","plus4_code":"8441"}]}]` +var validSecondaryCountResponse = `[{"smarty_key":"123","count":3}]` /**************************************************************************/ diff --git a/us-enrichment-api/response.go b/us-enrichment-api/response.go index 0016226..e7f20b6 100644 --- a/us-enrichment-api/response.go +++ b/us-enrichment-api/response.go @@ -556,40 +556,46 @@ type GeoReferenceAttributes struct { } `json:"place"` } +type RootAddress struct { + SecondaryCount uint64 `json:"secondary_count"` + SmartyKey string `json:"smarty_key"` + PrimaryNumber string `json:"primary_number"` + StreetPredirection string `json:"street_predirection"` + StreetName string `json:"street_name"` + StreetSuffix string `json:"street_suffix"` + StreetPostdirection string `json:"street_postdirection"` + CityName string `json:"city_name"` + StateAbbreviation string `json:"state_abbreviation"` + Zipcode string `json:"zipcode"` + Plus4Code string `json:"plus4_code"` +} + +type Alias struct { + SmartyKey string `json:"smarty_key"` + PrimaryNumber string `json:"primary_number"` + StreetPredirection string `json:"street_predirection"` + StreetName string `json:"street_name"` + StreetSuffix string `json:"street_suffix"` + StreetPostdirection string `json:"street_postdirection"` + CityName string `json:"city_name"` + StateAbbreviation string `json:"state_abbreviation"` + Zipcode string `json:"zipcode"` + Plus4Code string `json:"plus4_code"` +} + +type Secondary struct { + SmartyKey string `json:"smarty_key"` + SecondaryDesignator string `json:"secondary_designator"` + SecondaryNumber string `json:"secondary_number"` + Plus4Code string `json:"plus4_code"` +} + type SecondaryResponse struct { SmartyKey string `json:"smarty_key"` Etag string - RootAddress struct { - SecondaryCount uint64 `json:"secondary_count"` - SmartyKey string `json:"smarty_key"` - PrimaryNumber string `json:"primary_number"` - StreetPredirection string `json:"street_predirection"` - StreetName string `json:"street_name"` - StreetSuffix string `json:"street_suffix"` - StreetPostdirection string `json:"street_postdirection"` - CityName string `json:"city_name"` - StateAbbreviation string `json:"state_abbreviation"` - Zipcode string `json:"zipcode"` - Plus4Code string `json:"plus4_code"` - } `json:"root_address"` - Aliases []struct { - SmartyKey string `json:"smarty_key"` - PrimaryNumber string `json:"primary_number"` - StreetPredirection string `json:"street_predirection"` - StreetName string `json:"street_name"` - StreetSuffix string `json:"street_suffix"` - StreetPostdirection string `json:"street_postdirection"` - CityName string `json:"city_name"` - StateAbbreviation string `json:"state_abbreviation"` - Zipcode string `json:"zipcode"` - Plus4Code string `json:"plus4_code"` - } `json:"aliases"` - Secondaries []struct { - SmartyKey string `json:"smarty_key"` - SecondaryDesignator string `json:"secondary_designator"` - SecondaryNumber string `json:"secondary_number"` - Plus4Code string `json:"plus4_code"` - } `json:"secondaries"` + RootAddress RootAddress `json:"root_address"` + Aliases []Alias `json:"aliases"` + Secondaries []Secondary `json:"secondaries"` } type SecondaryCountResponse struct {