2023-06-22 09:36:43 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-03-11 17:21:32 +00:00
|
|
|
"fmt"
|
2023-06-22 09:36:43 +00:00
|
|
|
"net/http"
|
|
|
|
|
2024-11-18 07:23:41 +00:00
|
|
|
"gitea.pena/PenaSide/customer/pkg/json"
|
2023-06-22 09:36:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func parseResponse[T any](body []byte, response *http.Response) (*T, error) {
|
|
|
|
isJSONResponse := response.Header.Get("Content-Type") == "application/json"
|
2024-03-11 17:21:32 +00:00
|
|
|
fmt.Println("1OOOO", response.Header.Get("Content-Type"), string(body))
|
2023-06-22 09:36:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|