feat: allow to fetch collections

This commit is contained in:
Alexander Navarro 2024-11-21 20:27:57 -03:00
parent 01086d12c9
commit 8c660053e5
7 changed files with 147 additions and 83 deletions

View file

@ -2,24 +2,80 @@ package main
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
_ "modernc.org/sqlite"
synchronizator "git.alecodes.page/alecodes/synchronizator/pkg"
)
type PokeApi struct{}
func (pokeApi *PokeApi) ToNode() (string, []byte, error) {
return "POKEAPI", nil, nil
type PokeApiResponse[T any] struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []T `json:"results"`
}
func (pokeApi *PokeApi) FromNode(_class string, name string, metadata []byte) error {
if _class != "POKEAPI" {
return fmt.Errorf("invalid class %s", _class)
type Pokedex struct {
Name string `json:"name"`
Url string `json:"url"`
}
type Pokemon struct {
Id int `json:"id"`
Name string `json:"name"`
}
func getPokemons(
sync *synchronizator.DB,
pagination synchronizator.Pagination,
) ([]*synchronizator.Collection, synchronizator.Pagination, error) {
var collections []*synchronizator.Collection
params := url.Values{}
params.Add("offset", strconv.Itoa(pagination.Offset))
params.Add("limit", strconv.Itoa(pagination.Limit))
resp, err := http.Get("https://pokeapi.co/api/v2/pokedex?" + params.Encode())
if err != nil {
return nil, pagination, err
}
return nil
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
var data PokeApiResponse[Pokedex]
err = json.Unmarshal(body, &data)
if err != nil {
return nil, pagination, err
}
collections = make([]*synchronizator.Collection, 0, len(data.Results))
// TODO: this writes to the database in each collection creation
// add better strategies, like returning a collection to the platform and the
// platform do a final bulk update
for _, pokedex := range data.Results {
collection_name := "Pokedex_" + pokedex.Name
collection, err := sync.NewCollection(collection_name, nil)
if err != nil {
return nil, pagination, err
}
collections = append(collections, collection)
}
// fmt.Println(data)
pagination.Offset += pagination.Limit
pagination.HasMore = data.Next != ""
pagination.Total = data.Count
return collections, pagination, nil
}
func main() {
@ -43,6 +99,17 @@ func main() {
return
}
pokeApi := &PokeApi{}
sync.NewPlatform(pokeApi)
pokeApi, err := sync.NewPlatform("pokeapi", nil)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(pokeApi)
err = pokeApi.FetchCollections(getPokemons, synchronizator.StartPagination)
if err != nil {
fmt.Println(err)
return
}
}