Skip to content

Commit

Permalink
renamed private functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyStiles committed Jan 9, 2024
1 parent ba313d4 commit 647c8ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions ditto.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func getCacheFilePath(endpoint string) string {
return filepath.Join(".ditto", hashedEndpoint)
}

func loadCache(endpoint string) ([]byte, error) {
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 saveCache(endpoint string, data []byte) error {
func cache(endpoint string, data []byte) error {
cacheFilePath := getCacheFilePath(endpoint)
os.MkdirAll(filepath.Dir(cacheFilePath), os.ModePerm)
return os.WriteFile(cacheFilePath, data, 0644)
Expand All @@ -38,7 +38,7 @@ type CachingHTTPClient struct {
func (c *CachingHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) {
endpoint := req.URL.String()

data, err := loadCache(endpoint)
data, err := retrieve(endpoint)
if err == nil {
reader := io.NopCloser(bytes.NewReader(data))
return &http.Response{
Expand All @@ -57,7 +57,7 @@ func (c *CachingHTTPClient) RoundTrip(req *http.Request) (*http.Response, error)
return nil, err
}

err = saveCache(endpoint, data)
err = cache(endpoint, data)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions ditto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestGetCacheFilePath(t *testing.T) {
t.Errorf("Expected` %s, but got %s", expected, result)
}
}
func TestSaveCache(t *testing.T) {
func TestCache(t *testing.T) {
endpoint := "https://example.com/api"
data := []byte("test data")
expectedFilePath := getCacheFilePath(endpoint)

err := saveCache(endpoint, data)
err := cache(endpoint, data)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand All @@ -43,7 +43,7 @@ func TestSaveCache(t *testing.T) {
t.Errorf("Failed to remove cache file: %v", err)
}
}
func TestLoadCache(t *testing.T) {
func TestRetrieve(t *testing.T) {
endpoint := "https://example.com/api"
expectedData := []byte("test data")
expectedFilePath := getCacheFilePath(endpoint)
Expand All @@ -56,7 +56,7 @@ func TestLoadCache(t *testing.T) {
defer os.Remove(expectedFilePath)

// Test loading cache
result, err := loadCache(endpoint)
result, err := retrieve(endpoint)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand Down

0 comments on commit 647c8ac

Please sign in to comment.