Skip to content

Commit

Permalink
Add Secondary Data lookup and count.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLCox1 committed Jul 12, 2024
1 parent ec895b7 commit 63aef67
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 5 deletions.
25 changes: 23 additions & 2 deletions examples/us-enrichment-api/property-principal/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
Expand All @@ -21,7 +22,7 @@ func main() {
// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/us-address-enrichment-api#http-request-input-fields

smartyKey := "1682393594"
smartyKey := "87844267"

lookup := us_enrichment.Lookup{
SmartyKey: smartyKey,
Expand All @@ -43,6 +44,26 @@ func main() {

fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "principal")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
jsonResponse, _ := json.MarshalIndent(response, "", " ")
fmt.Printf("#%d: %s\n", s, string(jsonResponse))
}

client = wireup.BuildUSEnrichmentAPIClient(
//wireup.WebsiteKeyCredential(os.Getenv("SMARTY_AUTH_WEB"), os.Getenv("SMARTY_AUTH_REFERER")),
wireup.SecretKeyCredential(os.Getenv("SMARTY_AUTH_ID"), os.Getenv("SMARTY_AUTH_TOKEN")),
// The appropriate license values to be used for your subscriptions
// can be found on the Subscriptions page the account dashboard.
// https://www.smarty.com/docs/cloud/licensing
wireup.WithLicenses("us-property-data-financial-cloud"),
// wireup.DebugHTTPOutput(), // uncomment this line to see detailed HTTP request/response information.
)
err, financialResults := client.SendPropertyFinancialLookup(smartyKey)
if err != nil {
log.Fatal("Error sending lookup:", err)
}
fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "financial")
for s, response := range financialResults {
jsonResponse, _ := json.MarshalIndent(response, "", " ")
fmt.Printf("#%d: %s\n", s, string(jsonResponse))
}
}
45 changes: 45 additions & 0 deletions examples/us-enrichment-api/secondary-count/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
usenrich "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/wireup"
"log"
"net/http"
"os"
)

func main() {
log.SetFlags(log.Ltime | log.Llongfile)

client := wireup.BuildUSEnrichmentAPIClient(
//wireup.WebsiteKeyCredential(os.Getenv("SMARTY_AUTH_WEB"), os.Getenv("SMARTY_AUTH_REFERER")),
wireup.SecretKeyCredential(os.Getenv("SMARTY_AUTH_ID"), os.Getenv("SMARTY_AUTH_TOKEN")),
)

// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/us-address-enrichment-api#http-request-input-fields

smartyKey := "1270119982"

lookup := usenrich.Lookup{
SmartyKey: smartyKey,
ETag: "", // optional: check if the record has been updated
}

err, results := client.SendSecondaryCountLookup(&lookup)

if err != nil {
// If ETag was supplied in the lookup, this status will be returned if the ETag value for the record is current
if client.IsHTTPErrorCode(err, http.StatusNotModified) {
log.Printf("Record has not been modified since the last request")
return
}
log.Fatal("Error sending lookup:", err)
}

fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "secondary")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
}
}
45 changes: 45 additions & 0 deletions examples/us-enrichment-api/secondary/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
usenrich "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/wireup"
"log"
"net/http"
"os"
)

func main() {
log.SetFlags(log.Ltime | log.Llongfile)

client := wireup.BuildUSEnrichmentAPIClient(
//wireup.WebsiteKeyCredential(os.Getenv("SMARTY_AUTH_WEB"), os.Getenv("SMARTY_AUTH_REFERER")),
wireup.SecretKeyCredential(os.Getenv("SMARTY_AUTH_ID"), os.Getenv("SMARTY_AUTH_TOKEN")),
)

// Documentation for input fields can be found at:
// https://www.smarty.com/docs/cloud/us-address-enrichment-api#http-request-input-fields

smartyKey := "1270119982"

lookup := usenrich.Lookup{
SmartyKey: smartyKey,
ETag: "", // optional: check if the record has been updated
}

err, results := client.SendSecondaryLookup(&lookup)

if err != nil {
// If ETag was supplied in the lookup, this status will be returned if the ETag value for the record is current
if client.IsHTTPErrorCode(err, http.StatusNotModified) {
log.Printf("Record has not been modified since the last request")
return
}
log.Fatal("Error sending lookup:", err)
}

fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "secondary")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/smartystreets/smartystreets-go-sdk
go 1.21

