Skip to content

Commit b05e677

Browse files
authored
Use io.ReadCloser + json.NewDecoder (#88)
* Use io.ReadCloser + json.NewDecoder In order to use json.NewDecoder, we have to operate on an I/O stream. So, we swap out passing around a byteslice for this instead. This means we can't defer closure of the body stream within the `doRawRequest` method and have to unwrap it into individual close statements. But we can in the higher level `doRequest` method. Finally, in `Repository.GetFileBlob`, we utilize `ioutil.ReadAll` to replicate the original functionality to consume the response body. * Restore original error message
1 parent 470011f commit b05e677

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

client.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"log"
77

8-
"io/ioutil"
98
"net/http"
109
"net/url"
1110
"strconv"
@@ -151,7 +150,7 @@ func (c *Client) SetApiBaseURL(urlStr string) {
151150
c.apiBaseURL = urlStr
152151
}
153152

154-
func (c *Client) executeRaw(method string, urlStr string, text string) ([]byte, error) {
153+
func (c *Client) executeRaw(method string, urlStr string, text string) (io.ReadCloser, error) {
155154
body := strings.NewReader(text)
156155

157156
req, err := http.NewRequest(method, urlStr, body)
@@ -301,43 +300,43 @@ func (c *Client) authenticateRequest(req *http.Request) {
301300
}
302301

303302
func (c *Client) doRequest(req *http.Request, emptyResponse bool) (interface{}, error) {
304-
resBodyBytes, err := c.doRawRequest(req, emptyResponse)
303+
resBody, err := c.doRawRequest(req, emptyResponse)
305304
if err != nil {
306305
return nil, err
307306
}
307+
defer resBody.Close()
308308

309309
var result interface{}
310-
err = json.Unmarshal(resBodyBytes, &result)
311-
if err != nil {
310+
if err := json.NewDecoder(resBody).Decode(&result); err != nil {
312311
log.Println("Could not unmarshal JSON payload, returning raw response")
313-
return resBodyBytes, err
312+
return resBody, err
314313
}
315314

316315
return result, nil
317316
}
318317

319-
func (c *Client) doRawRequest(req *http.Request, emptyResponse bool) ([]byte, error) {
318+
func (c *Client) doRawRequest(req *http.Request, emptyResponse bool) (io.ReadCloser, error) {
320319
resp, err := c.HttpClient.Do(req)
321320
if err != nil {
322321
return nil, err
323322
}
324-
if resp.Body != nil {
325-
defer resp.Body.Close()
326-
}
327323

328324
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusCreated) {
325+
resp.Body.Close()
329326
return nil, fmt.Errorf(resp.Status)
330327
}
331328

332329
if emptyResponse {
330+
resp.Body.Close()
333331
return nil, nil
334332
}
335333

336334
if resp.Body == nil {
335+
resp.Body.Close()
337336
return nil, fmt.Errorf("response body is nil")
338337
}
339338

340-
return ioutil.ReadAll(resp.Body)
339+
return resp.Body, nil
341340
}
342341

343342
func (c *Client) requestUrl(template string, args ...interface{}) string {

repository.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bitbucket
22

33
import (
44
"encoding/json"
5+
"io/ioutil"
56
"net/url"
67
"os"
78
"path"
@@ -162,7 +163,12 @@ func (r *Repository) GetFileBlob(ro *RepositoryBlobOptions) (*RepositoryBlob, er
162163
return nil, err
163164
}
164165

165-
blob := RepositoryBlob{Content: response}
166+
content, err := ioutil.ReadAll(response)
167+
if err != nil {
168+
return nil, err
169+
}
170+
171+
blob := RepositoryBlob{Content: content}
166172

167173
return &blob, nil
168174
}

0 commit comments

Comments
 (0)