Skip to content

Commit

Permalink
refactored client usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyStiles committed Jan 9, 2024
1 parent 647c8ac commit da8d0f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
48 changes: 28 additions & 20 deletions ditto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,19 @@ import (
"path/filepath"
)

func getCacheFilePath(endpoint string) string {
hash := fnv.New64a()
hash.Write([]byte(endpoint))
hashedEndpoint := fmt.Sprintf("%x", hash.Sum(nil))
return filepath.Join(".ditto", hashedEndpoint)
}

func retrieve(endpoint string) ([]byte, error) {
cacheFilePath := getCacheFilePath(endpoint)
if _, err := os.Stat(cacheFilePath); os.IsNotExist(err) {
return nil, err
func Client() *http.Client {
return &http.Client{
Transport: &CachingTransport{
Transport: http.DefaultTransport,
},
}
return os.ReadFile(cacheFilePath)
}

func cache(endpoint string, data []byte) error {
cacheFilePath := getCacheFilePath(endpoint)
os.MkdirAll(filepath.Dir(cacheFilePath), os.ModePerm)
return os.WriteFile(cacheFilePath, data, 0644)
}

type CachingHTTPClient struct {
type CachingTransport struct {
Transport http.RoundTripper
}

func (c *CachingHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) {
func (c *CachingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
endpoint := req.URL.String()

data, err := retrieve(endpoint)
Expand Down Expand Up @@ -65,3 +52,24 @@ func (c *CachingHTTPClient) RoundTrip(req *http.Request) (*http.Response, error)
resp.Body = io.NopCloser(bytes.NewReader(data))
return resp, nil
}

func getCacheFilePath(endpoint string) string {
hash := fnv.New64a()
hash.Write([]byte(endpoint))
hashedEndpoint := fmt.Sprintf("%x", hash.Sum(nil))
return filepath.Join(".ditto", hashedEndpoint)
}

func retrieve(endpoint string) ([]byte, error) {
cacheFilePath := getCacheFilePath(endpoint)
if _, err := os.Stat(cacheFilePath); os.IsNotExist(err) {
return nil, err
}
return os.ReadFile(cacheFilePath)
}

func cache(endpoint string, data []byte) error {
cacheFilePath := getCacheFilePath(endpoint)
os.MkdirAll(filepath.Dir(cacheFilePath), os.ModePerm)
return os.WriteFile(cacheFilePath, data, 0644)
}
4 changes: 2 additions & 2 deletions ditto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestRetrieve(t *testing.T) {
t.Errorf("Expected data: %s, but got: %s", expectedData, result)
}
}
func TestCachingHTTPClient_RoundTrip_CachedResponse(t *testing.T) {
func TestCachingTransport_RoundTrip_CachedResponse(t *testing.T) {
// Define the test URL and expected response
url := "https://example.com/api"

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

// Create a new caching HTTP client
client := &CachingHTTPClient{
client := &CachingTransport{
Transport: http.DefaultTransport,
}

Expand Down
7 changes: 1 addition & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ package ditto_test
import (
"context"
"fmt"
"net/http"

"github.com/TimothyStiles/ditto"
"github.com/google/go-github/v57/github"
)

func Example_basic() {
client := github.NewClient(&http.Client{
Transport: &ditto.CachingHTTPClient{
Transport: http.DefaultTransport,
},
})
client := github.NewClient(ditto.Client()) // instead of http.DefaultClient we use ditto.Client()

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

0 comments on commit da8d0f0

Please sign in to comment.