haberdasher-twirp/cmd/client/main.go

39 lines
748 B
Go
Raw Normal View History

2021-07-26 14:00:25 +00:00
package main
import (
"context"
"fmt"
2021-07-31 18:34:43 +00:00
"math/rand"
2021-07-26 14:00:25 +00:00
"net/http"
"os"
"haberdasher-twirp/haberdasher"
)
2021-07-31 18:34:43 +00:00
const MaxSize = 12
2021-07-26 14:00:25 +00:00
func main() {
var host string = "http://localhost:8080"
if len(os.Args) > 1 {
host = os.Args[1]
}
client := haberdasher.NewHaberdasherProtobufClient(host, &http.Client{})
2021-07-31 18:34:43 +00:00
hat, err := client.MakeHat(context.Background(), &haberdasher.Size{Inches: int32(rand.Intn(MaxSize))})
2021-07-26 14:00:25 +00:00
if err != nil {
fmt.Printf("oh no: %v\n", err)
os.Exit(1)
}
fmt.Printf("I have a nice new hat: %+v\n", hat)
hats, err := client.ListHats(context.Background(), &haberdasher.HatQuery{Limit: 20})
if err != nil {
fmt.Printf("oh no: %v\n", err)
os.Exit(1)
}
for i, h := range hats.Hats {
fmt.Printf("%d: %+v\n", i, h)
}
}