-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookclient.go
55 lines (42 loc) · 1.14 KB
/
bookclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package book
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
type JsonError struct {
Code string `json:"code" pact:"example=1234"`
Msg string `json:"msg" pact:"example=No book with ISBN 123456789"`
}
type Book struct {
ISBN string `json:"isbn" pact:"example=987654321"`
Title string `json:"title" pact:"example=Testing All The Things"`
Image string `json:"image" pact:"example=testing.jpg"`
Genre string `json:"genre" pact:"example=Computers"`
YearPublished int `json:"year_published" pact:"example=2021"`
}
func NewClient(host string) Client {
return Client{host}
}
type Client struct {
host string
}
func (c Client) GetBook(isbn string) (Book, error) {
client := http.Client{}
req, _ := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/book/%s", c.host, isbn),
nil,
)
req.Header.Add("Accept", "application/json")
resp, _ := client.Do(req)
if resp.StatusCode == http.StatusNotFound {
je := JsonError{}
json.NewDecoder(resp.Body).Decode(&je)
return Book{}, errors.New(fmt.Sprintf("%s - %s", je.Code, je.Msg))
}
b := Book{}
json.NewDecoder(resp.Body).Decode(&b)
return b, nil
}