Skip to content

Commit 92edc64

Browse files
committed
objectbox: use improved client code
1 parent 629c187 commit 92edc64

File tree

5 files changed

+98
-56
lines changed

5 files changed

+98
-56
lines changed

internal/mbhttp/client.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package mbhttp
33

44
import (
55
"encoding/json"
6+
"fmt"
67
"io/ioutil"
78
"net/http"
9+
"strings"
810

911
"github.com/pkg/errors"
1012
)
@@ -27,6 +29,7 @@ func New(boxname string, client *http.Client) *Client {
2729
}
2830

2931
// Do makes the request and unmarshals the response into v.
32+
// The Body in the Response will be closed after calling this method.
3033
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
3134
resp, err := c.HTTPClient.Do(req)
3235
if err != nil {
@@ -43,18 +46,20 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
4346
}
4447
if err := json.Unmarshal(b, &o); err != nil {
4548
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
46-
return nil, errors.Errorf("%s: %s", c.boxname, resp.Status)
49+
return nil, errors.Errorf("%s: %d: %s", c.boxname, resp.StatusCode, strings.TrimSpace(string(b)))
4750
}
4851
return nil, errors.Wrap(err, "decode common response data")
4952
}
5053
if !o.Success {
5154
if o.Error == "" {
52-
o.Error = "an unknown error occurred in the box"
55+
o.Error = fmt.Sprintf("%d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
5356
}
5457
return nil, errors.Errorf("%s: %s", c.boxname, o.Error)
5558
}
56-
if err := json.Unmarshal(b, &v); err != nil {
57-
return nil, errors.Wrap(err, "decode response data")
59+
if v == nil {
60+
if err := json.Unmarshal(b, &v); err != nil {
61+
return nil, errors.Wrap(err, "decode response data")
62+
}
5863
}
5964
return resp, nil
6065
}

internal/mbhttp/client_test.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestDoBoxMissingError(t *testing.T) {
116116
var actualOut obj
117117
_, err = c.Do(req, &actualOut)
118118
is.True(err != nil)
119-
is.Equal(err.Error(), "testbox: an unknown error occurred in the box")
119+
is.Equal(err.Error(), `testbox: 200: {"success":false,"error":""}`)
120120
}
121121

122122
func TestDoHTTPError(t *testing.T) {
@@ -145,5 +145,72 @@ func TestDoHTTPError(t *testing.T) {
145145
var actualOut obj
146146
_, err = c.Do(req, &actualOut)
147147
is.True(err != nil)
148-
is.Equal(err.Error(), "testbox: 500 Internal Server Error")
148+
is.Equal(err.Error(), "testbox: 500: something went wrong")
149+
}
150+
151+
func TestDoNoResponseExpected(t *testing.T) {
152+
is := is.New(t)
153+
type obj struct {
154+
Field1 string `json:"field1"`
155+
Field2 int `json:"field2"`
156+
Field3 bool `json:"field3"`
157+
}
158+
in := obj{Field1: "in", Field2: 123, Field3: true}
159+
out := obj{Field1: "in", Field2: 123, Field3: true}
160+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
161+
var requestObj obj
162+
is.Equal(r.Method, http.MethodPost)
163+
is.Equal(r.URL.Path, "/something")
164+
is.NoErr(json.NewDecoder(r.Body).Decode(&requestObj))
165+
is.Equal(requestObj.Field1, in.Field1)
166+
is.Equal(requestObj.Field2, in.Field2)
167+
is.Equal(requestObj.Field3, in.Field3)
168+
is.NoErr(json.NewEncoder(w).Encode(struct {
169+
Success bool `json:"success"`
170+
obj
171+
}{
172+
Success: true,
173+
obj: out,
174+
}))
175+
}))
176+
defer srv.Close()
177+
var buf bytes.Buffer
178+
is.NoErr(json.NewEncoder(&buf).Encode(in))
179+
req, err := http.NewRequest(http.MethodPost, srv.URL+"/something", &buf)
180+
c := mbhttp.New("testbox", http.DefaultClient)
181+
_, err = c.Do(req, nil)
182+
is.NoErr(err)
183+
}
184+
func TestDoNoResponseExpectedWithError(t *testing.T) {
185+
is := is.New(t)
186+
type obj struct {
187+
Field1 string `json:"field1"`
188+
Field2 int `json:"field2"`
189+
Field3 bool `json:"field3"`
190+
}
191+
in := obj{Field1: "in", Field2: 123, Field3: true}
192+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
193+
var requestObj obj
194+
is.Equal(r.Method, http.MethodPost)
195+
is.Equal(r.URL.Path, "/something")
196+
is.NoErr(json.NewDecoder(r.Body).Decode(&requestObj))
197+
is.Equal(requestObj.Field1, in.Field1)
198+
is.Equal(requestObj.Field2, in.Field2)
199+
is.Equal(requestObj.Field3, in.Field3)
200+
is.NoErr(json.NewEncoder(w).Encode(struct {
201+
Success bool `json:"success"`
202+
Error string `json:"error"`
203+
}{
204+
Success: false,
205+
Error: "something went wrong",
206+
}))
207+
}))
208+
defer srv.Close()
209+
var buf bytes.Buffer
210+
is.NoErr(json.NewEncoder(&buf).Encode(in))
211+
req, err := http.NewRequest(http.MethodPost, srv.URL+"/something", &buf)
212+
c := mbhttp.New("testbox", http.DefaultClient)
213+
_, err = c.Do(req, nil)
214+
is.True(err != nil)
215+
is.Equal(err.Error(), "testbox: something went wrong")
149216
}

