Skip to content

Commit 4595d51

Browse files
committed
Moved http.Client{} to ScalewayAPI struct
1 parent ec0792d commit 4595d51

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

pkg/api/api.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type ScalewayAPI struct {
4343
// Cache is used to quickly resolve identifiers from names
4444
Cache *ScalewayCache
4545

46+
client *http.Client
4647
anonuuid anonuuid.AnonUUID
4748
}
4849

@@ -543,13 +544,17 @@ func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (*
543544
return nil, err
544545
}
545546
s := &ScalewayAPI{
547+
// exposed
546548
ComputeAPI: apiEndPoint,
547549
AccountAPI: accountEndPoint,
548550
APIUrl: apiEndPoint,
549551
Organization: organization,
550552
Token: token,
551553
Cache: cache,
552-
anonuuid: *anonuuid.New(),
554+
555+
// internal
556+
anonuuid: *anonuuid.New(),
557+
client: &http.Client{},
553558
}
554559

555560
return s, nil
@@ -564,20 +569,18 @@ func (s *ScalewayAPI) Sync() {
564569
func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) {
565570
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
566571
log.Debugf("GET %s", uri)
567-
client := &http.Client{}
568572
req, err := http.NewRequest("GET", uri, nil)
569573
if err != nil {
570574
return nil, err
571575
}
572576
req.Header.Set("X-Auth-Token", s.Token)
573577
req.Header.Set("Content-Type", "application/json")
574-
return client.Do(req)
578+
return s.client.Do(req)
575579
}
576580

577581
// PostResponse returns an http.Response object for the updated resource
578582
func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Response, error) {
579583
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
580-
client := &http.Client{}
581584
payload := new(bytes.Buffer)
582585
encoder := json.NewEncoder(payload)
583586
if err := encoder.Encode(data); err != nil {
@@ -596,13 +599,12 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res
596599
}
597600
req.Header.Set("X-Auth-Token", s.Token)
598601
req.Header.Set("Content-Type", "application/json")
599-
return client.Do(req)
602+
return s.client.Do(req)
600603
}
601604

602605
// PatchResponse returns an http.Response object for the updated resource
603606
func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Response, error) {
604607
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
605-
client := &http.Client{}
606608
payload := new(bytes.Buffer)
607609
encoder := json.NewEncoder(payload)
608610
if err := encoder.Encode(data); err != nil {
@@ -621,13 +623,12 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re
621623
}
622624
req.Header.Set("X-Auth-Token", s.Token)
623625
req.Header.Set("Content-Type", "application/json")
624-
return client.Do(req)
626+
return s.client.Do(req)
625627
}
626628

627629
// PutResponse returns an http.Response object for the updated resource
628630
func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Response, error) {
629631
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
630-
client := &http.Client{}
631632
payload := new(bytes.Buffer)
632633
encoder := json.NewEncoder(payload)
633634
if err := encoder.Encode(data); err != nil {
@@ -646,21 +647,20 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp
646647
}
647648
req.Header.Set("X-Auth-Token", s.Token)
648649
req.Header.Set("Content-Type", "application/json")
649-
return client.Do(req)
650+
return s.client.Do(req)
650651
}
651652

652653
// DeleteResponse returns an http.Response object for the deleted resource
653654
func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) {
654655
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
655-
client := &http.Client{}
656656
log.Debugf("DELETE %s", uri)
657657
req, err := http.NewRequest("DELETE", uri, nil)
658658
if err != nil {
659659
return nil, err
660660
}
661661
req.Header.Set("X-Auth-Token", s.Token)
662662
req.Header.Set("Content-Type", "application/json")
663-
return client.Do(req)
663+
return s.client.Do(req)
664664
}
665665

666666
// GetServers gets the list of servers from the ScalewayAPI

0 commit comments

Comments
 (0)