Skip to content

Commit d3d1765

Browse files
authored
FFM-7010 Use SDK HTTP Client for all requests (#132)
1 parent 0202715 commit d3d1765

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

analyticsservice/analytics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
variationValueAttribute string = "featureValue"
2626
targetAttribute string = "target"
2727
sdkVersionAttribute string = "SDK_VERSION"
28-
sdkVersion string = "1.12.0"
28+
sdkVersion string = "0.1.14"
2929
sdkTypeAttribute string = "SDK_TYPE"
3030
sdkType string = "server"
3131
sdkLanguageAttribute string = "SDK_LANGUAGE"

client/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ func (c *CfClient) streamConnect(ctx context.Context) {
244244
c.config.Logger.Info("Registering SSE consumer")
245245
sseClient := sse.NewClient(fmt.Sprintf("%s/stream?cluster=%s", c.config.url, c.clusterIdentifier))
246246

247+
// Use the SDKs http client
248+
sseClient.Connection = c.config.httpClient
249+
247250
streamErr := func() {
248251
c.config.Logger.Warnf("%s Stream disconnected. Swapping to polling mode", sdk_codes.StreamDisconnected)
249252
c.mux.RLock()
@@ -400,7 +403,7 @@ func (c *CfClient) authenticate(ctx context.Context) error {
400403
metricsClient, err := metricsclient.NewClientWithResponses(c.config.eventsURL,
401404
metricsclient.WithRequestEditorFn(bearerTokenProvider.Intercept),
402405
metricsclient.WithRequestEditorFn(c.InterceptAddCluster),
403-
metricsclient.WithHTTPClient(http.DefaultClient),
406+
metricsclient.WithHTTPClient(c.config.httpClient),
404407
)
405408
if err != nil {
406409
return err

examples/tls/example.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"os"
10+
"time"
11+
12+
harness "github.com/harness/ff-golang-server-sdk/client"
13+
"github.com/harness/ff-golang-server-sdk/evaluation"
14+
)
15+
16+
var (
17+
flagName string = getEnvOrDefault("FF_FLAG_NAME", "harnessappdemodarkmode")
18+
sdkKey string = getEnvOrDefault("FF_API_KEY", "change me")
19+
)
20+
21+
func main() {
22+
log.Println("Harness SDK TLS Example")
23+
24+
certPool, err := loadCertificates([]string{"path to PEM", "path to PEM"})
25+
if err != nil {
26+
log.Printf("Failed to parse PEM files: `%s`\n", err)
27+
}
28+
29+
// Create a custom TLS configuration and use the CA pool.
30+
tlsConfig := &tls.Config{
31+
RootCAs: certPool,
32+
}
33+
34+
transport := &http.Transport{
35+
TLSClientConfig: tlsConfig,
36+
}
37+
38+
httpClient := http.Client{Transport: transport}
39+
40+
// Create a feature flag client and wait for it to successfully initialize
41+
startTime := time.Now()
42+
43+
// Note that this code uses ffserver hostname as an example, likely you'll have your own hostname or IP.
44+
// You should ensure the endpoint is returning a cert with valid SANs configured for the host/IP.
45+
client, err := harness.NewCfClient(sdkKey, harness.WithEventsURL("https://ffserver:8003/api/1.0"), harness.WithURL("https://ffserver:8003/api/1.0"), harness.WithWaitForInitialized(true), harness.WithHTTPClient(&httpClient))
46+
47+
elapsedTime := time.Since(startTime)
48+
log.Printf("Took '%v' seconds to get a client initialization result ", elapsedTime.Seconds())
49+
50+
if err != nil {
51+
log.Printf("Client failed to initialize: `%s`\n", err)
52+
}
53+
54+
defer func() {
55+
err := client.Close()
56+
if err != nil {
57+
return
58+
}
59+
}()
60+
61+
// Create a target (different targets can get different results based on rules)
62+
target := evaluation.Target{
63+
Identifier: "HT_1",
64+
Name: "Harness_Target_1",
65+
Attributes: &map[string]interface{}{"email": "[email protected]"},
66+
}
67+
68+
// Loop forever reporting the state of the flag
69+
for {
70+
resultBool, err := client.BoolVariation(flagName, &target, false)
71+
if err != nil {
72+
log.Printf("failed to get evaluation: %v ", err)
73+
}
74+
log.Printf("Flag variation %v\n", resultBool)
75+
76+
time.Sleep(10 * time.Second)
77+
}
78+
79+
}
80+
81+
func getEnvOrDefault(key, defaultStr string) string {
82+
value := os.Getenv(key)
83+
if value == "" {
84+
return defaultStr
85+
}
86+
return value
87+
}
88+
89+
// Load certificates from PEM files
90+
func loadCertificates(filePaths []string) (*x509.CertPool, error) {
91+
pool := x509.NewCertPool()
92+
for _, ca := range filePaths {
93+
caBytes, err := os.ReadFile(ca)
94+
if err != nil {
95+
return nil, fmt.Errorf("failed to read CA certificate from file: %w", err)
96+
}
97+
98+
if !pool.AppendCertsFromPEM(caBytes) {
99+
return nil, fmt.Errorf("could not append CA certificate")
100+
}
101+
}
102+
return pool, nil
103+
}

0 commit comments

Comments
 (0)