Skip to content

Commit

Permalink
client: Use a single constructor for http.Client
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac committed Apr 2, 2020
1 parent f8ddaf7 commit e535af2
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
type Api struct {
serverUrl string
config Config
client http.Client
}

type NetInfo struct {
Expand Down Expand Up @@ -119,7 +120,11 @@ type TufCustom struct {
}

func NewApiClient(serverUrl string, config Config) *Api {
api := Api{strings.TrimRight(serverUrl, "/"), config}
api := Api{
serverUrl: strings.TrimRight(serverUrl, "/"),
config: config,
client: http.Client{Timeout: time.Second * 10},
}
return &api
}

Expand Down Expand Up @@ -147,10 +152,6 @@ func (a *Api) setReqHeaders(req *http.Request, jsonContent bool) {
}

func (a *Api) RawGet(url string, headers *map[string]string) (*http.Response, error) {
client := http.Client{
Timeout: time.Second * 10,
}

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
Expand All @@ -163,7 +164,7 @@ func (a *Api) RawGet(url string, headers *map[string]string) (*http.Response, er
}
}

return client.Do(req)
return a.client.Do(req)
}

func (a *Api) Get(url string) (*[]byte, error) {
Expand All @@ -184,18 +185,14 @@ func (a *Api) Get(url string) (*[]byte, error) {
}

func (a *Api) Patch(url string, data []byte) (*[]byte, error) {
client := http.Client{
Timeout: time.Second * 10,
}

req, err := http.NewRequest(http.MethodPatch, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}

a.setReqHeaders(req, true)

res, err := client.Do(req)
res, err := a.client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -212,18 +209,14 @@ func (a *Api) Patch(url string, data []byte) (*[]byte, error) {
}

func (a *Api) Post(url string, data []byte) (*[]byte, error) {
client := http.Client{
Timeout: time.Second * 10,
}

req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}

a.setReqHeaders(req, true)

res, err := client.Do(req)
res, err := a.client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -240,18 +233,14 @@ func (a *Api) Post(url string, data []byte) (*[]byte, error) {
}

func (a *Api) Put(url string, data []byte) (*[]byte, error) {
client := http.Client{
Timeout: time.Second * 10,
}

req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}

a.setReqHeaders(req, true)

res, err := client.Do(req)
res, err := a.client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -268,18 +257,14 @@ func (a *Api) Put(url string, data []byte) (*[]byte, error) {
}

func (a *Api) Delete(url string, data []byte) (*[]byte, error) {
client := http.Client{
Timeout: time.Second * 10,
}

req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}

a.setReqHeaders(req, true)

res, err := client.Do(req)
res, err := a.client.Do(req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e535af2

Please sign in to comment.