@@ -2,13 +2,13 @@ package facebox
22
33import (
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}
0 commit comments