This commit is contained in:
71
main.go
71
main.go
@@ -2,20 +2,35 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/rdarius/boot-dev-pokedex/pokeapi"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
pokeapiClient pokeapi.Client
|
||||||
|
nextLocationsURL *string
|
||||||
|
prevLocationsURL *string
|
||||||
|
}
|
||||||
|
|
||||||
type cliCommand struct {
|
type cliCommand struct {
|
||||||
name string
|
name string
|
||||||
description string
|
description string
|
||||||
callback func() error
|
callback func(*config) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var commands map[string]cliCommand
|
var commands map[string]cliCommand
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
pokeClient := pokeapi.NewClient(5 * time.Second)
|
||||||
|
cfg := &config{
|
||||||
|
pokeapiClient: pokeClient,
|
||||||
|
}
|
||||||
|
|
||||||
commands = map[string]cliCommand{
|
commands = map[string]cliCommand{
|
||||||
"exit": {
|
"exit": {
|
||||||
name: "exit",
|
name: "exit",
|
||||||
@@ -27,6 +42,16 @@ func main() {
|
|||||||
description: "Display a help message",
|
description: "Display a help message",
|
||||||
callback: printHelpMessage,
|
callback: printHelpMessage,
|
||||||
},
|
},
|
||||||
|
"map": {
|
||||||
|
name: "map",
|
||||||
|
description: "Get the next page of locations",
|
||||||
|
callback: commandMapf,
|
||||||
|
},
|
||||||
|
"mapb": {
|
||||||
|
name: "mapb",
|
||||||
|
description: "Get the previous page of locations",
|
||||||
|
callback: commandMapb,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
// read user input
|
// read user input
|
||||||
@@ -40,7 +65,7 @@ func main() {
|
|||||||
//check if command is in commands map
|
//check if command is in commands map
|
||||||
cmd, ok := commands[command]
|
cmd, ok := commands[command]
|
||||||
if ok {
|
if ok {
|
||||||
err := cmd.callback()
|
err := cmd.callback(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@@ -52,7 +77,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func commandExit() error {
|
func commandExit(*config) error {
|
||||||
fmt.Println("Closing the Pokedex... Goodbye!")
|
fmt.Println("Closing the Pokedex... Goodbye!")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
return nil
|
return nil
|
||||||
@@ -70,10 +95,44 @@ func cleanInput(text string) []string {
|
|||||||
return separated
|
return separated
|
||||||
}
|
}
|
||||||
|
|
||||||
func printHelpMessage() error {
|
func printHelpMessage(*config) error {
|
||||||
fmt.Print("Welcome to the Pokedex!\nUsage:\n\n")
|
fmt.Print("Welcome to the Pokedex!\nUsage:\n\n")
|
||||||
for k, v := range commands {
|
for _, v := range commands {
|
||||||
fmt.Printf("%s: %s\n", k, v.description)
|
fmt.Printf("%s: %s\n", v.name, v.description)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func commandMapf(cfg *config) error {
|
||||||
|
locationsResp, err := cfg.pokeapiClient.ListLocations(cfg.nextLocationsURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.nextLocationsURL = locationsResp.Next
|
||||||
|
cfg.prevLocationsURL = locationsResp.Previous
|
||||||
|
|
||||||
|
for _, loc := range locationsResp.Results {
|
||||||
|
fmt.Println(loc.Name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func commandMapb(cfg *config) error {
|
||||||
|
if cfg.prevLocationsURL == nil {
|
||||||
|
return errors.New("you're on the first page")
|
||||||
|
}
|
||||||
|
|
||||||
|
locationResp, err := cfg.pokeapiClient.ListLocations(cfg.prevLocationsURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.nextLocationsURL = locationResp.Next
|
||||||
|
cfg.prevLocationsURL = locationResp.Previous
|
||||||
|
|
||||||
|
for _, loc := range locationResp.Results {
|
||||||
|
fmt.Println(loc.Name)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
20
pokeapi/client.go
Normal file
20
pokeapi/client.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client -
|
||||||
|
type Client struct {
|
||||||
|
httpClient http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient -
|
||||||
|
func NewClient(timeout time.Duration) Client {
|
||||||
|
return Client{
|
||||||
|
httpClient: http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
42
pokeapi/location_list.go
Normal file
42
pokeapi/location_list.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ListLocations -
|
||||||
|
func (c *Client) ListLocations(pageURL *string) (RespShallowLocations, error) {
|
||||||
|
url := baseURL + "/location-area"
|
||||||
|
if pageURL != nil {
|
||||||
|
url = *pageURL
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return RespShallowLocations{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := c.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return RespShallowLocations{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func(Body io.ReadCloser) {
|
||||||
|
_ = Body.Close()
|
||||||
|
}(resp.Body)
|
||||||
|
|
||||||
|
dat, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return RespShallowLocations{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
locationsResp := RespShallowLocations{}
|
||||||
|
err = json.Unmarshal(dat, &locationsResp)
|
||||||
|
if err != nil {
|
||||||
|
return RespShallowLocations{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return locationsResp, nil
|
||||||
|
}
|
||||||
5
pokeapi/pokeapi.go
Normal file
5
pokeapi/pokeapi.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
const (
|
||||||
|
baseURL = "https://pokeapi.co/api/v2"
|
||||||
|
)
|
||||||
12
pokeapi/types_locations.go
Normal file
12
pokeapi/types_locations.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
// RespShallowLocations -
|
||||||
|
type RespShallowLocations struct {
|
||||||
|
Count int `json:"count"`
|
||||||
|
Next *string `json:"next"`
|
||||||
|
Previous *string `json:"previous"`
|
||||||
|
Results []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
} `json:"results"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user