generated from alecodes/base-template
feat: allow to fetch nodes
This commit is contained in:
parent
3cf643c83d
commit
8af94161ab
8 changed files with 141 additions and 30 deletions
|
|
@ -14,7 +14,7 @@ import (
|
|||
synchronizator "git.alecodes.page/alecodes/synchronizator/pkg"
|
||||
)
|
||||
|
||||
type PokeApiResponse[T any] struct {
|
||||
type PokeApiListResponse[T any] struct {
|
||||
Count int `json:"count"`
|
||||
Next string `json:"next"`
|
||||
Previous string `json:"previous"`
|
||||
|
|
@ -26,12 +26,21 @@ type Pokedex struct {
|
|||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type Pokemon struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
type PokeApiPokedexResponse struct {
|
||||
Pokemons []Pokemon `json:"pokemon_entries"`
|
||||
}
|
||||
|
||||
func getPokemons(
|
||||
type PokemonSpecies struct {
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type Pokemon struct {
|
||||
Id int `json:"entry_number"`
|
||||
PokemonSpecies PokemonSpecies `json:"pokemon_species"`
|
||||
}
|
||||
|
||||
func getPokedexs(
|
||||
pagination synchronizator.Pagination,
|
||||
) ([]*synchronizator.Collection, synchronizator.Pagination, error) {
|
||||
var collections []*synchronizator.Collection
|
||||
|
|
@ -48,7 +57,7 @@ func getPokemons(
|
|||
body, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
var data PokeApiResponse[Pokedex]
|
||||
var data PokeApiListResponse[Pokedex]
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return nil, pagination, err
|
||||
|
|
@ -58,7 +67,8 @@ func getPokemons(
|
|||
|
||||
for _, pokedex := range data.Results {
|
||||
collection_name := "Pokedex_" + pokedex.Name
|
||||
collection := synchronizator.NewCollection(collection_name, nil)
|
||||
metadata, err := json.Marshal(pokedex)
|
||||
collection := synchronizator.NewCollection(collection_name, metadata)
|
||||
if err != nil {
|
||||
return nil, pagination, err
|
||||
}
|
||||
|
|
@ -74,6 +84,45 @@ func getPokemons(
|
|||
return collections, pagination, nil
|
||||
}
|
||||
|
||||
func getPokemons(
|
||||
metadata []byte,
|
||||
pagination synchronizator.Pagination,
|
||||
) ([]*synchronizator.Node, synchronizator.Pagination, error) {
|
||||
var nodes []*synchronizator.Node
|
||||
|
||||
pokedex := &Pokedex{}
|
||||
json.Unmarshal(metadata, pokedex)
|
||||
|
||||
resp, err := http.Get(pokedex.Url)
|
||||
if err != nil {
|
||||
return nil, pagination, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
var data PokeApiPokedexResponse
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return nil, pagination, err
|
||||
}
|
||||
|
||||
nodes = make([]*synchronizator.Node, 0, len(data.Pokemons))
|
||||
|
||||
for _, pokemon := range data.Pokemons {
|
||||
metadata, err := json.Marshal(pokemon)
|
||||
node := synchronizator.NewNode(pokemon.PokemonSpecies.Name, metadata)
|
||||
if err != nil {
|
||||
return nil, pagination, err
|
||||
}
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
|
||||
// fmt.Println(data)
|
||||
|
||||
return nodes, pagination, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
connection, err := sql.Open("sqlite", "db.sql")
|
||||
if err != nil {
|
||||
|
|
@ -85,7 +134,7 @@ func main() {
|
|||
defer connection.Close()
|
||||
|
||||
opts := synchronizator.DefaultOptions
|
||||
opts.Log_level = synchronizator.DEBUG
|
||||
// opts.Log_level = synchronizator.DEBUG
|
||||
opts.DANGEROUSLY_DROP_TABLES = true
|
||||
|
||||
sync, err := synchronizator.New(connection, opts)
|
||||
|
|
@ -101,11 +150,21 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
err = pokeApi.FetchCollections(getPokemons, synchronizator.StartPagination)
|
||||
err = pokeApi.FetchCollections(getPokedexs, synchronizator.StartPagination)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(pokeApi)
|
||||
for _, pokedex := range pokeApi.Collections {
|
||||
if pokedex.IsDefault() {
|
||||
continue
|
||||
}
|
||||
|
||||
err = pokedex.FetchNodes(getPokemons, synchronizator.StartPagination)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue