Added map functions
This commit is contained in:
parent
c26a22c6d7
commit
ae7b6d0f83
@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func commandExit() error {
|
func commandExit(cfg *config) error {
|
||||||
fmt.Println("Closing the Pokedex... Goodbye!")
|
fmt.Println("Closing the Pokedex... Goodbye!")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func commandHelp() error {
|
func commandHelp(cfg *config) error {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Welcome to the Pokedex!")
|
fmt.Println("Welcome to the Pokedex!")
|
||||||
fmt.Println("Usage:")
|
fmt.Println("Usage:")
|
||||||
|
|||||||
46
command_map.go
Normal file
46
command_map.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func commandMap(cfg *config) error {
|
||||||
|
if cfg.nextLocationsURL == nil {
|
||||||
|
return errors.New("you're on the last page")
|
||||||
|
}
|
||||||
|
|
||||||
|
locationsResp, err := cfg.pokeClient.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")
|
||||||
|
}
|
||||||
|
|
||||||
|
locationsResp, err := cfg.pokeClient.ListLocations(cfg.prevLocationsURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.nextLocationsURL = locationsResp.Next
|
||||||
|
cfg.prevLocationsURL = locationsResp.Previous
|
||||||
|
|
||||||
|
for _, loc := range locationsResp.Results {
|
||||||
|
fmt.Println(" ", loc.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
18
internal/pokeapi/client.go
Normal file
18
internal/pokeapi/client.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
httpClient http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(timeout time.Duration) Client {
|
||||||
|
return Client{
|
||||||
|
httpClient: http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
32
internal/pokeapi/location_list.go
Normal file
32
internal/pokeapi/location_list.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 resp.Body.Close()
|
||||||
|
|
||||||
|
var locationsResp RespShallowLocations
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
if err = decoder.Decode(&locationsResp); err != nil {
|
||||||
|
return RespShallowLocations{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return locationsResp, nil
|
||||||
|
}
|
||||||
5
internal/pokeapi/pokeapi.go
Normal file
5
internal/pokeapi/pokeapi.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
const (
|
||||||
|
baseUrl = "https://pokeapi.co/api/v2"
|
||||||
|
)
|
||||||
11
internal/pokeapi/types_locations.go
Normal file
11
internal/pokeapi/types_locations.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package pokeapi
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
12
main.go
12
main.go
@ -1,7 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.eo13-dev.com/enordaz/gokedex/internal/pokeapi"
|
||||||
|
)
|
||||||
|
|
||||||
const prompt = "Pokedex > "
|
const prompt = "Pokedex > "
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
startRepl()
|
pokeClient := pokeapi.NewClient(5 * time.Second)
|
||||||
|
cfg := &config{
|
||||||
|
pokeClient: pokeClient,
|
||||||
|
}
|
||||||
|
startRepl(cfg)
|
||||||
}
|
}
|
||||||
|
|||||||
20
repl.go
20
repl.go
@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.eo13-dev.com/enordaz/gokedex/internal/pokeapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getCommands() map[string]cliCommand {
|
func getCommands() map[string]cliCommand {
|
||||||
@ -22,15 +24,17 @@ func getCommands() map[string]cliCommand {
|
|||||||
"map": {
|
"map": {
|
||||||
name: "map",
|
name: "map",
|
||||||
description: "Displays the names of the next 20 locations",
|
description: "Displays the names of the next 20 locations",
|
||||||
|
callback: commandMap,
|
||||||
},
|
},
|
||||||
"mapb": {
|
"mapb": {
|
||||||
name: "mapb",
|
name: "mapb",
|
||||||
description: "Displays the names of the previous 20 locations",
|
description: "Displays the names of the previous 20 locations",
|
||||||
|
callback: commandMapb,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startRepl() {
|
func startRepl(cfg *config) {
|
||||||
reader := bufio.NewScanner(os.Stdin)
|
reader := bufio.NewScanner(os.Stdin)
|
||||||
for {
|
for {
|
||||||
fmt.Print(prompt)
|
fmt.Print(prompt)
|
||||||
@ -45,13 +49,13 @@ func startRepl() {
|
|||||||
command, exists := getCommands()[inputCommand]
|
command, exists := getCommands()[inputCommand]
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
err := command.callback()
|
err := command.callback(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%v error: %v", command.name, err.Error())
|
fmt.Printf("%v error: %v\n", command.name, err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
fmt.Println("Unknown command")
|
fmt.Println("unknown command")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +65,13 @@ func startRepl() {
|
|||||||
type cliCommand struct {
|
type cliCommand struct {
|
||||||
name string
|
name string
|
||||||
description string
|
description string
|
||||||
callback func() error
|
callback func(*config) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
pokeClient pokeapi.Client
|
||||||
|
nextLocationsURL *string
|
||||||
|
prevLocationsURL *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanInput(text string) []string {
|
func cleanInput(text string) []string {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user