Skip to content

Commit

Permalink
Add command to list location areas
Browse files Browse the repository at this point in the history
  • Loading branch information
jurichar committed Mar 15, 2024
1 parent 2efecdb commit 766499b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 4 deletions.
1 change: 0 additions & 1 deletion command_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ func callbackHelp() {
fmt.Println(cmd.name, " - ", cmd.description)
}
fmt.Println()

}
21 changes: 21 additions & 0 deletions command_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"log"

"github.com/jurichar/pokedexcli/internal/pokeapi"
)

func callbackMap() {
pokeapiClient := pokeapi.NewClient()

resp, err := pokeapiClient.ListLocationArea()
if err != nil {
log.Fatal(err)
}
fmt.Println("Location areas:")
for _, area := range resp.Results {
fmt.Printf(" - %s\n", area.Name)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jurichar/pokedexcli

go 1.22.0
go 1.20
41 changes: 41 additions & 0 deletions internal/pokeapi/location_area_req.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pokeapi

import (
"encoding/json"
"fmt"
"io"
"net/http"
)

func (c *Client) ListLocationArea() (LocationAreasResp, error) {
endpoint := "/location-area"
fullUrl := baseURL + endpoint

req, err := http.NewRequest("GET", fullUrl, nil)
if err != nil {
return LocationAreasResp{}, err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return LocationAreasResp{}, err
}
defer resp.Body.Close()

if resp.StatusCode > 399 {
return LocationAreasResp{}, fmt.Errorf("bad status code: %v", resp.StatusCode)
}

dat, err := io.ReadAll(resp.Body)
if err != nil {
return LocationAreasResp{}, err
}

locationAreasResp := LocationAreasResp{}
err = json.Unmarshal(dat, &locationAreasResp)
if err != nil {
return LocationAreasResp{}, err
}

return locationAreasResp, nil
}
20 changes: 20 additions & 0 deletions internal/pokeapi/pokeapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pokeapi

import (
"net/http"
"time"
)

const baseURL = "https://pokeapi.co/api/v2/"

type Client struct {
httpClient http.Client
}

func NewClient() Client {
return Client{
httpClient: http.Client{
Timeout: time.Minute,
},
}
}
11 changes: 11 additions & 0 deletions internal/pokeapi/types_location_area.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pokeapi

type LocationAreasResp 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"`
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package main

func main () {
func main() {
startRepl()
}
}
5 changes: 5 additions & 0 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func getCommands() map[string]cliCommand {
description: "Exit the Pokedex",
callback: callbackExit,
},
"map": {
name: "map",
description: "List some location areas",
callback: callbackMap,
},
}
}

Expand Down

0 comments on commit 766499b

Please sign in to comment.