Skip to content

Commit 51030cc

Browse files
matryerdahernan
authored andcommitted
Better client (#25)
Added better mbclient for all the boxes
1 parent a0b0d8c commit 51030cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+251
-588
lines changed

classificationbox/classificationbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *Client) Info() (*boxutil.Info, error) {
4949
return nil, err
5050
}
5151
req.Header.Set("Accept", "application/json; charset=utf-8")
52-
_, err = c.client.Do(req, &info)
52+
_, err = c.client.DoUnmarshal(req, &info)
5353
if err != nil {
5454
return nil, err
5555
}

classificationbox/classificationbox_model.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (c *Client) CreateModel(ctx context.Context, model Model) (Model, error) {
7676
req.Header.Set("Accept", "application/json; charset=utf-8")
7777
req.Header.Set("Content-Type", "application/json; charset=utf-8")
7878
var outModel Model
79-
_, err = c.client.Do(req, &outModel)
79+
_, err = c.client.DoUnmarshal(req, &outModel)
8080
if err != nil {
8181
return outModel, err
8282
}
@@ -101,7 +101,7 @@ func (c *Client) ListModels(ctx context.Context) ([]Model, error) {
101101
var response struct {
102102
Models []Model
103103
}
104-
_, err = c.client.Do(req, &response)
104+
_, err = c.client.DoUnmarshal(req, &response)
105105
if err != nil {
106106
return nil, err
107107
}
@@ -124,7 +124,7 @@ func (c *Client) GetModel(ctx context.Context, modelID string) (Model, error) {
124124
}
125125
req = req.WithContext(ctx)
126126
req.Header.Set("Accept", "application/json; charset=utf-8")
127-
_, err = c.client.Do(req, &model)
127+
_, err = c.client.DoUnmarshal(req, &model)
128128
if err != nil {
129129
return model, err
130130
}
@@ -146,7 +146,7 @@ func (c *Client) DeleteModel(ctx context.Context, modelID string) error {
146146
}
147147
req = req.WithContext(ctx)
148148
req.Header.Set("Accept", "application/json; charset=utf-8")
149-
_, err = c.client.Do(req, nil)
149+
_, err = c.client.DoUnmarshal(req, nil)
150150
if err != nil {
151151
return err
152152
}
@@ -246,7 +246,7 @@ func (c *Client) GetModelStats(ctx context.Context, modelID string) (ModelStats,
246246
}
247247
req = req.WithContext(ctx)
248248
req.Header.Set("Accept", "application/json; charset=utf-8")
249-
_, err = c.client.Do(req, &stats)
249+
_, err = c.client.DoUnmarshal(req, &stats)
250250
if err != nil {
251251
return stats, err
252252
}

classificationbox/classificationbox_predict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *Client) Predict(ctx context.Context, modelID string, request PredictReq
5858
req = req.WithContext(ctx)
5959
req.Header.Set("Accept", "application/json; charset=utf-8")
6060
req.Header.Set("Content-Type", "application/json; charset=utf-8")
61-
_, err = c.client.Do(req, &response)
61+
_, err = c.client.DoUnmarshal(req, &response)
6262
if err != nil {
6363
return response, err
6464
}

classificationbox/classificationbox_state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *Client) PostState(ctx context.Context, r io.Reader, predictOnly bool) (
7070
req = req.WithContext(ctx)
7171
req.Header.Set("Accept", "application/json; charset=utf-8")
7272
req.Header.Set("Content-Type", w.FormDataContentType())
73-
_, err = c.client.Do(req, &model)
73+
_, err = c.client.DoUnmarshal(req, &model)
7474
if err != nil {
7575
return model, err
7676
}
@@ -103,7 +103,7 @@ func (c *Client) PostStateURL(ctx context.Context, stateURL *url.URL, predictOnl
103103
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
104104
req.Header.Set("Accept", "application/json; charset=utf-8")
105105
req = req.WithContext(ctx)
106-
_, err = c.client.Do(req, &model)
106+
_, err = c.client.DoUnmarshal(req, &model)
107107
if err != nil {
108108
return model, err
109109
}

classificationbox/classificationbox_teach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (c *Client) Teach(ctx context.Context, modelID string, example Example) err
3838
req = req.WithContext(ctx)
3939
req.Header.Set("Accept", "application/json; charset=utf-8")
4040
req.Header.Set("Content-Type", "application/json; charset=utf-8")
41-
_, err = c.client.Do(req, nil)
41+
_, err = c.client.DoUnmarshal(req, nil)
4242
if err != nil {
4343
return err
4444
}
@@ -70,7 +70,7 @@ func (c *Client) TeachMulti(ctx context.Context, modelID string, examples []Exam
7070
req = req.WithContext(ctx)
7171
req.Header.Set("Accept", "application/json; charset=utf-8")
7272
req.Header.Set("Content-Type", "application/json; charset=utf-8")
73-
_, err = c.client.Do(req, nil)
73+
_, err = c.client.DoUnmarshal(req, nil)
7474
if err != nil {
7575
return err
7676
}

facebox/facebox.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
package facebox
33

44
import (
5-
"encoding/json"
65
"errors"
76
"net/http"
87
"net/url"
98
"time"
109

10+
"github.com/machinebox/sdk-go/internal/mbhttp"
11+
1112
"github.com/machinebox/sdk-go/boxutil"
1213
)
1314

@@ -71,23 +72,9 @@ func (c *Client) Info() (*boxutil.Info, error) {
7172
return nil, err
7273
}
7374
req.Header.Set("Accept", "application/json; charset=utf-8")
74-
resp, err := c.HTTPClient.Do(req)
75+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, &info)
7576
if err != nil {
7677
return nil, err
7778
}
78-
defer resp.Body.Close()
79-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
80-
return nil, errors.New(resp.Status)
81-
}
82-
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
83-
return nil, err
84-
}
8579
return &info, nil
8680
}
87-
88-
// ErrFacebox represents an error from Facebox.
89-
type ErrFacebox string
90-
91-
func (e ErrFacebox) Error() string {
92-
return "facebox: " + string(e)
93-
}

