Skip to content

Commit d1b5be3

Browse files
committed
refactor: Adjust URL construction logic in integration
1 parent 1eb0f9a commit d1b5be3

File tree

4 files changed

+128
-18
lines changed

4 files changed

+128
-18
lines changed

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,40 @@ This HTTP client is intended to be used with targetted SDK's and terraform provi
2222

2323
TBC
2424

25+
## Getting Started
26+
'
27+
{
28+
"Integration": {
29+
"FQDN": "https://api.example.com",
30+
"AuthMethodDescriptor": "Bearer"
31+
// other necessary fields for integration
32+
},
33+
"HideSensitiveData": true,
34+
"CustomCookies": [
35+
{
36+
"Name": "sessionid",
37+
"Value": "abc123",
38+
"Path": "/",
39+
"Domain": "example.com",
40+
"Expires": "2025-01-02T15:04:05Z",
41+
"MaxAge": 86400,
42+
"Secure": true,
43+
"HttpOnly": true,
44+
"SameSite": 1
45+
}
46+
],
47+
"MaxRetryAttempts": 5,
48+
"MaxConcurrentRequests": 10,
49+
"EnableDynamicRateLimiting": true,
50+
"CustomTimeout": "10s",
51+
"TokenRefreshBufferPeriod": "2m",
52+
"TotalRetryDuration": "10m",
53+
"FollowRedirects": true,
54+
"MaxRedirects": 3,
55+
"ConcurrencyManagementEnabled": true
56+
}
57+
'
58+
2559

2660
## Reporting Issues and Feedback
2761

@@ -53,13 +87,4 @@ If you would like to become an active contributor to this repository or project,
5387
[GitHubDocs]: <https://docs.github.com/>
5488
[AzureDevOpsDocs]: <https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops>
5589
[GitHubIssues]: <https://github.com/segraef/Template/issues>
56-
[Contributing]: CONTRIBUTING.md
57-
58-
<!-- External -->
59-
[Az]: <https://img.shields.io/powershellgallery/v/Az.svg?style=flat-square&label=Az>
60-
[AzGallery]: <https://www.powershellgallery.com/packages/Az/>
61-
[PowerShellCore]: <https://github.com/PowerShell/PowerShell/releases/latest>
62-
63-
<!-- Docs -->
64-
[MicrosoftAzureDocs]: <https://docs.microsoft.com/en-us/azure/>
65-
[PowerShellDocs]: <https://docs.microsoft.com/en-us/powershell/>
90+
[Contributing]: CONTRIBUTING.md

httpclient/config_validation.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
package httpclient
44

55
import (
6+
"encoding/json"
67
"errors"
8+
"fmt"
9+
"io"
10+
"os"
711
"time"
812
)
913

@@ -24,16 +28,51 @@ const (
2428
DefaultMaxRedirects = 5
2529
)
2630

27-
// TODO migrate all the loose strings
28-
29-
// TODO LoadConfigFromFile Func
31+
// LoadConfigFromFile loads http client configuration settings from a JSON file.
3032
func LoadConfigFromFile(filepath string) (*ClientConfig, error) {
31-
return nil, nil
33+
absPath, err := validateFilePath(filepath)
34+
if err != nil {
35+
return nil, fmt.Errorf("invalid file path: %v", err)
36+
}
37+
38+
file, err := os.Open(absPath)
39+
if err != nil {
40+
return nil, fmt.Errorf("could not open file: %v", err)
41+
}
42+
defer file.Close()
43+
44+
byteValue, err := io.ReadAll(file)
45+
if err != nil {
46+
return nil, fmt.Errorf("could not read file: %v", err)
47+
}
48+
49+
var config ClientConfig
50+
err = json.Unmarshal(byteValue, &config)
51+
if err != nil {
52+
return nil, fmt.Errorf("could not unmarshal JSON: %v", err)
53+
}
54+
55+
// Set default values for missing fields.
56+
SetDefaultValuesClientConfig(&config)
57+
58+
return &config, nil
3259
}
3360

