Skip to content

Commit 28e4a50

Browse files
authored
Merge pull request #8 from machinebox/statuscheck
Status check
2 parents f1c012e + 8dc877e commit 28e4a50

File tree

9 files changed

+75
-24
lines changed

9 files changed

+75
-24
lines changed

facebox/facebox.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func (c *Client) Info() (*boxutil.Info, error) {
7474
return nil, err
7575
}
7676
defer resp.Body.Close()
77+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
78+
return nil, errors.New(resp.Status)
79+
}
7780
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
7881
return nil, err
7982
}

facebox/facebox_check.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func (c *Client) Check(image io.Reader) ([]Face, error) {
4545
return nil, err
4646
}
4747
defer resp.Body.Close()
48+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
49+
return nil, errors.New(resp.Status)
50+
}
4851
return c.parseCheckResponse(resp.Body)
4952
}
5053

@@ -73,6 +76,9 @@ func (c *Client) CheckURL(imageURL *url.URL) ([]Face, error) {
7376
return nil, err
7477
}
7578
defer resp.Body.Close()
79+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
80+
return nil, errors.New(resp.Status)
81+
}
7682
return c.parseCheckResponse(resp.Body)
7783
}
7884

@@ -98,6 +104,9 @@ func (c *Client) CheckBase64(data string) ([]Face, error) {
98104
return nil, err
99105
}
100106
defer resp.Body.Close()
107+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
108+
return nil, errors.New(resp.Status)
109+
}
101110
return c.parseCheckResponse(resp.Body)
102111
}
103112

facebox/facebox_rename.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ func (c *Client) Rename(id, name string) error {
4040
return err
4141
}
4242
defer resp.Body.Close()
43-
err = c.parseResponse(resp.Body)
44-
if err != nil {
45-
return err
43+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
44+
return errors.New(resp.Status)
4645
}
47-
return nil
46+
return c.parseResponse(resp.Body)
4847
}
4948

5049
// RenameAll changes the name for all the faces that match a given name
@@ -79,9 +78,8 @@ func (c *Client) RenameAll(oldName, newName string) error {
7978
return err
8079
}
8180
defer resp.Body.Close()
82-
err = c.parseResponse(resp.Body)
83-
if err != nil {
84-
return err
81+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
82+
return errors.New(resp.Status)
8583
}
86-
return nil
84+
return c.parseResponse(resp.Body)
8785
}

facebox/facebox_similar.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func (c *Client) Similar(image io.Reader) ([]Similar, error) {
4545
return nil, err
4646
}
4747
defer resp.Body.Close()
48+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
49+
return nil, errors.New(resp.Status)
50+
}
4851
return c.parseSimilarResponse(resp.Body)
4952
}
5053

@@ -73,6 +76,9 @@ func (c *Client) SimilarURL(imageURL *url.URL) ([]Similar, error) {
7376
return nil, err
7477
}
7578
defer resp.Body.Close()
79+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
80+
return nil, errors.New(resp.Status)
81+
}
7682
return c.parseSimilarResponse(resp.Body)
7783
}
7884

@@ -101,6 +107,9 @@ func (c *Client) SimilarID(id string) ([]Similar, error) {
101107
return nil, err
102108
}
103109
defer resp.Body.Close()
110+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
111+
return nil, errors.New(resp.Status)
112+
}
104113
return c.parseSimilarResponse(resp.Body)
105114
}
106115

@@ -126,6 +135,9 @@ func (c *Client) SimilarBase64(data string) ([]Similar, error) {
126135
return nil, err
127136
}
128137
defer resp.Body.Close()
138+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
139+
return nil, errors.New(resp.Status)
140+
}
129141
return c.parseSimilarResponse(resp.Body)
130142
}
131143

facebox/facebox_state.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (c *Client) OpenState() (io.ReadCloser, error) {
2929
if err != nil {
3030
return nil, err
3131
}
32+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
33+
return nil, errors.New(resp.Status)
34+
}
3235
return resp.Body, nil
3336
}
3437

@@ -65,6 +68,9 @@ func (c *Client) PostState(r io.Reader) error {
6568
return err
6669
}
6770
defer resp.Body.Close()
71+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
72+
return errors.New(resp.Status)
73+
}
6874
return c.parseResponse(resp.Body)
6975
}
7076

