forked from frain-dev/convoy-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
135 lines (111 loc) · 3.56 KB
/
project.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
package convoy_go
import (
"context"
"errors"
"fmt"
"time"
)
var (
ErrNotListProjectResponse = errors.New("invalid list project response")
ErrNotProjectResponse = errors.New("invalid project response")
)
type Project struct {
client *Client
}
type CreateProjectRequest struct {
Name string `json:"name"`
Type string `json:"type"`
LogoUrl string `json:"logo_url,omitempty"`
RateLimit int `json:"rate_limit,omitempty"`
RateLimitDuration string `json:"rate_limit_duration,omitempty"`
Project *ProjectConfig `json:"config"`
}
type ProjectConfig struct {
RateLimit *RateLimitConfiguration `json:"ratelimit"`
Strategy *StrategyConfiguration `json:"strategy"`
Signature *SignatureConfiguration `json:"signature"`
RetentionPolicy *RetentionPolicyConfiguration `json:"retention_policy"`
DisableEndpoint bool `json:"disable_endpoint"`
ReplayAttacks bool `json:"replay_attacks"`
IsRetentionPolicyEnabled bool `json:"is_retention_policy_enabled"`
}
type StrategyConfiguration struct {
Type string `json:"type"`
Duration uint64 `json:"duration"`
RetryCount uint64 `json:"retry_count"`
}
type RateLimitConfiguration struct {
Count int `json:"count"`
Duration uint64 `json:"duration"`
}
type RetentionPolicyConfiguration struct {
Policy string `json:"policy"`
}
type SignatureConfiguration struct {
Header string `json:"header"`
Hash string `json:"hash"`
}
type ProjectResponse struct {
UID string `json:"uid"`
Name string `json:"name"`
LogoUrl string `json:"logo_url"`
OrganisationID string `json:"organisation_id"`
Type string `json:"type"`
Config *ProjectConfig `json:"config"`
Statistics struct {
MessageSent int `json:"messages_sent"`
TotalApps int `json:"total_apps"`
} `json:"statistics"`
RateLimit int `json:"rate_limit"`
RateLimitDuration string `json:"rate_limit_duration"`
Metadata *ProjectMetadata `json:"metadata"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
type ProjectMetadata struct {
RetainedEvents int `json:"retained_events"`
}
type ListProjectResponse []ProjectResponse
func newProject(client *Client) *Project {
return &Project{
client: client,
}
}
func (p *Project) Find(ctx context.Context, projectID string) (*ProjectResponse, error) {
url, err := addOptions(p.generateUrl()+"/"+projectID, nil)
if err != nil {
return nil, err
}
respPtr := &ProjectResponse{}
err = getResource(ctx, p.client, url, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (p *Project) Update(ctx context.Context, projectID string, body *CreateProjectRequest) (*ProjectResponse, error) {
url, err := addOptions(p.generateUrl()+"/"+projectID, nil)
if err != nil {
return nil, err
}
respPtr := &ProjectResponse{}
err = postJSON(ctx, p.client, url, body, respPtr)
if err != nil {
return nil, err
}
return respPtr, nil
}
func (p *Project) Delete(ctx context.Context, projectID string) error {
url, err := addOptions(p.generateUrl()+"/"+projectID, nil)
if err != nil {
return err
}
err = deleteResource(ctx, p.client, url, nil)
if err != nil {
return err
}
return nil
}
func (p *Project) generateUrl() string {
return fmt.Sprintf("%s/projects", p.client.baseURL)
}