You've already forked email-api-client
refactor: streamline response handling by introducing readResponseBody utility
All checks were successful
🚀 Publish Release Package / publish (push) Successful in 22s
All checks were successful
🚀 Publish Release Package / publish (push) Successful in 22s
This commit is contained in:
26
client/response.go
Normal file
26
client/response.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func readResponseBody[T any](res *http.Response) (*T, error) {
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(res.Body)
|
||||
|
||||
responseBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var target T
|
||||
err = json.Unmarshal(responseBody, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &target, nil
|
||||
}
|
||||
Reference in New Issue
Block a user