Skip to content

Commit

Permalink
Merge pull request #415 from FUSAKLA/fus-http-config-from-file
Browse files Browse the repository at this point in the history
feat: make LoadHTTPConfigFile set directory and move from tests file
  • Loading branch information
roidelapluie authored Dec 8, 2022
2 parents 87b669d + d9cd6f2 commit 11bcb5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
25 changes: 25 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -248,6 +249,30 @@ func (a *OAuth2) SetDirectory(dir string) {
a.TLSConfig.SetDirectory(dir)
}

// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig.
func LoadHTTPConfig(s string) (*HTTPClientConfig, error) {
cfg := &HTTPClientConfig{}
err := yaml.UnmarshalStrict([]byte(s), cfg)
if err != nil {
return nil, err
}
return cfg, nil
}

// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig.
func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, nil, err
}
cfg, err := LoadHTTPConfig(string(content))
if err != nil {
return nil, nil, err
}
cfg.SetDirectory(filepath.Dir(filepath.Dir(filename)))
return cfg, content, nil
}

// HTTPClientConfig configures an HTTP client.
type HTTPClientConfig struct {
// The HTTP basic authentication credentials for the targets.
Expand Down
23 changes: 0 additions & 23 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,29 +1125,6 @@ func TestInvalidHTTPConfigs(t *testing.T) {
}
}

// LoadHTTPConfig parses the YAML input s into a HTTPClientConfig.
func LoadHTTPConfig(s string) (*HTTPClientConfig, error) {
cfg := &HTTPClientConfig{}
err := yaml.UnmarshalStrict([]byte(s), cfg)
if err != nil {
return nil, err
}
return cfg, nil
}

// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig.
func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, nil, err
}
cfg, err := LoadHTTPConfig(string(content))
if err != nil {
return nil, nil, err
}
return cfg, content, nil
}

type roundTrip struct {
theResponse *http.Response
theError error
Expand Down

0 comments on commit 11bcb5b

Please sign in to comment.