Skip to content

Commit a813400

Browse files
authored
go: Add support for sub-resources (#1838)
Part of svix/monorepo-private#10043.
2 parents 71a9565 + d6528f7 commit a813400

15 files changed

+182
-31
lines changed

codegen.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ output_dir = "java/lib/src/main/java/com/svix/models"
123123
[go]
124124
template_dir = "openapi-templates/go"
125125
extra_shell_commands = [
126-
"rm go/{environment,health,ingest_endpoint,ingest,ingest_source,operational_webhook}.go",
126+
"rm go/{health,ingest_endpoint,ingest,ingest_source}.go",
127127
]
128128
[[go.task]]
129129
template = "openapi-templates/go/api_resource.go.jinja"

go/application.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Application struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newApplication(client *SvixHttpClient) *Application {
15+
return &Application{
16+
client: client,
17+
}
18+
}
19+
1420
type ApplicationListOptions struct {
1521
// Limit the number of returned items
1622
Limit *uint64

go/authentication.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Authentication struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newAuthentication(client *SvixHttpClient) *Authentication {
15+
return &Authentication{
16+
client: client,
17+
}
18+
}
19+
1420
type AuthenticationAppPortalAccessOptions struct {
1521
IdempotencyKey *string
1622
}

go/background_task.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type BackgroundTask struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newBackgroundTask(client *SvixHttpClient) *BackgroundTask {
15+
return &BackgroundTask{
16+
client: client,
17+
}
18+
}
19+
1420
type BackgroundTaskListOptions struct {
1521

1622
// Filter the response based on the status.

go/endpoint.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type Endpoint struct {
1212
client *SvixHttpClient
1313
}
1414

15+
func newEndpoint(client *SvixHttpClient) *Endpoint {
16+
return &Endpoint{
17+
client: client,
18+
}
19+
}
20+
1521
type EndpointListOptions struct {
1622
// Limit the number of returned items
1723
Limit *uint64

go/environment.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Package svix this file is @generated DO NOT EDIT
2+
package svix
3+
4+
import (
5+
"context"
6+
7+
"github.com/svix/svix-webhooks/go/models"
8+
)
9+
10+
type Environment struct {
11+
client *SvixHttpClient
12+
}
13+
14+
func newEnvironment(client *SvixHttpClient) *Environment {
15+
return &Environment{
16+
client: client,
17+
}
18+
}
19+
20+
type EnvironmentExportOptions struct {
21+
IdempotencyKey *string
22+
}
23+
24+
type EnvironmentImportOptions struct {
25+
IdempotencyKey *string
26+
}
27+
28+
// Download a JSON file containing all org-settings and event types.
29+
func (environment *Environment) Export(
30+
ctx context.Context,
31+
o *EnvironmentExportOptions,
32+
) (*models.EnvironmentOut, error) {
33+
headerMap := map[string]string{}
34+
var err error
35+
if o != nil {
36+
serializeParamToMap("idempotency-key", o.IdempotencyKey, headerMap, &err)
37+
if err != nil {
38+
return nil, err
39+
}
40+
}
41+
return executeRequest[any, models.EnvironmentOut](
42+
ctx,
43+
environment.client,
44+
"POST",
45+
"/api/v1/environment/export",
46+
nil,
47+
nil,
48+
headerMap,
49+
nil,
50+
)
51+
}
52+
53+
// Import a configuration into the active organization.
54+
//
55+
// It doesn't delete anything, only adds / updates what was passed to it.
56+
func (environment *Environment) Import(
57+
ctx context.Context,
58+
environmentIn models.EnvironmentIn,
59+
o *EnvironmentImportOptions,
60+
) error {
61+
headerMap := map[string]string{}
62+
var err error
63+
if o != nil {
64+
serializeParamToMap("idempotency-key", o.IdempotencyKey, headerMap, &err)
65+
if err != nil {
66+
return err
67+
}
68+
}
69+
_, err = executeRequest[models.EnvironmentIn, any](
70+
ctx,
71+
environment.client,
72+
"POST",
73+
"/api/v1/environment/import",
74+
nil,
75+
nil,
76+
headerMap,
77+
&environmentIn,
78+
)
79+
return err
80+
}

go/event_type.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type EventType struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newEventType(client *SvixHttpClient) *EventType {
15+
return &EventType{
16+
client: client,
17+
}
18+
}
19+
1420
type EventTypeListOptions struct {
1521
// Limit the number of returned items
1622
Limit *uint64

go/integration.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Integration struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newIntegration(client *SvixHttpClient) *Integration {
15+
return &Integration{
16+
client: client,
17+
}
18+
}
19+
1420
type IntegrationListOptions struct {
1521
// Limit the number of returned items
1622
Limit *uint64

go/message.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type Message struct {
1212
client *SvixHttpClient
1313
}
1414

15+
func newMessage(client *SvixHttpClient) *Message {
16+
return &Message{
17+
client: client,
18+
}
19+
}
20+
1521
type MessageListOptions struct {
1622
// Limit the number of returned items
1723
Limit *uint64

go/message_attempt.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type MessageAttempt struct {
1212
client *SvixHttpClient
1313
}
1414

15+
func newMessageAttempt(client *SvixHttpClient) *MessageAttempt {
16+
return &MessageAttempt{
17+
client: client,
18+
}
19+
}
20+
1521
type MessageAttemptListByEndpointOptions struct {
1622
// Limit the number of returned items
1723
Limit *uint64

go/operational_webhook.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Package svix this file is @generated DO NOT EDIT
2+
package svix
3+
4+
type OperationalWebhook struct {
5+
Endpoint *OperationalWebhookEndpoint
6+
}
7+
8+
func newOperationalWebhook(client *SvixHttpClient) *OperationalWebhook {
9+
return &OperationalWebhook{
10+
Endpoint: newOperationalWebhookEndpoint(client),
11+
}
12+
}

go/operational_webhook_endpoint.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type OperationalWebhookEndpoint struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newOperationalWebhookEndpoint(client *SvixHttpClient) *OperationalWebhookEndpoint {
15+
return &OperationalWebhookEndpoint{
16+
client: client,
17+
}
18+
}
19+
1420
type OperationalWebhookEndpointListOptions struct {
1521
// Limit the number of returned items
1622
Limit *uint64

go/statistics.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Statistics struct {
1111
client *SvixHttpClient
1212
}
1313

14+
func newStatistics(client *SvixHttpClient) *Statistics {
15+
return &Statistics{
16+
client: client,
17+
}
18+
}
19+
1420
type StatisticsAggregateAppStatsOptions struct {
1521
IdempotencyKey *string
1622
}

go/svix.go

+13-28
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ type (
1919
Authentication *Authentication
2020
Application *Application
2121
Endpoint *Endpoint
22+
Environment *Environment
2223
EventType *EventType
2324
Integration *Integration
2425
Message *Message
2526
MessageAttempt *MessageAttempt
2627
Statistics *Statistics
28+
OperationalWebhook *OperationalWebhook
2729
OperationalWebhookEndpoint *OperationalWebhookEndpoint
2830
}
2931
)
@@ -53,33 +55,17 @@ func New(token string, options *SvixOptions) (*Svix, error) {
5355
svixHttpClient.DefaultHeaders["User-Agent"] = fmt.Sprintf("svix-libs/%s/go", Version)
5456

5557
svx := Svix{
56-
Authentication: &Authentication{
57-
client: &svixHttpClient,
58-
},
59-
Application: &Application{
60-
client: &svixHttpClient,
61-
},
62-
Endpoint: &Endpoint{
63-
client: &svixHttpClient,
64-
},
65-
EventType: &EventType{
66-
client: &svixHttpClient,
67-
},
68-
Message: &Message{
69-
client: &svixHttpClient,
70-
},
71-
Integration: &Integration{
72-
client: &svixHttpClient,
73-
},
74-
MessageAttempt: &MessageAttempt{
75-
client: &svixHttpClient,
76-
},
77-
Statistics: &Statistics{
78-
client: &svixHttpClient,
79-
},
80-
OperationalWebhookEndpoint: &OperationalWebhookEndpoint{
81-
client: &svixHttpClient,
82-
},
58+
Authentication: newAuthentication(&svixHttpClient),
59+
Application: newApplication(&svixHttpClient),
60+
Endpoint: newEndpoint(&svixHttpClient),
61+
Environment: newEnvironment(&svixHttpClient),
62+
EventType: newEventType(&svixHttpClient),
63+
Message: newMessage(&svixHttpClient),
64+
Integration: newIntegration(&svixHttpClient),
65+
MessageAttempt: newMessageAttempt(&svixHttpClient),
66+
Statistics: newStatistics(&svixHttpClient),
67+
OperationalWebhook: newOperationalWebhook(&svixHttpClient),
68+
OperationalWebhookEndpoint: newOperationalWebhookEndpoint(&svixHttpClient),
8369
}
8470
return &svx, nil
8571
}
@@ -96,5 +82,4 @@ func getDefaultBaseUrl(token string) string {
9682
} else {
9783
return "https://api.svix.com"
9884
}
99-
10085
}

openapi-templates/go/api_resource.go.jinja

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@ import (
1414

1515

1616
type {{ resource_type_name }} struct {
17+
{% if resource.operations | length > 0 -%}
1718
client *SvixHttpClient
19+
{%- endif %}
20+
{%- for name, sub in resource.subresources | items %}
21+
{{ name | to_upper_camel_case }} *{{ sub.name | to_upper_camel_case }}
22+
{%- endfor %}
1823
}
1924

25+
func new{{ resource_type_name }}(client *SvixHttpClient) *{{ resource_type_name }} {
26+
return &{{ resource_type_name }}{
27+
{% if resource.operations | length > 0 -%}
28+
client: client,
29+
{% endif -%}
30+
{% for name, sub in resource.subresources | items -%}
31+
{{ name | to_upper_camel_case }}: new{{ sub.name | to_upper_camel_case }}(client),
32+
{% endfor -%}
33+
}
34+
}
2035

2136
{% for op in resource.operations -%}
2237
{% if op | has_query_or_header_params -%}
2338
{% set opt_struct_name %}{{ resource_type_name }}{{ op.name | to_upper_camel_case }}Options{% endset %}
24-
type {{ opt_struct_name }} struct{
39+
type {{ opt_struct_name }} struct {
2540
{% for p in op.query_params -%}
2641
{% set p_ty = p.type.to_go() -%}
2742
{% if p.type.is_schema_ref() -%}
@@ -36,7 +51,6 @@ type {{ opt_struct_name }} struct{
3651
{% for p in op.header_params -%}
3752
{{ p.name | to_upper_camel_case }} *string
3853
{% endfor -%}
39-
4054
}
4155
{% endif -%}
4256
{% endfor -%}

0 commit comments

Comments
 (0)