34-
// TODO LoadConfigFromEnv Func
61+
// LoadConfigFromEnv loads configuration settings from environment variables.
3562
func LoadConfigFromEnv() (*ClientConfig, error) {
36-
return nil, nil
63+
config := &ClientConfig{
64+
HideSensitiveData: getEnvAsBool("HIDE_SENSITIVE_DATA", DefaultHideSensitiveData),
65+
MaxRetryAttempts: getEnvAsInt("MAX_RETRY_ATTEMPTS", DefaultMaxRetryAttempts),
66+
MaxConcurrentRequests: getEnvAsInt("MAX_CONCURRENT_REQUESTS", DefaultMaxConcurrentRequests),
67+
EnableDynamicRateLimiting: getEnvAsBool("ENABLE_DYNAMIC_RATE_LIMITING", DefaultEnableDynamicRateLimiting),
68+
CustomTimeout: getEnvAsDuration("CUSTOM_TIMEOUT", DefaultCustomTimeout),
69+
TokenRefreshBufferPeriod: getEnvAsDuration("TOKEN_REFRESH_BUFFER_PERIOD", DefaultTokenRefreshBufferPeriod),
70+
TotalRetryDuration: getEnvAsDuration("TOTAL_RETRY_DURATION", DefaultTotalRetryDuration),
71+
FollowRedirects: getEnvAsBool("FOLLOW_REDIRECTS", DefaultFollowRedirects),
72+
MaxRedirects: getEnvAsInt("MAX_REDIRECTS", DefaultMaxRedirects),
73+
}
74+
75+
return config, nil
3776
}
3877

3978
// TODO Review validateClientConfig

httpclient/integration.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// apiintegrations/apihandler/apihandler.go
1+
// httpclient/integration.go
22
package httpclient
33

44
import (
55
"net/http"
66
)
77

8-
// TODO comment
8+
// APIIntegration is an interface that defines the methods required for an API integration. These are obtained from go-api-http-client-integrations.
9+
// The methods defined in this interface are used by the HTTP client to authenticate and prepare requests for the API.
910
type APIIntegration interface {
1011
GetFQDN() string
1112
ConstructURL(endpoint string) string

httpclient/utility.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
// httpclient/utility.go
12
package httpclient
23

34
import (
45
"errors"
56
"fmt"
7+
"os"
68
"path/filepath"
79
"regexp"
10+
"strconv"
811
"strings"
12+
"time"
913
)
1014

1115
// TODO all func comments in here
@@ -74,3 +78,44 @@ func validatePassword(password string) error {
7478
}
7579
return nil
7680
}
81+
82+
// getEnvAsString reads an environment variable as a string, with a fallback default value.
83+
func getEnvAsString(name string, defaultVal string) string {
84+
if value, exists := os.LookupEnv(name); exists {
85+
return value
86+
}
87+
return defaultVal
88+
}
89+
90+
// getEnvAsBool reads an environment variable as a boolean, with a fallback default value.
91+
func getEnvAsBool(name string, defaultVal bool) bool {
92+
if value, exists := os.LookupEnv(name); exists {
93+
boolValue, err := strconv.ParseBool(value)
94+
if err == nil {
95+
return boolValue
96+
}
97+
}
98+
return defaultVal
99+
}
100+
101+
// getEnvAsInt reads an environment variable as an integer, with a fallback default value.
102+
func getEnvAsInt(name string, defaultVal int) int {
103+
if value, exists := os.LookupEnv(name); exists {
104+
intValue, err := strconv.Atoi(value)
105+
if err == nil {
106+
return intValue
107+
}
108+
}
109+
return defaultVal
110+
}
111+
112+
// getEnvAsDuration reads an environment variable as a duration, with a fallback default value.
113+
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
114+
if value, exists := os.LookupEnv(name); exists {
115+
durationValue, err := time.ParseDuration(value)
116+
if err == nil {
117+
return durationValue
118+
}
119+
}
120+
return defaultVal
121+
}

0 commit comments

Comments
 (0)