Skip to content

Commit

Permalink
fix(pim-client): resolve invalid logic for building a request (#76)
Browse files Browse the repository at this point in the history
fix(pim-client): resolve invalid logic for building a request dynamically
  • Loading branch information
netr0m authored Nov 21, 2024
1 parent 7462bc6 commit ece6a96
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions pkg/pim/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,37 @@ func GetUserInfo(token string) AzureUserInfo {
return *claims.AzureUserInfo
}

func handleRequestErr(_error *common.Error, err error, req *http.Request) {
_error.Message = err.Error()
_error.Err = err
_error.Request = req
slog.Error(_error.Error())
slog.Debug(_error.Debug())
os.Exit(1)
}

func Request(request *PIMRequest, responseModel any) any {
// Prepare request body
var req *http.Request
var err error
var payload io.Reader
var _error = common.Error{
Operation: "Request",
}

if request.Payload != nil {
payload := new(bytes.Buffer)
json.NewEncoder(payload).Encode(request.Payload) //nolint:errcheck
req, err = http.NewRequest(request.Method, request.Url, payload)
if err != nil {
handleRequestErr(&_error, err, req)
}
} else {
payload = nil
}
req, err = http.NewRequest(request.Method, request.Url, payload)
if err != nil {
_error.Message = err.Error()
_error.Err = err
_error.Request = req
slog.Error(_error.Error())
slog.Debug(_error.Debug())
os.Exit(1)
req, err = http.NewRequest(request.Method, request.Url, nil)
if err != nil {
handleRequestErr(&_error, err, req)
}
}

// Add headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
Expand Down

0 comments on commit ece6a96

Please sign in to comment.