package cachedAPI import ( "fmt" "generic-rag/backend/datastore" "io" "net/http" "time" ) // This package behaves like an API but uses libSQL as a cache that gets checked before the actual API is called. type CachedAPI interface { Get(url string, cacheTTL time.Duration) (string, error) } type cachedAPI struct { mapper datastore.CacheStore } func NewCachedAPI(mapper datastore.CacheStore) CachedAPI { return &cachedAPI{ mapper: mapper, } } func (c cachedAPI) Get(url string, cacheTTL time.Duration) (string, error) { response, found, err := c.mapper.CachedAPI(url) if err != nil { return "", fmt.Errorf("error getting cached API response: %w", err) } if found { return response, nil } // Call the actual API resp, err := http.Get(url) if err != nil { return "", fmt.Errorf("error calling API: %w", err) } defer resp.Body.Close() // Read the response bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("error reading API response: %w", err) } // Save the response to the cache err = c.mapper.SaveAPIResponse(url, string(bodyBytes), cacheTTL) if err != nil { return "", fmt.Errorf("error saving API response: %w", err) } return string(bodyBytes), nil }