facebox/facebox_check.go

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package facebox
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"io"
76
"mime/multipart"
87
"net/http"
98
"net/url"
109
"strings"
1110

11+
"github.com/machinebox/sdk-go/internal/mbhttp"
1212
"github.com/pkg/errors"
1313
)
1414

@@ -40,15 +40,14 @@ func (c *Client) Check(image io.Reader) ([]Face, error) {
4040
}
4141
req.Header.Set("Accept", "application/json; charset=utf-8")
4242
req.Header.Set("Content-Type", w.FormDataContentType())
43-
resp, err := c.HTTPClient.Do(req)
43+
var checkResponse struct {
44+
Faces []Face
45+
}
46+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, &checkResponse)
4447
if err != nil {
4548
return nil, err
4649
}
47-
defer resp.Body.Close()
48-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
49-
return nil, errors.New(resp.Status)
50-
}
51-
return c.parseCheckResponse(resp.Body)
50+
return checkResponse.Faces, nil
5251
}
5352

5453
// CheckURL checks the image at the specified URL for faces.
@@ -71,15 +70,14 @@ func (c *Client) CheckURL(imageURL *url.URL) ([]Face, error) {
7170
}
7271
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
7372
req.Header.Set("Accept", "application/json; charset=utf-8")
74-
resp, err := c.HTTPClient.Do(req)
73+
var checkResponse struct {
74+
Faces []Face
75+
}
76+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, &checkResponse)
7577
if err != nil {
7678
return nil, err
7779
}
78-
defer resp.Body.Close()
79-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
80-
return nil, errors.New(resp.Status)
81-
}
82-
return c.parseCheckResponse(resp.Body)
80+
return checkResponse.Faces, nil
8381
}
8482

8583
// CheckBase64 checks the Base64 encoded image for faces.
@@ -111,28 +109,12 @@ func (c *Client) checkBase64WithOptions(data string, options map[string]string)
111109
}
112110
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
113111
req.Header.Set("Accept", "application/json; charset=utf-8")
114-
resp, err := c.HTTPClient.Do(req)
115-
if err != nil {
116-
return nil, err
117-
}
118-
defer resp.Body.Close()
119-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
120-
return nil, errors.New(resp.Status)
121-
}
122-
return c.parseCheckResponse(resp.Body)
123-
}
124-
125-
func (c *Client) parseCheckResponse(r io.Reader) ([]Face, error) {
126112
var checkResponse struct {
127-
Success bool
128-
Error string
129-
Faces []Face
130-
}
131-
if err := json.NewDecoder(r).Decode(&checkResponse); err != nil {
132-
return nil, errors.Wrap(err, "decoding response")
113+
Faces []Face
133114
}
134-
if !checkResponse.Success {
135-
return nil, ErrFacebox(checkResponse.Error)
115+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, &checkResponse)
116+
if err != nil {
117+
return nil, err
136118
}
137119
return checkResponse.Faces, nil
138120
}

