-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpets.go
177 lines (145 loc) · 3.58 KB
/
pets.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package petstore
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ Pets = (*pets)(nil)
var ctx = context.Background()
// Pets describes all the pet related methods that the Petstore
// API supports.
type Pets interface {
// List all the pets.
List(options PetListOptions) (*PetList, error)
// Create a new pet with the given options.
Create(options PetCreateOptions) (*Pet, error)
// Read an pet by its ID.
Read(petID string) (*Pet, error)
// Update an pet by its ID.
Update(petID string, options PetUpdateOptions) (*Pet, error)
// Delete an pet by its ID.
Delete(petID string) error
}
// pets implements Pets.
type pets struct {
client *Client
}
// PetList represents a list of pets.
type PetList struct {
Items []*Pet `json:"items"`
}
// Pet represents a Petstore pet.
type Pet struct {
ID string `json:"id"`
Name string `json:"name"`
Species string `json:"species"`
Age int `json:"age"`
}
// PetListOptions represents the options for listing pets.
type PetListOptions struct {
Limit int
}
// List all pets.
func (s *pets) List(options PetListOptions) (*PetList, error) {
if options.Limit == 0 {
options.Limit = 100
}
req, err := s.client.newRequest("GET", "pets", &options)
if err != nil {
return nil, err
}
petl := &PetList{}
err = s.client.do(ctx, req, petl)
if err != nil {
return nil, err
}
return petl, nil
}
// PetCreateOptions represents the options for creating an pet.
type PetCreateOptions struct {
Name string `json:"name"`
Species string `json:"species"`
Age int `json:"age"`
}
func (p PetCreateOptions) valid() error {
if !validString(&p.Name) {
return errors.New("pet name is required")
}
if !validString(&p.Species) {
return errors.New("pet species is required")
}
if !validInteger(&p.Age) {
return errors.New("age is required")
}
return nil
}
// Create a new pet with the given options.
func (s *pets) Create(options PetCreateOptions) (*Pet, error) {
if err := options.valid(); err != nil {
return nil, err
}
req, err := s.client.newRequest("POST", "pets", &options)
if err != nil {
return nil, err
}
ent := &Pet{}
err = s.client.do(ctx, req, ent)
if err != nil {
return nil, err
}
return ent, nil
}
// Read an pet by id.
func (s *pets) Read(petID string) (*Pet, error) {
if !validStringID(&petID) {
return nil, errors.New("invalid value for petID")
}
u := fmt.Sprintf("pets/%s", url.QueryEscape(petID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
ent := &Pet{}
err = s.client.do(ctx, req, ent)
if err != nil {
return nil, err
}
return ent, nil
}
// PetUpdateOptions represents the options for updating an pet.
type PetUpdateOptions struct {
Name string `json:"name"`
Species string `json:"species"`
Age int `json:"age"`
}
// Update attributes of an existing pet.
func (s *pets) Update(petID string, options PetUpdateOptions) (*Pet, error) {
if !validStringID(&petID) {
return nil, errors.New("invalid value for petID")
}
u := fmt.Sprintf("pets/%s", url.QueryEscape(petID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
ent := &Pet{}
err = s.client.do(ctx, req, ent)
if err != nil {
return nil, err
}
return ent, nil
}
// Delete an pet by its ID.
func (s *pets) Delete(petID string) error {
if !validStringID(&petID) {
return errors.New("invalid value for petID")
}
u := fmt.Sprintf("pets/%s", url.QueryEscape(petID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}