Skip to content

Commit

Permalink
Renamed generic to universal, added tests, and created examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMcKenzieSmarty committed Feb 26, 2024
1 parent b6dc573 commit 875d69e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
2 changes: 2 additions & 0 deletions examples/us-enrichment-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ func main() {
fmt.Printf("#%d: %+v\n", s, response)
}

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

log.Println("OK")
}
53 changes: 53 additions & 0 deletions examples/us-enrichment-api/universal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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")),
// 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

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.SendUniversalLookup(&lookup, "property", "principal") //for datasets with no subsets, enter the empty string, "", for the dataSubset field

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")
fmt.Println(string(results))

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

func (c *Client) SendGenericLookup(lookup *Lookup, dataSet, dataSubset string) (error, []byte) {
g := &genericLookup{
func (c *Client) SendUniversalLookup(lookup *Lookup, dataSet, dataSubset string) (error, []byte) {
g := &universalLookup{
Lookup: lookup,
DataSet: dataSet,
DataSubset: dataSubset,
Expand Down
16 changes: 8 additions & 8 deletions us-enrichment-api/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ type enrichmentLookup interface {
populate(query url.Values)
}

type genericLookup struct {
type universalLookup struct {
Lookup *Lookup
DataSet string
DataSubset string
Response []byte
}

func (g *genericLookup) getSmartyKey() string { return g.Lookup.SmartyKey }
func (g *genericLookup) getDataSet() string { return g.DataSet }
func (g *genericLookup) getDataSubset() string { return g.DataSubset }
func (g *genericLookup) getLookup() *Lookup { return g.Lookup }
func (g *genericLookup) getResponse() interface{} { return g.Response }
func (g *genericLookup) unmarshalResponse(bytes []byte, headers http.Header) error {
func (g *universalLookup) getSmartyKey() string { return g.Lookup.SmartyKey }
func (g *universalLookup) getDataSet() string { return g.DataSet }
func (g *universalLookup) getDataSubset() string { return g.DataSubset }
func (g *universalLookup) getLookup() *Lookup { return g.Lookup }
func (g *universalLookup) getResponse() interface{} { return g.Response }
func (g *universalLookup) unmarshalResponse(bytes []byte, headers http.Header) error {
g.Response = bytes
if headers != nil {
if etag, found := headers[lookupETagHeader]; found && len(etag) > 0 {
Expand All @@ -58,7 +58,7 @@ func (g *genericLookup) unmarshalResponse(bytes []byte, headers http.Header) err
return nil

}
func (g *genericLookup) populate(query url.Values) {
func (g *universalLookup) populate(query url.Values) {
g.Lookup.populateInclude(query)
g.Lookup.populateExclude(query)
}
Expand Down

0 comments on commit 875d69e

Please sign in to comment.