facebox/facebox_faceprint.go

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package facebox
33
import (
44
"bytes"
55
"encoding/json"
6-
"io"
76
"net/http"
87
"net/url"
98

9+
"github.com/machinebox/sdk-go/internal/mbhttp"
1010
"github.com/pkg/errors"
1111
)
1212

@@ -39,38 +39,21 @@ func (c *Client) CompareFaceprints(target string, faceprintCandidates []string)
3939
}
4040
req.Header.Set("Accept", "application/json; charset=utf-8")
4141
req.Header.Set("Content-Type", "application/json; charset=utf-8")
42-
43-
resp, err := c.HTTPClient.Do(req)
42+
var compareFaceprintsResponse struct {
43+
Confidences []float64
44+
}
45+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, &compareFaceprintsResponse)
4446
if err != nil {
4547
return nil, err
4648
}
47-
defer resp.Body.Close()
48-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
49-
return nil, errors.New(resp.Status)
50-
}
51-
return c.parseCompareFacesResponse(resp.Body)
49+
return compareFaceprintsResponse.Confidences, nil
5250
}
5351

5452
type compareFaceprintRequest struct {
5553
Faceprints []string `json:"faceprints"`
5654
Target string `json:"target"`
5755
}
5856

59-
func (c *Client) parseCompareFacesResponse(r io.Reader) ([]float64, error) {
60-
var compareFaceprintsResponse struct {
61-
Success bool
62-
Error string
63-
Confidences []float64
64-
}
65-
if err := json.NewDecoder(r).Decode(&compareFaceprintsResponse); err != nil {
66-
return nil, errors.Wrap(err, "decoding response")
67-
}
68-
if !compareFaceprintsResponse.Success {
69-
return nil, ErrFacebox(compareFaceprintsResponse.Error)
70-
}
71-
return compareFaceprintsResponse.Confidences, nil
72-
}
73-
7457
type checkFaceprintRequest struct {
7558
Faceprints []string `json:"faceprints"`
7659
}
@@ -101,29 +84,12 @@ func (c *Client) CheckFaceprints(faceprints []string) ([]Face, error) {
10184
}
10285
req.Header.Set("Accept", "application/json; charset=utf-8")
10386
req.Header.Set("Content-Type", "application/json; charset=utf-8")
104-
105-
resp, err := c.HTTPClient.Do(req)
106-
if err != nil {
107-
return nil, err
108-
}
109-
defer resp.Body.Close()
110-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
111-
return nil, errors.New(resp.Status)
112-
}
113-
return c.parseCheckFaceprintResponse(resp.Body)
114-
}
115-
116-
func (c *Client) parseCheckFaceprintResponse(r io.Reader) ([]Face, error) {
11787
var checkResponse struct {
118-
Success bool
119-
Error string
12088
Faceprints []Face
12189
}
122-
if err := json.NewDecoder(r).Decode(&checkResponse); err != nil {
123-
return nil, errors.Wrap(err, "decoding response")
124-
}
125-
if !checkResponse.Success {
126-
return nil, ErrFacebox(checkResponse.Error)
90+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, &checkResponse)
91+
if err != nil {
92+
return nil, err
12793
}
12894
return checkResponse.Faceprints, nil
12995
}

facebox/facebox_rename.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66
"strings"
77

8+
"github.com/machinebox/sdk-go/internal/mbhttp"
89
"github.com/pkg/errors"
910
)
1011

@@ -35,15 +36,11 @@ func (c *Client) Rename(id, name string) error {
3536
}
3637
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
3738
req.Header.Set("Accept", "application/json; charset=utf-8")
38-
resp, err := c.HTTPClient.Do(req)
39+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
3940
if err != nil {
4041
return err
4142
}
42-
defer resp.Body.Close()
43-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
44-
return errors.New(resp.Status)
45-
}
46-
return c.parseResponse(resp.Body)
43+
return nil
4744
}
4845

4946
// RenameAll changes the name for all the faces that match a given name
@@ -73,13 +70,9 @@ func (c *Client) RenameAll(oldName, newName string) error {
7370
}
7471
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
7572
req.Header.Set("Accept", "application/json; charset=utf-8")
76-
resp, err := c.HTTPClient.Do(req)
73+
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
7774
if err != nil {
7875
return err
7976
}
80-
defer resp.Body.Close()
81-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
82-
return errors.New(resp.Status)
83-
}
84-
return c.parseResponse(resp.Body)
77+
return nil
8578
}

0 commit comments

Comments
 (0)