IBM / keyprotect-go-client

Go SDK for interacting with the IBM Cloud KeyProtect service.
Apache License 2.0
6 stars 29 forks source link

APIs should also return raw http.Response #74

Open ghost opened 2 years ago

ghost commented 2 years ago

In order to allow user full functionality and inspection of response, the API should return the full Response object.

Example (https://github.com/IBM/keyprotect-go-client/blob/88feabbd3d762f6940726cc9f34231b8c3606748/keys.go#L207-L229 ):

func (c *Client) GetKeys(ctx context.Context, limit int, offset int) (*Keys, *http.Response, error) {
    if limit == 0 {
        limit = 2000
    }

    req, err := c.newRequest("GET", "keys", nil)
    if err != nil {
        return nil, err
    }

    v := url.Values{}
    v.Set("limit", strconv.Itoa(limit))
    v.Set("offset", strconv.Itoa(offset))
    req.URL.RawQuery = v.Encode()

    keys := Keys{}
    resp, err = c.do(ctx, req, &keys)
    if err != nil {
        return nil, err
    }

    return &keys, resp, nil
}

As a note this also means that the response body should be recreated after reading so the body could be read again if needed (https://github.com/IBM/keyprotect-go-client/blob/88feabbd3d762f6940726cc9f34231b8c3606748/kp.go#L272)