29 lines
578 B
Go
29 lines
578 B
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
|
|
"penahub.gitlab.yandexcloud.net/external/treasurer/pkg/json"
|
|
)
|
|
|
|
func parseResponse[T any](body []byte, response *http.Response) (*T, error) {
|
|
isJSONResponse := response.Header.Get("Content-Type") == "application/json"
|
|
|
|
if !isJSONResponse {
|
|
responseBody, unmarshalErr := json.Unmarshal[T](body)
|
|
if unmarshalErr != nil {
|
|
return nil, unmarshalErr
|
|
}
|
|
|
|
return responseBody, nil
|
|
}
|
|
|
|
responseBody, parseErr := json.Parse[T](bytes.NewReader(body))
|
|
if parseErr != nil {
|
|
return nil, parseErr
|
|
}
|
|
|
|
return responseBody, nil
|
|
}
|