Skip to content

Commit da8d0f0

Browse files
committed
refactored client usage.
1 parent 647c8ac commit da8d0f0

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

ditto.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,19 @@ import (
1010
"path/filepath"
1111
)
1212

13-
func getCacheFilePath(endpoint string) string {
14-
hash := fnv.New64a()
15-
hash.Write([]byte(endpoint))
16-
hashedEndpoint := fmt.Sprintf("%x", hash.Sum(nil))
17-
return filepath.Join(".ditto", hashedEndpoint)
18-
}
19-
20-
func retrieve(endpoint string) ([]byte, error) {
21-
cacheFilePath := getCacheFilePath(endpoint)
22-
if _, err := os.Stat(cacheFilePath); os.IsNotExist(err) {
23-
return nil, err
13+
func Client() *http.Client {
14+
return &http.Client{
15+
Transport: &CachingTransport{
16+
Transport: http.DefaultTransport,
17+
},
2418
}
25-
return os.ReadFile(cacheFilePath)
26-
}
27-
28-
func cache(endpoint string, data []byte) error {
29-
cacheFilePath := getCacheFilePath(endpoint)
30-
os.MkdirAll(filepath.Dir(cacheFilePath), os.ModePerm)
31-
return os.WriteFile(cacheFilePath, data, 0644)
3219
}
3320

34-
type CachingHTTPClient struct {
21+
type CachingTransport struct {
3522
Transport http.RoundTripper
3623
}
3724

38-
func (c *CachingHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) {
25+
func (c *CachingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
3926
endpoint := req.URL.String()
4027

4128
data, err := retrieve(endpoint)
@@ -65,3 +52,24 @@ func (c *CachingHTTPClient) RoundTrip(req *http.Request) (*http.Response, error)
6552
resp.Body = io.NopCloser(bytes.NewReader(data))
6653
return resp, nil
6754
}
55+
56+
func getCacheFilePath(endpoint string) string {
57+
hash := fnv.New64a()
58+
hash.Write([]byte(endpoint))
59+
hashedEndpoint := fmt.Sprintf("%x", hash.Sum(nil))
60+
return filepath.Join(".ditto", hashedEndpoint)
61+
}
62+
63+
func retrieve(endpoint string) ([]byte, error) {
64+
cacheFilePath := getCacheFilePath(endpoint)
65+
if _, err := os.Stat(cacheFilePath); os.IsNotExist(err) {
66+
return nil, err
67+
}
68+
return os.ReadFile(cacheFilePath)
69+
}
70+
71+
func cache(endpoint string, data []byte) error {
72+
cacheFilePath := getCacheFilePath(endpoint)
73+
os.MkdirAll(filepath.Dir(cacheFilePath), os.ModePerm)
74+
return os.WriteFile(cacheFilePath, data, 0644)
75+
}

ditto_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestRetrieve(t *testing.T) {
6666
t.Errorf("Expected data: %s, but got: %s", expectedData, result)
6767
}
6868
}
69-
func TestCachingHTTPClient_RoundTrip_CachedResponse(t *testing.T) {
69+
func TestCachingTransport_RoundTrip_CachedResponse(t *testing.T) {
7070
// Define the test URL and expected response
7171
url := "https://example.com/api"
7272

@@ -75,7 +75,7 @@ func TestCachingHTTPClient_RoundTrip_CachedResponse(t *testing.T) {
7575
_ = os.Remove(cacheFilePath)
7676

7777
// Create a new caching HTTP client
78-
client := &CachingHTTPClient{
78+
client := &CachingTransport{
7979
Transport: http.DefaultTransport,
8080
}
8181

example_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ package ditto_test
33
import (
44
"context"
55
"fmt"
6-
"net/http"
76

87
"github.com/TimothyStiles/ditto"
98
"github.com/google/go-github/v57/github"
109
)
1110

1211
func Example_basic() {
13-
client := github.NewClient(&http.Client{
14-
Transport: &ditto.CachingHTTPClient{
15-
Transport: http.DefaultTransport,
16-
},
17-
})
12+
client := github.NewClient(ditto.Client()) // instead of http.DefaultClient we use ditto.Client()
1813

1914
// Use client...
2015
repos, _, err := client.Repositories.List(context.Background(), "octocat", nil)

0 commit comments

Comments
 (0)