-
Notifications
You must be signed in to change notification settings - Fork 8
/
service_ctx.go
108 lines (94 loc) · 2.99 KB
/
service_ctx.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
package service
import (
"context"
"encoding/json"
"net/http"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/client"
)
type service interface {
CloudContext
AccountContext
FirewallContext
HostContext
IpAddressContext
LoadBalancerContext
ServerGroupContext
NetworkContext
TagContext
ServerContext
StorageContext
ObjectStorageContext
ManagedDatabaseServiceManagerContext
ManagedDatabaseUserManagerContext
ManagedDatabaseLogicalDatabaseManagerContext
PermissionContext
}
var _ service = (*ServiceContext)(nil)
// Service represents the API service with context support. The specified client is used to communicate with the API
type ServiceContext struct {
client *client.ClientContext
}
// Get performs a GET request to the specified location with context and stores the result in the value pointed to by v.
func (s *ServiceContext) get(ctx context.Context, location string, v interface{}) error {
body, err := s.client.PerformJSONRequest(ctx, http.MethodGet, s.client.CreateRequestURL(location), nil)
if err != nil {
return parseJSONServiceError(err)
}
return json.Unmarshal(body, v)
}
// Create performs a POST request to the specified location with context and stores the response in the value pointed to by v.
func (s *ServiceContext) create(ctx context.Context, r requestable, v interface{}) error {
payload, err := json.Marshal(r)
if err != nil {
return err
}
res, err := s.client.PerformJSONPostRequest(ctx, s.client.CreateRequestURL(r.RequestURL()), payload)
if err != nil {
return parseJSONServiceError(err)
}
if v == nil {
return nil
}
return json.Unmarshal(res, v)
}
// Modify performs a PATCH request to the specified location with context and stores the response in the value pointed to by v.
func (s *ServiceContext) modify(ctx context.Context, r requestable, v interface{}) error {
payload, err := json.Marshal(r)
if err != nil {
return err
}
res, err := s.client.PerformJSONPatchRequest(ctx, s.client.CreateRequestURL(r.RequestURL()), payload)
if err != nil {
return parseJSONServiceError(err)
}
if v == nil {
return nil
}
return json.Unmarshal(res, v)
}
// Modify performs a PUT request to the specified location with context and stores the response in the value pointed to by v.
func (s *ServiceContext) replace(ctx context.Context, r requestable, v interface{}) error {
payload, err := json.Marshal(r)
if err != nil {
return err
}
res, err := s.client.PerformJSONPutRequest(ctx, s.client.CreateRequestURL(r.RequestURL()), payload)
if err != nil {
return parseJSONServiceError(err)
}
if v == nil {
return nil
}
return json.Unmarshal(res, v)
}
// Delete performs a DELETE request to the specified location with context
func (s *ServiceContext) delete(ctx context.Context, r requestable) error {
err := s.client.PerformJSONDeleteRequest(ctx, s.client.CreateRequestURL(r.RequestURL()))
if err != nil {
return parseJSONServiceError(err)
}
return nil
}
func NewWithContext(client *client.ClientContext) *ServiceContext {
return &ServiceContext{client}
}