customer/pkg/client/response.go
Pasha 34a88a3a70
Some checks failed
Lint / Lint (push) Failing after 1m2s
rename go.mod
2024-11-18 21:44:09 +00:00

31 lines
637 B
Go

package client
import (
"bytes"
"fmt"
"net/http"
"gitea.pena/PenaSide/customer/pkg/json"
)
func parseResponse[T any](body []byte, response *http.Response) (*T, error) {
isJSONResponse := response.Header.Get("Content-Type") == "application/json"
fmt.Println("1OOOO", response.Header.Get("Content-Type"), string(body))
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
}