-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
69 lines (62 loc) · 1.67 KB
/
matcher.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
package matcher
import (
"bytes"
"io/ioutil"
"net/http"
"github.com/gnames/gnfmt"
"github.com/gnames/gnlib/ent/gnvers"
mlib "github.com/gnames/gnlib/ent/matcher"
"github.com/gnames/gnmatcher"
gnmcfg "github.com/gnames/gnmatcher/config"
"github.com/rs/zerolog/log"
)
type matcherREST struct {
url string
enc gnfmt.Encoder
}
// New creates an implementation of GNmatcher interface.
func New(url string) gnmatcher.GNmatcher {
return matcherREST{url: url, enc: gnfmt.GNjson{}}
}
func (mr matcherREST) GetVersion() gnvers.Version {
var err error
response := gnvers.Version{}
resp, err := http.Get(mr.url + "version")
if err != nil {
log.Warn().Err(err).Msg("Cannot get gnmatcher version")
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warn().Err(err).Msg("Cannot get gnmatcher version")
}
err = mr.enc.Decode(respBytes, &response)
if err != nil {
log.Warn().Err(err).Msg("Cannot get gnmatcher version")
}
return response
}
func (mr matcherREST) MatchNames(names []string) []mlib.Match {
var response []mlib.Match
req, err := mr.enc.Encode(names)
if err != nil {
log.Warn().Err(err).Msg("Cannot encode name-strings")
}
r := bytes.NewReader(req)
resp, err := http.Post(mr.url+"matches", "application/json", r)
if err != nil {
log.Warn().Err(err).Msg("Cannot get matches response")
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warn().Err(err).Msg("Cannot read matches from response")
}
err = mr.enc.Decode(respBytes, &response)
if err != nil {
log.Warn().Err(err).Msg("Cannot decode matches")
}
return response
}
// GetConfig is a placeholder
func (mr matcherREST) GetConfig() gnmcfg.Config {
return gnmcfg.New()
}