objectbox/objectbox.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package objectbox
33

44
import (
55
"encoding/json"
6-
"io"
76
"net/http"
87
"net/url"
98
"time"
@@ -89,17 +88,3 @@ type CheckDetectorResponse struct {
8988
Name string `json:"name"`
9089
Objects []Object `json:"objects"`
9190
}
92-
93-
func (c *Client) parseResponse(r io.Reader) error {
94-
var response struct {
95-
Success bool
96-
Error string
97-
}
98-
if err := json.NewDecoder(r).Decode(&response); err != nil {
99-
return errors.Wrap(err, "decoding response")
100-
}
101-
if !response.Success {
102-
return ErrObjectbox(response.Error)
103-
}
104-
return nil
105-
}

objectbox/objectbox_check.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package objectbox
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,12 +40,13 @@ func (c *Client) Check(image io.Reader) (CheckResponse, 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+
client := mbhttp.New("objectbox", c.HTTPClient)
44+
var response CheckResponse
45+
_, err = client.Do(req, &response)
4446
if err != nil {
4547
return CheckResponse{}, err
4648
}
47-
defer resp.Body.Close()
48-
return c.parseCheckResponse(resp.Body)
49+
return response, nil
4950
}
5051

5152
// CheckURL gets the tags for the image at the specified URL.
@@ -68,12 +69,13 @@ func (c *Client) CheckURL(imageURL *url.URL) (CheckResponse, error) {
6869
}
6970
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
7071
req.Header.Set("Accept", "application/json; charset=utf-8")
71-
resp, err := c.HTTPClient.Do(req)
72+
client := mbhttp.New("objectbox", c.HTTPClient)
73+
var response CheckResponse
74+
_, err = client.Do(req, &response)
7275
if err != nil {
7376
return CheckResponse{}, err
7477
}
75-
defer resp.Body.Close()
76-
return c.parseCheckResponse(resp.Body)
78+
return response, nil
7779
}
7880

7981
// CheckBase64 gets the tags for the image in the encoded Base64 data string.
@@ -93,26 +95,11 @@ func (c *Client) CheckBase64(data string) (CheckResponse, error) {
9395
}
9496
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
9597
req.Header.Set("Accept", "application/json; charset=utf-8")
96-
resp, err := c.HTTPClient.Do(req)
98+
client := mbhttp.New("objectbox", c.HTTPClient)
99+
var response CheckResponse
100+
_, err = client.Do(req, &response)
97101
if err != nil {
98102
return CheckResponse{}, err
99103
}
100-
defer resp.Body.Close()
101-
return c.parseCheckResponse(resp.Body)
102-
}
103-
104-
// parseCheckResponse parses the check response data.
105-
func (c *Client) parseCheckResponse(r io.Reader) (CheckResponse, error) {
106-
var resp struct {
107-
Success bool
108-
Error string
109-
CheckResponse
110-
}
111-
if err := json.NewDecoder(r).Decode(&resp); err != nil {
112-
return CheckResponse{}, errors.Wrap(err, "decoding response")
113-
}
114-
if !resp.Success {
115-
return CheckResponse{}, ErrObjectbox(resp.Error)
116-
}
117-
return resp.CheckResponse, nil
104+
return response, nil
118105
}

objectbox/objectbox_state.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"net/url"
1010
"strings"
11+
12+
"github.com/machinebox/sdk-go/internal/mbhttp"
1113
)
1214

1315
// PostState uploads new state data.
@@ -38,12 +40,12 @@ func (c *Client) PostState(r io.Reader) error {
3840
}
3941
req.Header.Set("Accept", "application/json; charset=utf-8")
4042
req.Header.Set("Content-Type", w.FormDataContentType())
41-
resp, err := c.HTTPClient.Do(req)
43+
client := mbhttp.New("objectbox", c.HTTPClient)
44+
_, err = client.Do(req, nil)
4245
if err != nil {
4346
return err
4447
}
45-
defer resp.Body.Close()
46-
return c.parseResponse(resp.Body)
48+
return nil
4749
}
4850

4951
// PostStateURL tells objectbox to download the state file specified
@@ -67,12 +69,8 @@ func (c *Client) PostStateURL(stateURL *url.URL) error {
6769
}
6870
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
6971
req.Header.Set("Accept", "application/json; charset=utf-8")
70-
resp, err := c.HTTPClient.Do(req)
71-
if err != nil {
72-
return err
73-
}
74-
defer resp.Body.Close()
75-
err = c.parseResponse(resp.Body)
72+
client := mbhttp.New("objectbox", c.HTTPClient)
73+
_, err = client.Do(req, nil)
7674
if err != nil {
7775
return err
7876
}

0 commit comments

Comments
 (0)