Skip to content

Commit

Permalink
Use base64 encoded file instead of multipart upload (#4)
Browse files Browse the repository at this point in the history
* don't use multipart upload

* Test init

* Clean up

* Checkin tests

* Decouple tests

* Test config

* Add trailing slash for consistency

* Check nil mock server

* Test github action
  • Loading branch information
hoanhan101 authored Aug 31, 2021
1 parent c4b8b07 commit ac1964c
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 176 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.15.x, 1.16.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test -race -run Unit ./...
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,5 @@ require (
github.com/go-resty/resty/v2 v2.6.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/yuin/goldmark v1.3.5 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/tools v0.1.0 // indirect
)
35 changes: 0 additions & 35 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,19 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
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=
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
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 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/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/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
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/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/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/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
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/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/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=
Expand Down
34 changes: 18 additions & 16 deletions veryfi/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package veryfi

import (
"crypto/tls"
"fmt"

"github.com/pkg/errors"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -58,20 +60,24 @@ func (c *Client) Config() *Options {
return c.options
}

// ProcessDocumentUpload returns the processed document using file upload.
// SetTLSConfig sets the TLS configurations for underling transportation layer.
func (c *Client) SetTLSConfig(config *tls.Config) {
c.client.SetTLSClientConfig(config)
}

// ProcessDocumentUpload returns the processed document.
func (c *Client) ProcessDocumentUpload(opts scheme.DocumentUploadOptions) (*scheme.Document, error) {
out := new(*scheme.Document)
if err := c.post(documentURI, opts.FilePath, opts.DocumentSharedOptions, out); err != nil {
encodedFile, err := Base64EncodeFile(opts.FilePath)
if err != nil {
return nil, err
}

return *out, nil
}

// ProcessDocumentUploadBase64 returns the processed base64 encoded document.
func (c *Client) ProcessDocumentUploadBase64(opts scheme.DocumentUploadBase64Options) (*scheme.Document, error) {
out := new(*scheme.Document)
if err := c.post(documentURI, "", opts, out); err != nil {
payload := scheme.DocumentUploadBase64Options{
FileData: encodedFile,
DocumentSharedOptions: opts.DocumentSharedOptions,
}
if err := c.post(documentURI, payload, out); err != nil {
return nil, err
}

Expand All @@ -81,7 +87,7 @@ func (c *Client) ProcessDocumentUploadBase64(opts scheme.DocumentUploadBase64Opt
// ProcessDocumentURL returns the processed document using URL.
func (c *Client) ProcessDocumentURL(opts scheme.DocumentURLOptions) (*scheme.Document, error) {
out := new(*scheme.Document)
if err := c.post(documentURI, "", opts, out); err != nil {
if err := c.post(documentURI, opts, out); err != nil {
return nil, err
}

Expand Down Expand Up @@ -141,7 +147,7 @@ func (c *Client) GetLineItems(documentID string) (*scheme.LineItems, error) {
// AddLineItem returns a added line item for a processed document.
func (c *Client) AddLineItem(documentID string, opts scheme.LineItemOptions) (*scheme.LineItem, error) {
out := new(*scheme.LineItem)
if err := c.post(fmt.Sprintf("%s%s%s", documentURI, documentID, lineItemURI), "", opts, out); err != nil {
if err := c.post(fmt.Sprintf("%s%s%s", documentURI, documentID, lineItemURI), opts, out); err != nil {
return nil, err
}

Expand Down Expand Up @@ -247,14 +253,10 @@ func (c *Client) setBaseURL() *resty.Client {
}

// post performs a POST request against Veryfi API.
func (c *Client) post(uri string, filePath string, body interface{}, okScheme interface{}) error {
func (c *Client) post(uri string, body interface{}, okScheme interface{}) error {
errScheme := new(scheme.Error)
request := c.request(okScheme, errScheme).SetBody(body)

if len(filePath) != 0 {
request.SetFile("file", filePath)
}

_, err := request.Post(uri)

return check(err, errScheme)
Expand Down
80 changes: 29 additions & 51 deletions veryfi/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ package veryfi
import (
"fmt"
"os"
"strconv"
"testing"
"time"

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

func cleanUp(t *testing.T, documentID int) {
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)

err = client.DeleteDocument(strconv.Itoa(documentID))
assert.NoError(t, err)
}

func TestIntegrationFailNoAuth(t *testing.T) {
client, err := NewClientV7(&Options{})
assert.NotNil(t, client)
Expand All @@ -18,8 +31,8 @@ func TestIntegrationFailNoAuth(t *testing.T) {
resp, err := client.ProcessDocumentURL(scheme.DocumentURLOptions{
FileURL: "foo.com",
})
assert.Nil(t, resp)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestIntegrationFailInvalidClientID(t *testing.T) {
Expand All @@ -34,8 +47,8 @@ func TestIntegrationFailInvalidClientID(t *testing.T) {
resp, err := client.ProcessDocumentURL(scheme.DocumentURLOptions{
FileURL: "foo.com",
})
assert.Nil(t, resp)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestIntegrationFailInvalidUsername(t *testing.T) {
Expand All @@ -44,14 +57,14 @@ func TestIntegrationFailInvalidUsername(t *testing.T) {
Username: "foo",
APIKey: os.Getenv("API_KEY"),
})
assert.NotNil(t, client)
assert.NoError(t, err)
assert.NotNil(t, client)

resp, err := client.ProcessDocumentURL(scheme.DocumentURLOptions{
FileURL: "foo.com",
})
assert.Nil(t, resp)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestIntegrationFailInvalidAPIKey(t *testing.T) {
Expand All @@ -60,45 +73,37 @@ func TestIntegrationFailInvalidAPIKey(t *testing.T) {
Username: os.Getenv("USERNAME"),
APIKey: "foo",
})
assert.NotNil(t, client)
assert.NoError(t, err)
assert.NotNil(t, client)

resp, err := client.ProcessDocumentURL(scheme.DocumentURLOptions{
FileURL: "foo.com",
})
assert.Nil(t, resp)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestIntegrationFailInvalidDocument(t *testing.T) {
timeout, _ := time.ParseDuration("10s")
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
HTTP: HTTPOptions{
Timeout: timeout,
},
})
assert.NotNil(t, client)
assert.NoError(t, err)

resp, err := client.ProcessDocumentURL(scheme.DocumentURLOptions{
FileURL: "foo.com",
})
assert.Nil(t, resp)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestIntegrationSuccessProcessDocumentURL(t *testing.T) {
timeout, _ := time.ParseDuration("10s")
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
HTTP: HTTPOptions{
Timeout: timeout,
},
})
assert.NotNil(t, client)
assert.NoError(t, err)
Expand All @@ -109,58 +114,31 @@ func TestIntegrationSuccessProcessDocumentURL(t *testing.T) {
Tags: []string{"integration", "test", "url"},
},
})
assert.NotNil(t, resp)
defer cleanUp(t, resp.ID)
assert.NoError(t, err)
assert.NotNil(t, resp)
}

func TestIntegrationSuccessProcessDocumentUpload(t *testing.T) {
timeout, _ := time.ParseDuration("10s")
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
HTTP: HTTPOptions{
Timeout: timeout,
},
})
assert.NotNil(t, client)
assert.NoError(t, err)

testpath, _ := os.Getwd()
testpath, err := os.Getwd()
assert.NoError(t, err)

resp, err := client.ProcessDocumentUpload(scheme.DocumentUploadOptions{
FilePath: fmt.Sprintf("%s/testdata/invoice1.png", testpath),
FilePath: fmt.Sprintf("%s/testdata/receipt_public.jpeg", testpath),
DocumentSharedOptions: scheme.DocumentSharedOptions{
FileName: "test_invoice",
FileName: "receipt_public.jpeg",
Tags: []string{"integration", "test", "upload"},
},
})
assert.NotNil(t, resp)
assert.NoError(t, err)
}

func TestIntegrationSuccessProcessDocumentUploadBase64(t *testing.T) {
timeout, _ := time.ParseDuration("10s")
client, err := NewClientV7(&Options{
ClientID: os.Getenv("CLIENT_ID"),
Username: os.Getenv("USERNAME"),
APIKey: os.Getenv("API_KEY"),
HTTP: HTTPOptions{
Timeout: timeout,
},
})
assert.NotNil(t, client)
defer cleanUp(t, resp.ID)
assert.NoError(t, err)

testpath, _ := os.Getwd()
encodedFile, _ := Base64EncodeFile(fmt.Sprintf("%s/testdata/invoice1.png", testpath))
resp, err := client.ProcessDocumentUploadBase64(scheme.DocumentUploadBase64Options{
FileData: encodedFile,
DocumentSharedOptions: scheme.DocumentSharedOptions{
FileName: "invoice1.png",
Tags: []string{"integration", "test", "upload", "base64"},
},
})

assert.NotNil(t, resp)
assert.NoError(t, err)
}
Loading

0 comments on commit ac1964c

Please sign in to comment.