// Homework 46, problem 1: A quote client. // // Start the quotes server from homework 45 first, in another terminal: // go run exercises/45/homework/solutions/01-quotes-api.go // Write getQuote, which asks for GET /api/quotes/{id} and decodes the quote. // Any status other than 200 is an error that names the status, such as // "get quote 9: the server replied 404 Not Found". // // Run with: go run exercises/46/homework/01-quote-client.go -id 2 package main import ( "context" "errors" "flag" "fmt" "net/http" "os" "time" ) type quote struct { ID int `json:"id"` Text string `json:"text"` Author string `json:"author"` } func getQuote(ctx context.Context, client *http.Client, server string, id int) (quote, error) { // TODO: write your code here, in place of this placeholder line: return quote{}, errors.New("TODO: getQuote is not written yet") } func main() { server := flag.String("server", "http://localhost:8080", "address of the quotes server") id := flag.Int("id", 1, "which quote to get") flag.Parse() client := &http.Client{Timeout: 10 * time.Second} q, err := getQuote(context.Background(), client, *server, *id) if err != nil { fmt.Fprintln(os.Stderr, "Error:", err) os.Exit(1) } fmt.Printf("%q\n - %s\n", q.Text, q.Author) }