-
Notifications
You must be signed in to change notification settings - Fork 2
/
giniapi.go
297 lines (241 loc) · 8.53 KB
/
giniapi.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2015-2018 The gini-api-go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package giniapi interacts with Gini's API service to make sense of unstructured
documents. Please visit http://developer.gini.net/gini-api/html/index.html
for more details about the Gini API and it's capabilities.
API features
Supported API calls include:
- Upload documents (native, scanned, text)
- List a users documents
- Search documents
- Get extractions (incubator is supported)
- Download rendered pages, processed document and layout XML
- Submit feedback on extractions
- Submit error reports
Contributing
It's awesome that you consider contributing to gini-api-go. Here are the 5 easy steps you should follow:
- Fork repository on Github
- Create a topic/feature branch
- Write code AND tests
- Update documentation if necessary
- Open a pull request
*/
package giniapi
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"time"
)
const (
// VERSION is the API client version
VERSION = "1.0.0"
ErrConfigInvalid = "failed to initialize config object"
ErrMissingCredentials = "username or password cannot be empty in Oauth2 flow"
ErrOauthAuthCodeExchange = "failed to exchange oauth2 auth code"
ErrOauthCredentials = "failed to obtain token with username/password"
ErrOauthParametersMissing = "oauth2 authentication requires AuthCode or Username + Password"
ErrUploadFailed = "failed to upload document"
ErrDocumentGet = "failed to GET document object"
ErrDocumentParse = "failed to parse document json"
ErrDocumentRead = "failed to read document body"
ErrDocumentList = "failed to get document list"
ErrDocumentTimeout = "failed to process document in time"
ErrDocumentProcessing = "failed to process document"
ErrDocumentDelete = "failed to delete document"
ErrDocumentLayout = "failed to retrieve layout"
ErrDocumentExtractions = "failed to retrieve extractions"
ErrDocumentProcessed = "failed to retrieve processed document"
ErrDocumentFeedback = "failed to submit feedback"
ErrHTTPPostFailed = "failed to complete POST request"
ErrHTTPGetFailed = "failed to complete GET request"
ErrHTTPDeleteFailed = "failed to complete DELETE request"
ErrHTTPPutFailed = "failed to complete PUT request"
)
// Config to setup Gini API connection
type Config struct {
// ClientID is the application's ID.
ClientID string
// ClientSecret is the application's secret.
ClientSecret string
// Username for oauth2 password grant
Username string
// Password for oauth2 pssword grant
Password string
// Auth_code to exchange for oauth2 token
AuthCode string
// Scopes to use (leave empty for all assigned scopes)
Scopes []string
// API & Usercenter endpoints
Endpoints
// APIVersion to use (v1)
APIVersion string `default:"v1"`
// Authentication to use
// oauth2: auth_code || password credentials
// basicAuth: basic auth + user identifier
Authentication APIAuthScheme
}
func (c *Config) Verify() error {
if c.ClientID == "" || c.ClientSecret == "" {
return errors.New(ErrConfigInvalid)
}
if reflect.TypeOf(c.Authentication).Name() == "Oauth2" {
if c.AuthCode == "" && (c.Username == "" || c.Password == "") {
return errors.New(ErrMissingCredentials)
}
}
cType := reflect.TypeOf(*c)
// Fix potentially missing APIVersion with default
if c.APIVersion == "" {
f, _ := cType.FieldByName("APIVersion")
c.APIVersion = f.Tag.Get("default")
}
// Fix potential missing Endpoints with defaults
cType = reflect.TypeOf(c.Endpoints)
if c.Endpoints.API == "" {
f, _ := cType.FieldByName("API")
c.Endpoints.API = f.Tag.Get("default")
}
if c.Endpoints.UserCenter == "" {
f, _ := cType.FieldByName("UserCenter")
c.Endpoints.UserCenter = f.Tag.Get("default")
}
return nil
}
// APIResponse will transport about the request back to the caller
type APIResponse struct {
// Error: stores error object encountered on the way
Error error
// Message: error message with more context
Message string
// DocumentId: internal Id of the document. Can be empty
DocumentId string
// RequestId: request Id returned for the request to the gini API
RequestId string
// HttpResponse: full response object
HttpResponse *http.Response
}
// Endpoints to access API and Usercenter
type Endpoints struct {
API string `default:"https://api.gini.net"`
UserCenter string `default:"https://user.gini.net"`
}
// UploadOptions specify parameters to the Upload function
type UploadOptions struct {
FileName string
DocType string
UserIdentifier string
}
// ListOptions specify parameters to the List function
type ListOptions struct {
Limit int
Offset int
UserIdentifier string
}
// APIClient is the main interface for the user
type APIClient struct {
// Config
Config
// Http client
HTTPClient *http.Client
}
// NewClient validates your Config parameters and returns a APIClient object
// with a matching http client included.
func NewClient(config *Config) (*APIClient, error) {
if err := config.Verify(); err != nil {
return nil, err
}
// Get http client based on the selected authentication scheme
client, resp := newHTTPClient(config)
if resp.Error != nil {
return nil, resp.Error
}
return &APIClient{
Config: *config,
HTTPClient: client,
}, nil
}
// Upload a document from a given io.Reader object (document). Additional options can be
// passed with a instance of UploadOptions. FileName and DocType are optional and can be empty.
// UserIdentifier is required if Authentication method is "basic_auth".
// Upload time is measured and stored in Timing struct (part of Document).
func (api *APIClient) Upload(ctx context.Context, document io.Reader, options UploadOptions) (*Document, APIResponse) {
start := time.Now()
resp, err := api.makeAPIRequest(ctx, "POST", fmt.Sprintf("%s/documents", api.Config.Endpoints.API), document, nil, options.UserIdentifier)
if err != nil {
return nil, apiResponse(ErrHTTPPostFailed, "", resp, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, apiResponse(ErrUploadFailed, "", resp, errors.New(ErrUploadFailed))
}
uploadDuration := time.Since(start)
// Fetch the document
doc, response := api.Get(ctx, resp.Header.Get("Location"), options.UserIdentifier)
if err != nil {
return nil, response
}
// Add upload timer to document
doc.Timing.Upload = uploadDuration
return doc, apiResponse("document upload completed", doc.ID, resp, err)
}
// Get Document struct from URL
func (api *APIClient) Get(ctx context.Context, url, userIdentifier string) (*Document, APIResponse) {
resp, err := api.makeAPIRequest(ctx, "GET", url, nil, nil, userIdentifier)
if err != nil {
return nil, apiResponse(ErrHTTPGetFailed, "", resp, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiResponse(ErrDocumentGet, "", resp, errors.New(ErrDocumentGet))
}
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, apiResponse(ErrDocumentRead, "", resp, err)
}
var doc Document
if err := json.Unmarshal(contents, &doc); err != nil {
return nil, apiResponse(ErrDocumentParse, "", resp, err)
}
// Add client and owner to doc object
doc.client = api
doc.Owner = userIdentifier
return &doc, apiResponse("document fetch completed", doc.ID, resp, err)
}
// List returns DocumentSet
func (api *APIClient) List(ctx context.Context, options ListOptions) (*DocumentSet, APIResponse) {
params := map[string]interface{}{
"limit": options.Limit,
"offset": options.Offset,
}
u := encodeURLParams(fmt.Sprintf("%s/documents", api.Config.Endpoints.API), params)
resp, err := api.makeAPIRequest(ctx, "GET", u, nil, nil, options.UserIdentifier)
if err != nil {
return nil, apiResponse(ErrHTTPGetFailed, "", resp, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiResponse(ErrDocumentList, "", resp, errors.New(ErrDocumentList))
}
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, apiResponse(ErrDocumentRead, "", resp, err)
}
var docs DocumentSet
if err := json.Unmarshal(contents, &docs); err != nil {
return nil, apiResponse(ErrDocumentParse, "", resp, err)
}
// Extra round: Ingesting *APIClient into each and every doc
for _, d := range docs.Documents {
d.client = api
d.Owner = options.UserIdentifier
}
return &docs, apiResponse("document list completed", "", resp, err)
}