@@ -94,9 +100,8 @@ func (c *Client) PostStateURL(stateURL *url.URL) error {
94100
return err
95101
}
96102
defer resp.Body.Close()
97-
err = c.parseResponse(resp.Body)
98-
if err != nil {
99-
return err
103+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
104+
return errors.New(resp.Status)
100105
}
101-
return nil
106+
return c.parseResponse(resp.Body)
102107
}

facebox/facebox_teach.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func (c *Client) Teach(image io.Reader, id, name string) error {
5757
return err
5858
}
5959
defer resp.Body.Close()
60+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
61+
return errors.New(resp.Status)
62+
}
6063
return c.parseResponse(resp.Body)
6164
}
6265

@@ -88,11 +91,10 @@ func (c *Client) TeachURL(imageURL *url.URL, id, name string) error {
8891
return err
8992
}
9093
defer resp.Body.Close()
91-
err = c.parseResponse(resp.Body)
92-
if err != nil {
93-
return err
94+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
95+
return errors.New(resp.Status)
9496
}
95-
return nil
97+
return c.parseResponse(resp.Body)
9698
}
9799

98100
// TeachBase64 teaches facebox the face in the Base64 encoded image.
@@ -120,11 +122,10 @@ func (c *Client) TeachBase64(data, id, name string) error {
120122
return err
121123
}
122124
defer resp.Body.Close()
123-
err = c.parseResponse(resp.Body)
124-
if err != nil {
125-
return err
125+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
126+
return errors.New(resp.Status)
126127
}
127-
return nil
128+
return c.parseResponse(resp.Body)
128129
}
129130

130131
// Remove makes facebox to forget a face
@@ -153,11 +154,10 @@ func (c *Client) Remove(id string) error {
153154
return err
154155
}
155156
defer resp.Body.Close()
156-
err = c.parseResponse(resp.Body)
157-
if err != nil {
158-
return err
157+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
158+
return errors.New(resp.Status)
159159
}
160-
return nil
160+
return c.parseResponse(resp.Body)
161161
}
162162

163163
func (c *Client) parseResponse(r io.Reader) error {

fakebox/fakebox.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func (c *Client) Info() (*boxutil.Info, error) {
108108
return nil, err
109109
}
110110
defer resp.Body.Close()
111+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
112+
return nil, errors.New(resp.Status)
113+
}
111114
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
112115
return nil, err
113116
}
@@ -139,6 +142,9 @@ func (c *Client) Check(title string, content string, u *url.URL) (*Analysis, err
139142
return nil, err
140143
}
141144
defer resp.Body.Close()
145+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
146+
return nil, errors.New(resp.Status)
147+
}
142148
var response struct {
143149
Success bool
144150
Error string

nudebox/nudebox.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func (c *Client) Info() (*boxutil.Info, error) {
5757
return nil, err
5858
}
5959
defer resp.Body.Close()
60+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
61+
return nil, errors.New(resp.Status)
62+
}
6063
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
6164
return nil, err
6265
}
@@ -95,6 +98,9 @@ func (c *Client) Check(image io.Reader) (float64, error) {
9598
if err != nil {
9699
return 0, err
97100
}
101+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
102+
return 0, errors.New(resp.Status)
103+
}
98104
return c.parseCheckResponse(resp.Body)
99105
}
100106

@@ -123,6 +129,9 @@ func (c *Client) CheckURL(imageURL *url.URL) (float64, error) {
123129
return 0, err
124130
}
125131
defer resp.Body.Close()
132+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
133+
return 0, errors.New(resp.Status)
134+
}
126135
return c.parseCheckResponse(resp.Body)
127136
}
128137

@@ -148,6 +157,9 @@ func (c *Client) CheckBase64(data string) (float64, error) {
148157
return 0, err
149158
}
150159
defer resp.Body.Close()
160+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
161+
return 0, errors.New(resp.Status)
162+
}
151163
return c.parseCheckResponse(resp.Body)
152164
}
153165

textbox/textbox.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func (c *Client) Info() (*boxutil.Info, error) {
9191
return nil, err
9292
}
9393
defer resp.Body.Close()
94+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
95+
return nil, errors.New(resp.Status)
96+
}
9497
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
9598
return nil, err
9699
}
@@ -123,6 +126,9 @@ func (c *Client) Check(r io.Reader) (*Analysis, error) {
123126
return nil, err
124127
}
125128
defer resp.Body.Close()
129+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
130+
return nil, errors.New(resp.Status)
131+
}
126132
var response struct {
127133
Success bool
128134
Error string

0 commit comments

Comments
 (0)