@@ -3,6 +3,7 @@ package remoteconfig
33import (
44 "encoding/json"
55 "fmt"
6+ "io"
67 "net/http"
78 "reflect"
89 "strings"
@@ -20,39 +21,30 @@ type Validater interface {
2021// Downloads a configuration JSON file from S3.
2122// Parses it to a particular struct type and runs a validation.
2223// URL should be of the format s3://bucket/path/file.json
23- func LoadConfigFromS3 (configURL string , configRegion AWSRegion , configEndpoint string , configStruct interface {}) error {
24- // Build a Signed URL to the config file in S3
25- signedURL , err := BuildSignedS3URL (configURL , configRegion , DEFAULT_S3_EXPIRY , configEndpoint )
26- if err != nil {
27- return err
28- }
29-
30- return DownloadJSONValidate (signedURL , configStruct )
31- }
32-
33- // Downloads JSON from a URL, decodes it and then validates.
34- func DownloadJSONValidate (signedURL string , configStruct interface {}) error {
35- // Download the config file from S3
36- resp , err := http .Get (signedURL )
24+ func LoadConfigFromURL (configURL string , configStruct interface {}) error {
25+ resp , err := http .Get (configURL )
3726 if err != nil {
3827 return err
3928 }
4029 defer resp .Body .Close ()
4130
42- // Check that we got a valid response code
4331 if resp .StatusCode != http .StatusOK {
44- return fmt .Errorf ("Download of JSON failed, URL = %s, Response Code = %d " , signedURL , resp .StatusCode )
32+ return fmt .Errorf ("Request to '%s' returned non-200 OK status '%d: %s' " , configURL , resp .StatusCode , http . StatusText ( resp . StatusCode ) )
4533 }
4634
35+ return ReadJSONValidate (resp .Body , configStruct )
36+ }
37+
38+ // Downloads JSON from a URL, decodes it and then validates.
39+ func ReadJSONValidate (cfgReader io.Reader , configStruct interface {}) error {
4740 // Do a streaming JSON decode
48- dec := json .NewDecoder (resp . Body )
49- if err = dec .Decode (configStruct ); err != nil {
41+ dec := json .NewDecoder (cfgReader )
42+ if err : = dec .Decode (configStruct ); err != nil {
5043 return fmt .Errorf ("Failed to decode JSON, with error, %s" , err .Error ())
5144 }
5245
5346 // Run validation on the config
54- err = validateConfigWithReflection (configStruct )
55- if err != nil {
47+ if err := validateConfigWithReflection (configStruct ); err != nil {
5648 return err
5749 }
5850
0 commit comments