2023-06-13 13:22:51 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
|
2024-12-16 13:47:40 +00:00
|
|
|
"gitea.pena/PenaSide/treasurer/pkg/json"
|
2023-06-13 13:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|