Skip to content

Commit 0abb2ee

Browse files
committed
Merge branch 'add-contacts' into add-groups
2 parents ae48fd7 + 7dccb97 commit 0abb2ee

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

client.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,30 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
109109
c.DebugLog.Printf("HTTP RESPONSE: %s", string(responseBody))
110110
}
111111

112-
// Status code 500 is a server error and means nothing can be done at this
113-
// point.
114-
if response.StatusCode == http.StatusInternalServerError {
115-
return ErrUnexpectedResponse
116-
}
117-
118-
// Status code 204 is returned for successful DELETE requests. Don't try to
119-
// unmarshal the body: that would return errors.
120-
if response.StatusCode == http.StatusNoContent && response.ContentLength == 0 {
121-
return nil
122-
}
123-
124-
// Status codes 200 and 201 are indicative of being able to convert the
125-
// response body to the struct that was specified.
126-
if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusCreated {
112+
switch response.StatusCode {
113+
case http.StatusOK, http.StatusCreated:
114+
// Status codes 200 and 201 are indicative of being able to convert the
115+
// response body to the struct that was specified.
127116
if err := json.Unmarshal(responseBody, &v); err != nil {
128117
return fmt.Errorf("could not decode response JSON, %s: %v", string(responseBody), err)
129118
}
119+
130120
return nil
131-
}
121+
case http.StatusNoContent:
122+
// Status code 204 is returned for successful DELETE requests. Don't try to
123+
// unmarshal the body: that would return errors.
124+
return nil
125+
case http.StatusInternalServerError:
126+
// Status code 500 is a server error and means nothing can be done at this
127+
// point.
128+
return ErrUnexpectedResponse
129+
default:
130+
// Anything else than a 200/201/204/500 should be a JSON error.
131+
var errorResponse ErrorResponse
132+
if err := json.Unmarshal(responseBody, &errorResponse); err != nil {
133+
return err
134+
}
132135

133-
// Anything else than a 200/201/500 should be a JSON error.
134-
var errorResponse ErrorResponse
135-
if err := json.Unmarshal(responseBody, &errorResponse); err != nil {
136-
return err
136+
return errorResponse
137137
}
138-
139-
return errorResponse
140138
}

internal/messagebirdtest/test_server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func closeServer() {
6161
server.Close()
6262
}
6363

64+
// WillReturn sets the response body (b) and status (s) to be returned by the
65+
// server for incoming requests. These can be used by tests to assert
66+
// unmarshalling responses works as intended.
6467
func WillReturn(b []byte, s int) {
6568
responseBody = b
6669
status = s

0 commit comments

Comments
 (0)