diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-01-06 14:53:34 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-01-06 14:59:10 +0100 |
| commit | ff70e291c96de23ae4dab20f9c4e9f895eee53d5 (patch) | |
| tree | fc46d7eced911fb083fa3c58bcd6d173e3afa82d /internal/discovery | |
| parent | 965e36a91ca4eb6614ee71d0352ef42b465eb54f (diff) | |
Refactor: Re-use a HTTP client
Diffstat (limited to 'internal/discovery')
| -rw-r--r-- | internal/discovery/discovery.go | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index b2a90cd..41685ac 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -14,6 +14,9 @@ import ( // Discovery is the main structure used for this package. type Discovery struct { + // The httpClient for sending HTTP requests + httpClient *http.Client + // organizations represents the organizations that are returned by the discovery server organizations types.DiscoveryOrganizations @@ -23,12 +26,17 @@ type Discovery struct { var DiscoURL = "https://disco.eduvpn.org/v2/" -// discoFile is a helper function that gets a disco JSON and fills the structure with it +// file is a helper function that gets a disco JSON and fills the structure with it // If it was unsuccessful it returns an error. -func discoFile(jsonFile string, previousVersion uint64, structure interface{}) error { +func (discovery *Discovery) file(jsonFile string, previousVersion uint64, structure interface{}) error { + // No HTTP client present, create one + if discovery.httpClient == nil { + discovery.httpClient = http.NewClient() + } + // Get json data jsonURL := DiscoURL + jsonFile - _, body, err := http.Get(jsonURL) + _, body, err := discovery.httpClient.Get(jsonURL) if err != nil { return err } @@ -36,7 +44,7 @@ func discoFile(jsonFile string, previousVersion uint64, structure interface{}) e // Get signature sigFile := jsonFile + ".minisig" sigURL := DiscoURL + sigFile - _, sigBody, err := http.Get(sigURL) + _, sigBody, err := discovery.httpClient.Get(sigURL) if err != nil { return err } @@ -162,7 +170,7 @@ func (discovery *Discovery) Organizations() (*types.DiscoveryOrganizations, erro return &discovery.organizations, nil } file := "organization_list.json" - err := discoFile(file, discovery.organizations.Version, &discovery.organizations) + err := discovery.file(file, discovery.organizations.Version, &discovery.organizations) if err != nil { // Return previous with an error return &discovery.organizations, err @@ -178,7 +186,7 @@ func (discovery *Discovery) Servers() (*types.DiscoveryServers, error) { return &discovery.servers, nil } file := "server_list.json" - err := discoFile(file, discovery.servers.Version, &discovery.servers) + err := discovery.file(file, discovery.servers.Version, &discovery.servers) if err != nil { // Return previous with an error return &discovery.servers, err |
