-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from calvinmclean/example/client-only
Add client-only example and improve JSON CLI
- Loading branch information
Showing
4 changed files
with
147 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Pokemon Client | ||
|
||
This is an example of how babyapi can be used just to create a client for an external API, like the [PokeAPI](https://pokeapi.co/docs/v2). | ||
|
||
Although it works with the default commands, this example adds an extra command to better demonstrate the Pokemon struct since the default commands will print the raw JSON response. | ||
|
||
## How To | ||
|
||
Use the custom `get` command to pretty-print Pokemon details | ||
```shell | ||
> go run main.go get pikachu | ||
Name: pikachu | ||
Types: [electric] | ||
2 Abilities | ||
105 Moves | ||
``` | ||
|
||
Use the default `client get` command to print raw JSON data | ||
```shell | ||
go run main.go client --address "https://pokeapi.co" pokemon get pikachu | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/calvinmclean/babyapi" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Pokemon struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
Abilities []any `json:"abilities"` | ||
Moves []any `json:"moves"` | ||
Types []struct { | ||
Type struct { | ||
Name string `json:"name"` | ||
} `json:"type"` | ||
} `json:"types"` | ||
} | ||
|
||
func (p *Pokemon) String() string { | ||
var sb strings.Builder | ||
|
||
writef := func(format string, a ...any) { | ||
sb.WriteString(fmt.Sprintf(format, a...)) | ||
} | ||
|
||
var types []string | ||
for _, t := range p.Types { | ||
types = append(types, t.Type.Name) | ||
} | ||
|
||
writef("Name: %s\n", p.Name) | ||
writef("Types: %s\n", types) | ||
writef("%d Abilities\n", len(p.Abilities)) | ||
writef("%d Moves\n", len(p.Moves)) | ||
|
||
return sb.String() | ||
} | ||
|
||
func (p *Pokemon) GetID() string { | ||
return fmt.Sprint(p.ID) | ||
} | ||
|
||
func (*Pokemon) Bind(*http.Request) error { | ||
return nil | ||
} | ||
|
||
func (*Pokemon) Render(http.ResponseWriter, *http.Request) error { | ||
return nil | ||
} | ||
|
||
func main() { | ||
api := babyapi.NewAPI("pokemon", "/api/v2/pokemon/", func() *Pokemon { return &Pokemon{} }) | ||
|
||
cmd := api.Command() | ||
|
||
// Add a custom get command because the regular get command will just print raw JSON | ||
cmd.AddCommand(&cobra.Command{ | ||
Use: "get", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
address, _ := cmd.Flags().GetString("address") | ||
if address == "" { | ||
address = "https://pokeapi.co" | ||
} | ||
|
||
resp, err := api.Client(address).Get(context.Background(), args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.Data != nil { | ||
fmt.Println(resp.Data.String()) | ||
} | ||
|
||
return nil | ||
}, | ||
}) | ||
|
||
err := cmd.Execute() | ||
if err != nil { | ||
fmt.Printf("error: %v\n", err) | ||
} | ||
} |