-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
91 lines (70 loc) · 2.12 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package giniapi_test
import (
"context"
"fmt"
"github.com/dkerwin/gini-api-go"
"log"
"os"
"time"
)
// Very simplistic example. You shoud have a lot more error handling in place
func ExampleNewClient() {
//////////////////////////////////
// Oauth2
//////////////////////////////////
// Setup api connection
api, err := giniapi.NewClient(&giniapi.Config{
ClientID: "MY_CLIENT_ID",
ClientSecret: "********",
Username: "user1",
Password: "secret",
Authentication: giniapi.UseOauth2,
})
if err != nil {
log.Panicf("Gini API login failed: %s", err)
}
// create context
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// Read a PDF document
document, _ := os.Open("/tmp/invoice.pdf")
// Upload document to gini without doctype hint and user identifier
doc, _ := api.Upload(ctx, document, giniapi.UploadOptions{FileName: "invoice.pdf"})
// Poll progress
resp := doc.Poll(ctx, 0)
if resp.Error != nil {
log.Printf("Polling Error: %s", resp.Error)
return
}
// Get extractions from our uploaded document
extractions, _ := doc.GetExtractions(ctx, false)
// Print IBAN
fmt.Printf("IBAN has been found: %s Woohoo!\n", extractions.GetValue("iban"))
//////////////////////////////////
// basic Auth
//////////////////////////////////
// Setup api connection
api, err = giniapi.NewClient(&giniapi.Config{
ClientID: "MY_CLIENT_ID",
ClientSecret: "********",
Authentication: giniapi.UseBasicAuth,
})
if err != nil {
log.Panicf("Gini API login failed: %s", err)
}
// Read a PDF document
document, _ = os.Open("/tmp/invoice.pdf")
// Upload document to gini without doctype hint and user identifier
doc, _ = api.Upload(ctx, document, giniapi.UploadOptions{FileName: "invoice.pdf", UserIdentifier: "user123"})
// Poll progress
resp = doc.Poll(ctx, 0)
if resp.Error != nil {
log.Printf("Polling Error: %s", resp.Error)
return
}
// Get extractions from our uploaded document
extractions, _ = doc.GetExtractions(ctx, false)
// Print IBAN
fmt.Printf("IBAN has been found: %s Woohoo!\n", extractions.GetValue("iban"))
}