forked from cloudscale-ch/cloudscale-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_service.go
109 lines (85 loc) · 2.98 KB
/
generic_service.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
package cloudscale
import (
"context"
"fmt"
"net/http"
)
type GenericCreateService[TResource any, TCreateRequest any, TUpdateRequest any] interface {
Create(ctx context.Context, createRequest *TCreateRequest) (*TResource, error)
}
type GenericGetService[TResource any, TCreateRequest any, TUpdateRequest any] interface {
Get(ctx context.Context, resourceID string) (*TResource, error)
}
type GenericListService[TResource any, TCreateRequest any, TUpdateRequest any] interface {
List(ctx context.Context, modifiers ...ListRequestModifier) ([]TResource, error)
}
type GenericUpdateService[TResource any, TCreateRequest any, TUpdateRequest any] interface {
Update(ctx context.Context, resourceID string, updateRequest *TUpdateRequest) error
}
type GenericDeleteService[TResource any, TCreateRequest any, TUpdateRequest any] interface {
Delete(ctx context.Context, resourceID string) error
}
type GenericServiceOperations[TResource any, TCreateRequest any, TUpdateRequest any] struct {
client *Client
path string
}
func (g GenericServiceOperations[TResource, TCreateRequest, TUpdateRequest]) Create(ctx context.Context, createRequest *TCreateRequest) (*TResource, error) {
req, err := g.client.NewRequest(ctx, http.MethodPost, g.path, createRequest)
if err != nil {
return nil, err
}
resource := new(TResource)
err = g.client.Do(ctx, req, resource)
if err != nil {
return nil, err
}
return resource, nil
}
func (g GenericServiceOperations[TResource, TCreateRequest, TUpdateRequest]) Get(ctx context.Context, resourceID string) (*TResource, error) {
path := fmt.Sprintf("%s/%s", g.path, resourceID)
req, err := g.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
resource := new(TResource)
err = g.client.Do(ctx, req, resource)
if err != nil {
return nil, err
}
return resource, nil
}
func (g GenericServiceOperations[TResource, TCreateRequest, TUpdateRequest]) List(ctx context.Context, modifiers ...ListRequestModifier) ([]TResource, error) {
req, err := g.client.NewRequest(ctx, http.MethodGet, g.path, nil)
if err != nil {
return nil, err
}
for _, modifier := range modifiers {
modifier(req)
}
resources := []TResource{}
err = g.client.Do(ctx, req, &resources)
if err != nil {
return nil, err
}
return resources, nil
}
func (g GenericServiceOperations[TResource, TCreateRequest, TUpdateRequest]) Update(ctx context.Context, resourceID string, updateRequest *TUpdateRequest) error {
path := fmt.Sprintf("%s/%s", g.path, resourceID)
req, err := g.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
if err != nil {
return err
}
err = g.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
func (g GenericServiceOperations[TResource, TCreateRequest, TUpdateRequest]) Delete(ctx context.Context, resourceID string) error {
path := fmt.Sprintf("%s/%s", g.path, resourceID)
req, err := g.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return err
}
return g.client.Do(ctx, req, nil)
}