Skip to content

Commit

Permalink
simplify: net/http/httpsimple: merge do.go into simpleclient.go
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Nov 20, 2023
1 parent 724d724 commit 0f8ca0c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 56 deletions.
56 changes: 0 additions & 56 deletions net/http/httpsimple/do.go

This file was deleted.

49 changes: 49 additions & 0 deletions net/http/httpsimple/simpleclient.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package httpsimple

import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"regexp"
Expand Down Expand Up @@ -65,3 +67,50 @@ func (sc *Client) DoUnmarshalJSON(req Request, resBody any) ([]byte, *http.Respo
err = json.Unmarshal(bytes, resBody)
return bytes, resp, err
}

func doSimple(client *http.Client, httpMethod, requrl string, headers map[string][]string, body []byte) (*http.Response, error) {
requrl = strings.TrimSpace(requrl)
if len(requrl) == 0 {
return nil, errors.New("requrl is required but not present")
}
if client == nil {
client = &http.Client{}
}
httpMethod = strings.TrimSpace(httpMethod)
if httpMethod == "" {
return nil, errors.New("httpMethod is required but not present")
}
var req *http.Request
var err error

if len(body) == 0 {
req, err = http.NewRequest(httpMethod, requrl, nil)
} else {
req, err = http.NewRequest(httpMethod, requrl, bytes.NewBuffer(body))
}
if err != nil {
return nil, err
}
for k, vals := range headers {
for _, v := range vals {
req.Header.Set(k, v)
}
}

return client.Do(req)
}

func Do(req Request) (*http.Response, error) {
sc := Client{}
return sc.Do(req)
}

func DoMore(req Request) ([]byte, *http.Response, error) {
sc := Client{}
resp, err := sc.Do(req)
if err != nil {
return []byte{}, resp, err
}
body, err := io.ReadAll(resp.Body)
return body, resp, err
}

0 comments on commit 0f8ca0c

Please sign in to comment.