diff options
| author | Bobby <[email protected]> | 2026-02-06 18:07:07 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-02-06 18:07:07 +0530 |
| commit | 31b5543be4faa4f01946d532d4b5e34828b285b5 (patch) | |
| tree | 393bb47243d6a68849e5de04ee4ef6931f7cd990 /utils | |
| parent | 2f4220364665b5b52dbee8c549e68ea9da46df21 (diff) | |
| download | metachan-31b5543be4faa4f01946d532d4b5e34828b285b5.tar.xz metachan-31b5543be4faa4f01946d532d4b5e34828b285b5.zip | |
Refactor Jikan API client: improve context management in GetAnimeProducers and enhance logging for pagination
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/api/jikan/jikan.go | 7 | ||||
| -rw-r--r-- | utils/logger/logger.go | 12 |
2 files changed, 14 insertions, 5 deletions
diff --git a/utils/api/jikan/jikan.go b/utils/api/jikan/jikan.go index 920f6dc..87c7cfd 100644 --- a/utils/api/jikan/jikan.go +++ b/utils/api/jikan/jikan.go @@ -251,7 +251,6 @@ func GetAnimeByGenre(genreID int, page int, limit int) (*types.JikanAnimeSearchR func GetAnimeProducers() (*types.JikanProducersResponse, error) { url := fmt.Sprintf("%s/producers", jikanAPIBaseURL) - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) page := 1 hasNextPage := true @@ -260,12 +259,12 @@ func GetAnimeProducers() (*types.JikanProducersResponse, error) { Pagination: types.JikanGenericPaginationEntity{}, } - defer cancel() - for hasNextPage { pageURL := fmt.Sprintf("%s?page=%d", url, page) + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) bytes, err := clientInstance.makeRequest(ctx, pageURL) + cancel() if err != nil { logger.Errorf("JikanClient", "GetAnimeProducers failed on page %d: %v", page, err) return nil, errors.New("failed to fetch anime producers from Jikan API") @@ -281,6 +280,8 @@ func GetAnimeProducers() (*types.JikanProducersResponse, error) { response.Pagination = pageResponse.Pagination } + logger.Debugf("JikanClient", "Fetched page (%d/%d) - %d producers", page, response.Pagination.LastVisiblePage, len(pageResponse.Data)) + response.Data = append(response.Data, pageResponse.Data...) hasNextPage = pageResponse.Pagination.HasNextPage page++ diff --git a/utils/logger/logger.go b/utils/logger/logger.go index f1c94e7..9fc1bc7 100644 --- a/utils/logger/logger.go +++ b/utils/logger/logger.go @@ -15,6 +15,7 @@ const prefixWidth = 15 var ( loggerInstance *zap.Logger level zap.AtomicLevel + showTimestamp bool ) func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { @@ -66,14 +67,17 @@ func Init() { level = zap.NewAtomicLevelAt(zapcore.InfoLevel) encoderCfg := zapcore.EncoderConfig{ - TimeKey: "ts", LevelKey: "level", MessageKey: "msg", LineEnding: "\n", - EncodeTime: timeEncoder, EncodeLevel: levelEncoder, } + if showTimestamp { + encoderCfg.TimeKey = "ts" + encoderCfg.EncodeTime = timeEncoder + } + encoder := zapcore.NewConsoleEncoder(encoderCfg) stdout := zapcore.AddSync(os.Stdout) @@ -91,6 +95,10 @@ func Init() { loggerInstance = zap.New(core, zap.AddCaller()) } +func SetTimestamp(enabled bool) { + showTimestamp = enabled +} + func SetDebug(enabled bool) { if enabled { level.SetLevel(zapcore.DebugLevel) |
