Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,25 @@ func (c *Client) Request(
}
return request(ctx, c.httpClient, opts)
}
func (c *Client) RequestBytes(
ctx context.Context,
method string,
baseURL string,
endpoint string,
body io.Reader,
params map[string]string) ([]byte,error){
queryParams := url.Values{}
for key, value := range params {
queryParams.Set(key, value)
}
opts := &requestOptions{
method: method,
baseURL: baseURL,
endpoint: endpoint,
authString: c.authHeader,
payload: body,
queryParams: queryParams,
}
return requestBytes(ctx, c.httpClient, opts)

}
38 changes: 38 additions & 0 deletions pkg/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,43 @@ func request(
if err != nil {
return "", err
}

return formattedJSON.String(), nil
}

func requestBytes(
ctx context.Context,
client httpClient,
opts *requestOptions) ([]byte, error) {

req, err := http.NewRequestWithContext(ctx, opts.method, opts.baseURL, opts.payload)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", opts.authString)
req.Header.Add("Content-Type", `application/json`)
req.URL.Path += opts.endpoint

if opts.queryParams != nil {
req.URL.RawQuery = opts.queryParams.Encode()
}

resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var formattedJSON bytes.Buffer
err = json.Indent(&formattedJSON, body, "", "\t")
if err != nil {
return nil, err
}

return formattedJSON.Bytes(), nil
}