Skip to content

Commit

Permalink
Merge postJson into makeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
jungaretti committed Oct 15, 2024
1 parent 390ba48 commit 536f08d
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions pkg/providers/porkbun/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ func (r baseRes) checkStatus() error {
var _ checkable = baseRes{}

func makeRequest(url string, req interface{}, out checkable) error {
res, err := postJson(url, req)
jsonData, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("making POST request: %v", err)
return err
}
defer res.Body.Close()

res, err := http.Post(url, "application/json", bytes.NewReader(jsonData))
if err != nil {
return fmt.Errorf("making POST request: %v", err)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return fmt.Errorf("received non-success status code: %d", res.StatusCode)
}
Expand All @@ -58,12 +61,3 @@ func makeRequest(url string, req interface{}, out checkable) error {

return nil
}

func postJson(url string, body interface{}) (*http.Response, error) {
jsonData, err := json.Marshal(body)
if err != nil {
return nil, err
}

return http.Post(url, "application/json", bytes.NewReader(jsonData))
}

0 comments on commit 536f08d

Please sign in to comment.