This repository has been archived by the owner on Feb 25, 2025. It is now read-only.
forked from thomaspoignant/go-feature-flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretriever_github.go
65 lines (53 loc) · 1.52 KB
/
retriever_github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ffclient
import (
"context"
"fmt"
"net/http"
"time"
"github.com/thomaspoignant/go-feature-flag/internal"
)
// GithubRetriever is a configuration struct for a GitHub retriever.
type GithubRetriever struct {
RepositorySlug string
Branch string // default is main
FilePath string
GithubToken string
Timeout time.Duration // default is 10 seconds
// httpClient is the http.Client if you want to override it.
httpClient internal.HTTPClient
}
func (r *GithubRetriever) Retrieve(ctx context.Context) ([]byte, error) {
if r.FilePath == "" || r.RepositorySlug == "" {
return nil, fmt.Errorf("missing mandatory information filePath=%s, repositorySlug=%s", r.FilePath, r.RepositorySlug)
}
// default branch is main
branch := r.Branch
if branch == "" {
branch = "main"
}
// add header for Github Token if specified
header := http.Header{}
if r.GithubToken != "" {
header.Add("Authorization", fmt.Sprintf("token %s", r.GithubToken))
}
URL := fmt.Sprintf(
"https://raw.githubusercontent.com/%s/%s/%s",
r.RepositorySlug,
branch,
r.FilePath)
httpRetriever := HTTPRetriever{
URL: URL,
Method: http.MethodGet,
Header: header,
Timeout: r.Timeout,
}
if r.httpClient != nil {
httpRetriever.SetHTTPClient(r.httpClient)
}
return httpRetriever.Retrieve(ctx)
}
// SetHTTPClient is here if you want to override the default http.Client we are using.
// It is also used for the tests.
func (r *GithubRetriever) SetHTTPClient(client internal.HTTPClient) {
r.httpClient = client
}