-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
238 lines (206 loc) · 5.62 KB
/
api.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
package web
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/gnames/gndoc"
gnfinder "github.com/gnames/gnfinder/pkg"
"github.com/gnames/gnfinder/pkg/config"
"github.com/gnames/gnfinder/pkg/ent/api"
"github.com/gnames/gnfinder/pkg/ent/lang"
"github.com/gnames/gnfinder/pkg/ent/output"
"github.com/gnames/gnfinder/pkg/ent/verifier"
"github.com/gnames/gnfmt"
"github.com/labstack/echo/v4"
)
func infoApiGET(gnf gnfinder.GNfinder) func(echo.Context) error {
return func(c echo.Context) error {
info := fmt.Sprintf(
"OpenAPI for gnfinder is described at\n\n%s\n",
gnf.GetConfig().APIDoc,
)
return c.String(http.StatusOK, info)
}
}
func pingApiGET() func(echo.Context) error {
return func(c echo.Context) error {
return c.String(http.StatusOK, "pong")
}
}
func verApiGET(gnf gnfinder.GNfinder) func(echo.Context) error {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, gnf.GetVersion())
}
}
func findApiGET(gnf gnfinder.GNfinder) func(echo.Context) error {
return func(c echo.Context) error {
ctx, cancel := getContext(c)
defer cancel()
chErr := make(chan error)
params := paramsFindGET(c)
go finder(c, gnf, params, chErr)
select {
case <-ctx.Done():
<-chErr
return ctx.Err()
case err := <-chErr:
return err
case <-time.After(1 * time.Minute):
return errors.New("request took too long")
}
}
}
func paramsFindGET(c echo.Context) api.FinderParams {
var textURL, text string
text, _ = url.QueryUnescape(c.Param("text"))
if strings.HasPrefix(text, "http") {
textURL = text
text = ""
}
var wordsAround int
if num, err := strconv.Atoi(c.QueryParam("words_around")); err == nil {
wordsAround = num
}
var sources []int
elements := strings.Split(c.QueryParam("sources"), ",")
for _, v := range elements {
v = strings.TrimSpace(v)
if num, err := strconv.Atoi(v); err == nil {
sources = append(sources, num)
}
}
params := api.FinderParams{
URL: textURL,
Text: text,
Format: c.QueryParam("format"),
Language: c.QueryParam("language"),
BytesOffset: c.QueryParam("bytes_offset") == "true",
ReturnContent: c.QueryParam("return_content") == "true",
UniqueNames: c.QueryParam("unique_names") == "true",
AmbiguousNames: c.QueryParam("ambiguous_names") == "true",
NoBayes: c.QueryParam("no_bayes") == "true",
OddsDetails: c.QueryParam("odds_details") == "true",
WordsAround: wordsAround,
Verification: c.QueryParam("verification") == "true",
Sources: sources,
AllMatches: c.QueryParam("all_matches") == "true",
}
if len(params.Sources) > 0 || params.AllMatches {
params.Verification = true
}
return params
}
func findApiPOST(gnf gnfinder.GNfinder) func(echo.Context) error {
return func(c echo.Context) error {
ctx, cancel := getContext(c)
defer cancel()
chErr := make(chan error)
var params api.FinderParams
err := c.Bind(¶ms)
if err != nil {
return err
}
go finder(c, gnf, params, chErr)
select {
case <-ctx.Done():
<-chErr
return ctx.Err()
case err := <-chErr:
return err
case <-time.After(1 * time.Minute):
return errors.New("request took too long")
}
}
}
func finder(c echo.Context, gnf gnfinder.GNfinder, params api.FinderParams, chErr chan<- error) {
var err error
var text, filename string
var txtExtr float32
var out output.Output
var format gnfmt.Format
var opts []config.Option
text, filename, txtExtr, err = getText(
c,
params,
gnf.GetConfig().TikaURL,
)
opts, format = getOptsAPI(params)
gnf = gnf.ChangeConfig(opts...)
out = gnf.Find(filename, text)
out.TextExtractionSec = txtExtr
cfg := gnf.GetConfig()
if cfg.WithVerification {
verif := verifier.New(
cfg.VerifierURL,
cfg.DataSources,
cfg.WithAllMatches,
)
verifiedNames, stats, dur := verif.Verify(out.UniqueNameStrings())
out.MergeVerification(verifiedNames, stats, dur)
}
out.TotalSec = out.TextExtractionSec + out.NameFindingSec + out.NameVerifSec
if err == nil {
if format == gnfmt.CompactJSON || format == gnfmt.PrettyJSON {
err = c.JSON(http.StatusOK, out)
} else {
err = c.String(http.StatusOK, out.Format(format))
}
}
chErr <- err
}
func getOptsAPI(params api.FinderParams) ([]config.Option, gnfmt.Format) {
format, _ := gnfmt.NewFormat(params.Format)
if format == gnfmt.FormatNone {
format = gnfmt.CompactJSON
}
opts := []config.Option{
config.OptWithBayesOddsDetails(params.OddsDetails),
config.OptFormat(format),
config.OptWithBayes(!params.NoBayes),
config.OptWithPositonInBytes(params.BytesOffset),
config.OptLanguage(getLanguage(params.Language)),
config.OptDataSources(params.Sources),
config.OptIncludeInputText(params.ReturnContent),
config.OptWithAllMatches(params.AllMatches),
config.OptWithAmbiguousNames(params.AmbiguousNames),
config.OptWithVerification(
params.Verification ||
len(params.Sources) > 0 ||
params.AllMatches,
),
config.OptTokensAround(params.WordsAround),
}
return opts, format
}
func getLanguage(s string) lang.Language {
l, _ := lang.New(s)
return l
}
func getContext(c echo.Context) (ctx context.Context, cancel func()) {
ctx = c.Request().Context()
ctx, cancel = context.WithTimeout(ctx, 1*time.Minute)
return ctx, cancel
}
func getText(
c echo.Context,
params api.FinderParams,
tikaURL string,
) (string, string, float32, error) {
var err error
var txt, filename string
var dur float32
if params.Text != "" {
return params.Text, filename, dur, err
}
d := gndoc.New(tikaURL)
if params.URL != "" {
txt, dur, err = d.TextFromURL(params.URL)
return txt, filename, dur, err
}
return textFromFile(c, tikaURL)
}