/
tagbox.go
74 lines (65 loc) · 1.75 KB
/
tagbox.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
// Package tagbox provides a client for accessing Tagbox services.
package tagbox
import (
"net/http"
"net/url"
"time"
"github.com/machinebox/sdk-go/boxutil"
"github.com/machinebox/sdk-go/internal/mbhttp"
"github.com/pkg/errors"
)
// Tag represents a single tag that describes an image.
type Tag struct {
// Tag is the tag string.
Tag string
// Confidence is a probability number between 0 and 1.
Confidence float64
// ID is unique identifier of the image, previosly teach
ID string
}
// Client is an HTTP client that can make requests to the box.
type Client struct {
addr string
// HTTPClient is the http.Client that will be used to
// make requests.
HTTPClient *http.Client
}
// make sure the Client implements boxutil.Box
var _ boxutil.Box = (*Client)(nil)
// New makes a new Client for the box at the specified address.
func New(addr string) *Client {
return &Client{
addr: addr,
HTTPClient: &http.Client{
Timeout: 1 * time.Minute,
},
}
}
// Info gets the details about the box.
func (c *Client) Info() (*boxutil.Info, error) {
var info boxutil.Info
u, err := url.Parse(c.addr + "/info")
if err != nil {
return nil, err
}
if !u.IsAbs() {
return nil, errors.New("box address must be absolute")
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json; charset=utf-8")
_, err = mbhttp.New("tagbox", c.HTTPClient).DoUnmarshal(req, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// CheckResponse is all the data from /check request to tagbox
type CheckResponse struct {
// Tags are the standard tags returned
Tags []Tag `json:"tags"`
// CustomTags are the custom tags (previously teach) that match
CustomTags []Tag `json:"custom_tags"`
}