Skip to content

Commit

Permalink
support searching documents
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanhan101 committed May 3, 2021
1 parent 647ad5d commit e8b5e09
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 26 deletions.
13 changes: 13 additions & 0 deletions examples/document/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func main() {
}

documentID := "35239377"

// Update Document
resp, err := client.UpdateDocument(documentID, scheme.DocumentUpdateOptions{
BillToName: "Hoanh An",
BillToAddress: "NY",
Expand All @@ -43,4 +45,15 @@ func main() {
log.Fatal(err)
}
fmt.Printf("ID: %v\tBill To Name: %v\n", resp.ID, resp.BillToName)

// Search Documents
docs, err := client.SearchDocuments(scheme.DocumentSearchOptions{
Tag: "example",
})
if err != nil {
log.Fatal(err)
}
for _, doc := range *docs {
fmt.Printf("ID: %v\tBill To Name: %v\n", doc.ID, doc.BillToName)
}
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ require (
github.com/creasty/defaults v1.5.1
github.com/go-resty/resty/v2 v2.6.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/tools v0.1.0 // indirect
github.com/stretchr/testify v1.7.0
)
25 changes: 1 addition & 24 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 3 additions & 0 deletions veryfi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ type Client interface {

// UpdateDocument updates a previously processed document.
UpdateDocument(string, scheme.DocumentUpdateOptions) (*scheme.Document, error)

// SearchDocuments retrieve previously processed documents.
SearchDocuments(scheme.DocumentSearchOptions) (*[]scheme.Document, error)
}
19 changes: 19 additions & 0 deletions veryfi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ func (c *httpClient) UpdateDocument(documentID string, opts scheme.DocumentUpdat
return *out, nil
}

// SearchDocuments returns processed document with matching queries.
func (c *httpClient) SearchDocuments(opts scheme.DocumentSearchOptions) (*[]scheme.Document, error) {
out := new(*[]scheme.Document)
if err := c.get(documentURI, opts, out); err != nil {
return nil, err
}

return *out, nil
}

// request returns an authorized request to Veryfi API.
func (c *httpClient) request(okScheme interface{}, errScheme interface{}) *resty.Request {
return c.setBaseURL().R().
Expand Down Expand Up @@ -139,6 +149,15 @@ func (c *httpClient) put(uri string, body interface{}, okScheme interface{}) err
return check(err, errScheme)
}

// get performs a GET request against Veryfi API.
func (c *httpClient) get(uri string, queryParams interface{}, okScheme interface{}) error {
errScheme := new(scheme.Error)
request := c.request(okScheme, errScheme).SetQueryParams(structToMap(queryParams))
_, err := request.Get(uri)

return check(err, errScheme)
}

// check validates returned response from Veryfi.
func check(err error, errResp *scheme.Error) error {
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions veryfi/scheme/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ type VendorUpdateOptions struct {
Address string `json:"address,omitempty"`
}

// DocumentSearchOptions describes the query parameters to search document.
type DocumentSearchOptions struct {
Q string `json:"q"`
ExternalID string `json:"external_id"`
Tag string `json:"tag"`
CreatedGT string `json:"created__gt"`
CreatedGTE string `json:"created__gte"`
CreatedLT string `json:"created__lt"`
CreatedLTE string `json:"created__lte"`
}

// Document describes the response.
type Document struct {
ABNNumber string `json:"abn_number"`
Expand Down
20 changes: 20 additions & 0 deletions veryfi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package veryfi
import (
"bufio"
"encoding/base64"
"fmt"
"io/ioutil"
"net/url"
"os"
"reflect"
"strings"
)

Expand Down Expand Up @@ -35,3 +37,21 @@ func buildURL(host string, path ...string) string {

return u.String()
}

// structToMap converts a struct of string fields to a map[string]string.
func structToMap(s interface{}) map[string]string {
out := map[string]string{}

fields := reflect.TypeOf(s)
values := reflect.ValueOf(s)

for i := 0; i < fields.NumField(); i++ {
field := fields.Field(i).Tag.Get("json")
value := fmt.Sprint(values.Field(i))
if len(value) > 0 {
out[field] = value
}
}

return out
}
65 changes: 65 additions & 0 deletions veryfi/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package veryfi
import (
"testing"

"github.com/hoanhan101/veryfi-go/veryfi/scheme"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -46,3 +47,67 @@ func TestUnitBuildURL(t *testing.T) {
assert.Equal(t, tt.expected, out)
}
}

func TestUnitStructToMap(t *testing.T) {
tests := []struct {
in scheme.DocumentSearchOptions
expected map[string]string
}{
{
in: scheme.DocumentSearchOptions{},
expected: map[string]string{},
},
{
in: scheme.DocumentSearchOptions{
CreatedGT: "foo",
},
expected: map[string]string{
"created__gt": "foo",
},
},
{
in: scheme.DocumentSearchOptions{
Q: "",
CreatedGT: "foo",
},
expected: map[string]string{
"created__gt": "foo",
},
},
{
in: scheme.DocumentSearchOptions{
Q: "foo",
CreatedGT: "foo",
},
expected: map[string]string{
"q": "foo",
"created__gt": "foo",
},
},
{
in: scheme.DocumentSearchOptions{
Q: "foo",
ExternalID: "foo",
Tag: "foo",
CreatedGT: "foo",
CreatedGTE: "foo",
CreatedLT: "foo",
CreatedLTE: "foo",
},
expected: map[string]string{
"q": "foo",
"external_id": "foo",
"tag": "foo",
"created__gt": "foo",
"created__gte": "foo",
"created__lt": "foo",
"created__lte": "foo",
},
},
}

for _, tt := range tests {
out := structToMap(tt.in)
assert.Equal(t, tt.expected, out)
}
}

0 comments on commit e8b5e09

Please sign in to comment.