-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
55 lines (47 loc) · 1.5 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package raiderio
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
)
type apiErrorResponse struct {
StatusCode int `json:"statusCode"`
Err string `json:"error"`
Message string `json:"message"`
}
// getAPIResponse is a helper function that makes a GET request to the Raider.IO API
// It returns an error if the API returns a non-200 status code, or if the
// response body cannot be read
// Returns the error message from the api back to the client method that calls it,
// so in cases where the realm or the character name cannot be found, developer is presented
// with that error state.
func (c *Client) getAPIResponse(ctx context.Context, reqUrl string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, nil)
if err != nil {
return nil, errors.New("error creating HTTP request")
}
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, wrapHttpError(err)
}
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("error reading response body")
}
// If not 200, api is returning an error state
if resp.StatusCode != 200 {
var responseBody apiErrorResponse
err = json.Unmarshal(body, &responseBody)
// unmarshal error implies response is in an incorrect format
// instead of api message, return http status
if err != nil {
return nil, wrapApiError(&responseBody)
}
// return error with message directly from the api
return nil, wrapApiError(&responseBody)
}
return body, nil
}