require (
github.com/smarty/assertions v1.15.1
github.com/smarty/assertions v1.16.0
github.com/smarty/gunit v1.5.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/smarty/assertions v1.15.1 h1:812oFiXI+G55vxsFf+8bIZ1ux30qtkdqzKbEFwyX3Tk=
github.com/smarty/assertions v1.15.1/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smarty/assertions v1.16.0 h1:EvHNkdRA4QHMrn75NZSoUQ/mAUXAYWfatfB01yTCzfY=
github.com/smarty/assertions v1.16.0/go.mod h1:duaaFdCS0K9dnoM50iyek/eYINOZ64gbh1Xlf6LG7AI=
github.com/smarty/gunit v1.5.0 h1:OmG6a/rgi7qCjlQis6VjXbvx/WqZ8I6xSlbfN4YB5MY=
github.com/smarty/gunit v1.5.0/go.mod h1:uAeNibUD292KZRcg5OTy7lb6WR5++UC0BQOzNuiRzpU=
12 changes: 12 additions & 0 deletions us-enrichment-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func (c *Client) SendGeoReference(lookup *Lookup) (error, []*GeoReferenceRespons
return err, geoRefLookup.Response
}

func (c *Client) SendSecondaryLookup(lookup *Lookup) (error, []*SecondaryResponse) {
secondaryLookup := &secondaryLookup{Lookup: lookup}
err := c.sendLookup(secondaryLookup)
return err, secondaryLookup.Response
}

func (c *Client) SendSecondaryCountLookup(lookup *Lookup) (error, []*SecondaryCountResponse) {
secondaryCountLookup := &secondaryCountLookup{Lookup: lookup}
err := c.sendLookup(secondaryCountLookup)
return err, secondaryCountLookup.Response
}

func (c *Client) SendUniversalLookup(lookup *Lookup, dataSet, dataSubset string) (error, []byte) {
g := &universalLookup{
Lookup: lookup,
Expand Down
96 changes: 96 additions & 0 deletions us-enrichment-api/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,107 @@ func (g *geoReferenceLookup) unmarshalResponse(bytes []byte, headers http.Header

////////////////////////////////////////////////////////////////////////////////////////

type secondaryLookup struct {
*Lookup
Response []*SecondaryResponse
}

func (s *secondaryLookup) getSmartyKey() string {
return s.SmartyKey
}

func (s *secondaryLookup) getDataSet() string {
return secondaryData
}

func (s *secondaryLookup) getDataSubset() string {
return emptyDataSubset
}

func (s *secondaryLookup) getLookup() *Lookup {
return s.Lookup
}

func (s *secondaryLookup) getResponse() interface{} {
return s.Response
}

func (s *secondaryLookup) unmarshalResponse(bytes []byte, header http.Header) error {
if err := json.Unmarshal(bytes, &s.Response); err != nil {
return err
}

if header != nil {
if etag, found := header[lookupETagHeader]; found {
if len(etag) > 0 && len(s.Response) > 0 {
s.Response[0].Etag = etag[0]
}
}
}

return nil
}

func (s *secondaryLookup) populate(query url.Values) {
s.Lookup.populateInclude(query)
s.Lookup.populateExclude(query)
}

////////////////////////////////////////////////////////////////////////////////////////

type secondaryCountLookup struct {
*Lookup
Response []*SecondaryCountResponse
}

func (s *secondaryCountLookup) getSmartyKey() string {
return s.SmartyKey
}

func (s *secondaryCountLookup) getDataSet() string {
return secondaryData
}

func (s *secondaryCountLookup) getDataSubset() string {
return secondaryDataCount
}

func (s *secondaryCountLookup) getLookup() *Lookup {
return s.Lookup
}

func (s *secondaryCountLookup) getResponse() interface{} {
return s.Response
}

func (s *secondaryCountLookup) unmarshalResponse(bytes []byte, header http.Header) error {
if err := json.Unmarshal(bytes, &s.Response); err != nil {
return err
}

if header != nil {
if etag, found := header[lookupETagHeader]; found {
if len(etag) > 0 && len(s.Response) > 0 {
s.Response[0].Etag = etag[0]
}
}
}

return nil
}

func (s *secondaryCountLookup) populate(query url.Values) {
s.Lookup.populateInclude(query)
s.Lookup.populateExclude(query)
}

const (
financialDataSubset = "financial"
principalDataSubset = "principal"
propertyDataSet = "property"
geoReferenceDataSet = "geo-reference"
secondaryData = "secondary"
secondaryDataCount = "count"
emptyDataSubset = ""
)

Expand Down
42 changes: 42 additions & 0 deletions us-enrichment-api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,45 @@ type GeoReferenceAttributes struct {
Type string `json:"type"`
} `json:"place"`
}

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"`
}

type SecondaryCountResponse struct {
SmartyKey string `json:"smarty_key"`
Count int `json:"count"`
Etag string
}

0 comments on commit 63aef67

Please sign in to comment.