feat: auto calculate pagination in fetcher

also refactor a little the worker pool
This commit is contained in:
Alexander Navarro 2024-11-30 17:30:38 -03:00
parent 837e703bc4
commit 96af51ee68
4 changed files with 451 additions and 374 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
@ -16,7 +17,7 @@ import (
)
type PokeApiListResponse[T any] struct {
Count int `json:"count"`
Count uint64 `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []T `json:"results"`
@ -42,17 +43,22 @@ type Pokemon struct {
}
func getPokedexs(
ctx context.Context,
pagination synchronizator.Pagination,
) ([]*synchronizator.Collection, synchronizator.Pagination, error) {
) (synchronizator.FetchCollectionResponse, error) {
payload := synchronizator.FetchCollectionResponse{
Pagination: pagination,
}
var collections []*synchronizator.Collection
params := url.Values{}
params.Add("offset", strconv.Itoa(pagination.Offset))
params.Add("limit", strconv.Itoa(pagination.Limit))
params.Add("offset", strconv.FormatUint(pagination.Offset, 10))
params.Add("limit", strconv.FormatUint(pagination.Limit, 10))
resp, err := http.Get("https://pokeapi.co/api/v2/pokedex?" + params.Encode())
if err != nil {
return nil, pagination, err
return payload, err
}
body, err := io.ReadAll(resp.Body)
@ -61,7 +67,7 @@ func getPokedexs(
var data PokeApiListResponse[Pokedex]
err = json.Unmarshal(body, &data)
if err != nil {
return nil, pagination, err
return payload, err
}
collections = make([]*synchronizator.Collection, 0, len(data.Results))
@ -71,18 +77,20 @@ func getPokedexs(
metadata, err := json.Marshal(pokedex)
collection := synchronizator.NewCollection(collection_name, metadata)
if err != nil {
return nil, pagination, err
return payload, err
}
collections = append(collections, collection)
}
// fmt.Println(data)
pagination.Offset += pagination.Limit
pagination.HasMore = data.Next != ""
pagination.Total = data.Count
payload.Offset += pagination.Limit
payload.HasMore = data.Next != ""
payload.Total = data.Count
return collections, pagination, nil
payload.Response = collections
return payload, nil
}
func getPokemons(
@ -158,7 +166,9 @@ func main() {
return
}
err = pokeApi.FetchCollections(getPokedexs, synchronizator.StartPagination)
pagination := synchronizator.StartPagination
err = pokeApi.FetchCollections(getPokedexs, pagination)
if err != nil {
fmt.Println(err)
return