Skip to content

Commit

Permalink
Merge branch 'geo-reference'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Amundson committed Mar 1, 2024
2 parents a3cc7aa + 0c45e1b commit f9743de
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 22 deletions.
46 changes: 46 additions & 0 deletions examples/us-enrichment-api/geo-reference/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

us_enrichment "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/wireup"
)

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 := "1682393594"

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

err, results := client.SendGeoReference(&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, "principal")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
}
}
48 changes: 48 additions & 0 deletions examples/us-enrichment-api/property-financial/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

us_enrichment "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/wireup"
)

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 := "1682393594"

lookup := us_enrichment.Lookup{
SmartyKey: smartyKey,
Include: "group_structural,sale_date", // optional: only include these attributes in the returned data
Exclude: "", // optional: exclude attributes from the returned data
ETag: "", // optional: check if the record has been updated
}

err, results := client.SendPropertyFinancial(&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, "principal")
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ func main() {
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-principal-cloud"),
// wireup.DebugHTTPOutput(), // uncomment this line to see detailed HTTP request/response information.
)

// Documentation for input fields can be found at:
Expand Down Expand Up @@ -50,8 +45,4 @@ func main() {
for s, response := range results {
fmt.Printf("#%d: %+v\n", s, response)
}

//TODO: Add a test for the "genericLookup" feature

log.Println("OK")
}
20 changes: 8 additions & 12 deletions examples/us-enrichment-api/universal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@ func main() {
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-principal-cloud"),
// wireup.DebugHTTPOutput(), // uncomment this line to see detailed HTTP request/response information.
)

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

smartyKey := "1682393594"

lookup := us_enrichment.Lookup{
SmartyKey: smartyKey,
Include: "group_structural,sale_date", // optional: only include these attributes in the returned data
Exclude: "", // optional: exclude attributes from the returned data
ETag: "", // optional: check if the record has been updated
Exclude: "", // optional: exclude attributes from the returned data
ETag: "", // optional: check if the record has been updated
}

err, results := client.SendUniversalLookup(&lookup, "property", "principal") //for datasets with no subsets, enter the empty string, "", for the dataSubset field
// Universal lookup works with all datasets and optional subsets.
// Returns JSON bytes
// Note: The DataSubset field can be an empty string for datasets that have no subsets.
err, results := client.SendUniversalLookup(&lookup, "property", "principal")

if err != nil {
// If ETag was supplied in the lookup, this status will be returned if the ETag value for the record is current
Expand All @@ -48,6 +46,4 @@ func main() {

fmt.Printf("Results for input: (%s, %s)\n", smartyKey, "principal")
fmt.Println(string(results))

log.Println("OK")
}
8 changes: 7 additions & 1 deletion us-enrichment-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func (c *Client) SendPropertyPrincipal(lookup *Lookup) (error, []*PrincipalRespo
return err, propertyLookup.Response
}

func (c *Client) SendGeoReference(lookup *Lookup) (error, []*GeoReferenceResponse) {
geoRefLookup := &geoReferenceLookup{Lookup: lookup}
err := c.sendLookup(geoRefLookup)
return err, geoRefLookup.Response
}

func (c *Client) SendUniversalLookup(lookup *Lookup, dataSet, dataSubset string) (error, []byte) {
g := &universalLookup{
Lookup: lookup,
Expand Down Expand Up @@ -100,7 +106,7 @@ func buildLookupURL(lookup enrichmentLookup) string {
}

newLookupURL = strings.Replace(newLookupURL, lookupURLDataSet, lookup.getDataSet(), 1)
return strings.Replace(newLookupURL, lookupURLDataSubSet, lookup.getDataSubset(), 1)
return strings.TrimSuffix(strings.Replace(newLookupURL, lookupURLDataSubSet, lookup.getDataSubset(), 1), "/")
}

const (
Expand Down
50 changes: 50 additions & 0 deletions us-enrichment-api/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,60 @@ func (e *principalLookup) populate(query url.Values) {

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

type geoReferenceLookup struct {
Lookup *Lookup
Response []*GeoReferenceResponse
}

func (g *geoReferenceLookup) getDataSubset() string {
return emptyDataSubset
}

func (g *geoReferenceLookup) populate(query url.Values) {
g.Lookup.populateInclude(query)
g.Lookup.populateExclude(query)
}

func (g *geoReferenceLookup) getSmartyKey() string {
return g.Lookup.SmartyKey
}

func (g *geoReferenceLookup) getDataSet() string {
return geoReferenceDataSet
}

func (g *geoReferenceLookup) getLookup() *Lookup {
return g.Lookup
}

func (g *geoReferenceLookup) getResponse() interface{} {
return g.Response
}

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

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

return nil
}

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

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

func (l Lookup) populateInclude(query url.Values) {
Expand Down
32 changes: 32 additions & 0 deletions us-enrichment-api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,35 @@ type FinancialAttributes struct {
VeteranTaxExemption string `json:"veteran_tax_exemption"`
WidowTaxExemption string `json:"widow_tax_exemption"`
}

type GeoReferenceResponse struct {
SmartyKey string `json:"smarty_key"`
DataSetName string `json:"data_set_name"`
Attributes GeoReferenceAttributes `json:"attributes"`
Etag string
}

type GeoReferenceAttributes struct {
CensusBlock struct {
Accuracy string `json:"accuracy"`
Id string `json:"id"`
} `json:"census_block"`

CensusCountyDivision struct {
Accuracy string `json:"accuracy"`
Id string `json:"id"`
Name string `json:"name"`
} `json:"census_county_division"`

CoreBasedStatArea struct {
Id string `json:"id"`
Name string `json:"name"`
} `json:"core_based_stat_area"`

Place struct {
Accuracy string `json:"accuracy"`
Id string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
} `json:"place"`
}

0 comments on commit f9743de

Please sign in to comment.