Skip to content

Commit b662067

Browse files
authored
Add missing imports to java/kotlin (#2086)
Add missing java/kotlin imports in preparation for stream Also, start generating the `go/svix.go` file. the missing `BackgroundTask` resource, is now part of the Go API.
2 parents 39caf32 + e5baeac commit b662067

File tree

6 files changed

+125
-5
lines changed

6 files changed

+125
-5
lines changed

codegen/codegen.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ extra_shell_commands = [
101101
"sed -i.bak 's/package svix/package internalapi/g' go/internalapi/management* && rm go/internalapi/management*.bak",
102102
]
103103
extra_codegen_args = ["-e=v1.health.get"]
104+
105+
[[go.task]]
106+
template = "templates/go/summary.go.jinja"
107+
output_dir = "go"
104108
[[go.task]]
105109
template = "templates/go/api_resource.go.jinja"
106110
output_dir = "go"

codegen/generated_files.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@
403403
"go/models/settings_internal_patch.go",
404404
"go/models/settings_internal_update_out.go"
405405
],
406+
[
407+
"go/svix.go"
408+
],
406409
[
407410
"java/lib/src/main/java/com/svix/api/Application.java",
408411
"java/lib/src/main/java/com/svix/api/Authentication.java",
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Package svix this file is @generated DO NOT EDIT
2+
package svix
3+
4+
import (
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"regexp"
9+
"strings"
10+
"time"
11+
12+
"github.com/svix/svix-webhooks/go/internal"
13+
)
14+
15+
type (
16+
SvixOptions struct {
17+
ServerUrl *url.URL
18+
HTTPClient *http.Client
19+
RetrySchedule *[]time.Duration
20+
Debug bool
21+
}
22+
Svix struct {
23+
// hidden field. allows me to override the user agent in `SvixHttpClient.DefaultHeaders["User-Agent"]`
24+
// to override the user agent use `SetUseragentSuffix`
25+
client *internal.SvixHttpClient
26+
27+
{% for resource in api.resources -%}
28+
{{ resource.name | to_upper_camel_case }} *{{ resource.name | to_upper_camel_case }}
29+
{% endfor -%}
30+
OperationalWebhookEndpoint *OperationalWebhookEndpoint
31+
}
32+
)
33+
34+
func New(token string, options *SvixOptions) (*Svix, error) {
35+
svixHttpClient := internal.DefaultSvixHttpClient(getDefaultBaseUrl(token))
36+
37+
if options != nil {
38+
if options.ServerUrl != nil {
39+
svixHttpClient.BaseURL = options.ServerUrl.String()
40+
}
41+
if options.RetrySchedule != nil {
42+
if len(*options.RetrySchedule) > 5 {
43+
return nil, fmt.Errorf("number of retries must not exceed 5")
44+
}
45+
svixHttpClient.RetrySchedule = *options.RetrySchedule
46+
47+
}
48+
if options.HTTPClient != nil {
49+
svixHttpClient.HTTPClient = options.HTTPClient
50+
}
51+
svixHttpClient.Debug = options.Debug
52+
53+
}
54+
55+
svixHttpClient.DefaultHeaders["Authorization"] = fmt.Sprintf("Bearer %s", token)
56+
svixHttpClient.DefaultHeaders["User-Agent"] = fmt.Sprintf("svix-libs/%s/go", Version)
57+
58+
svx := Svix{
59+
client: &svixHttpClient,
60+
61+
62+
{% for resource in api.resources -%}
63+
{{ resource.name | to_upper_camel_case }}: new{{ resource.name | to_upper_camel_case }}(&svixHttpClient),
64+
{% endfor -%}
65+
OperationalWebhookEndpoint: newOperationalWebhookEndpoint(&svixHttpClient),
66+
}
67+
return &svx, nil
68+
}
69+
70+
// Add a custom suffix to the default user-agent
71+
//
72+
// The default user agent is `svix-libs/<version>/go`.
73+
// The suffix will be separated from the base user agent with a `/`
74+
//
75+
// The suffix must be less then 50 chars, And must match this regex `^[A-Za-z\d\.\-]+$`
76+
func SetUserAgentSuffix(s *Svix, userAgentSuffix string) error {
77+
if len(userAgentSuffix) > 50 {
78+
return fmt.Errorf("user agent suffix must be less then 50 chars")
79+
}
80+
validateStr := regexp.MustCompile(`^[A-Za-z\d\.\-]+$`).MatchString
81+
if !validateStr(userAgentSuffix) {
82+
return fmt.Errorf("invalid user agent suffix")
83+
}
84+
85+
s.client.DefaultHeaders["User-Agent"] = fmt.Sprintf("svix-libs/%s/go/%s", Version, userAgentSuffix)
86+
return nil
87+
}
88+
89+
func getDefaultBaseUrl(token string) string {
90+
var tokenParts = strings.Split(token, ".")
91+
var region = tokenParts[len(tokenParts)-1]
92+
if region == "us" {
93+
return "https://api.us.svix.com"
94+
} else if region == "eu" {
95+
return "https://api.eu.svix.com"
96+
} else if region == "in" {
97+
return "https://api.in.svix.com"
98+
} else if region == "ca" {
99+
return "https://api.ca.svix.com"
100+
} else if region == "au" {
101+
return "https://api.au.svix.com"
102+
} else {
103+
return "https://api.svix.com"
104+
}
105+
}
106+
107+
108+
{% do set_summary_filename("svix.go") -%}

codegen/templates/java/types/struct_enum.java.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import java.lang.annotation.RetentionPolicy;
3333
import java.lang.annotation.Target;
3434
import java.util.HashMap;
3535
import java.util.Map;
36+
import java.util.List;
3637
import java.util.Objects;
3738
import java.net.URI;
3839
import java.time.OffsetDateTime;

codegen/templates/kotlin/types/struct_enum.kt.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.serialization.json.Json
1515
import kotlinx.serialization.json.JsonElement
1616
import kotlinx.serialization.json.buildJsonObject
1717
import kotlinx.datetime.Instant
18+
import com.svix.kotlin.MaybeUnset
1819

1920

2021
@Serializable(with = {{ type_name }}Serializer::class)

go/svix.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package svix this file is @generated DO NOT EDIT
12
package svix
23

34
import (
@@ -23,17 +24,18 @@ type (
2324
// to override the user agent use `SetUseragentSuffix`
2425
client *internal.SvixHttpClient
2526

26-
Authentication *Authentication
2727
Application *Application
28+
Authentication *Authentication
29+
BackgroundTask *BackgroundTask
2830
Endpoint *Endpoint
2931
Environment *Environment
3032
EventType *EventType
3133
Ingest *Ingest
3234
Integration *Integration
3335
Message *Message
3436
MessageAttempt *MessageAttempt
35-
Statistics *Statistics
3637
OperationalWebhook *OperationalWebhook
38+
Statistics *Statistics
3739
OperationalWebhookEndpoint *OperationalWebhookEndpoint
3840
}
3941
)
@@ -65,17 +67,18 @@ func New(token string, options *SvixOptions) (*Svix, error) {
6567
svx := Svix{
6668
client: &svixHttpClient,
6769

68-
Authentication: newAuthentication(&svixHttpClient),
6970
Application: newApplication(&svixHttpClient),
71+
Authentication: newAuthentication(&svixHttpClient),
72+
BackgroundTask: newBackgroundTask(&svixHttpClient),
7073
Endpoint: newEndpoint(&svixHttpClient),
7174
Environment: newEnvironment(&svixHttpClient),
7275
EventType: newEventType(&svixHttpClient),
73-
Message: newMessage(&svixHttpClient),
7476
Ingest: newIngest(&svixHttpClient),
7577
Integration: newIntegration(&svixHttpClient),
78+
Message: newMessage(&svixHttpClient),
7679
MessageAttempt: newMessageAttempt(&svixHttpClient),
77-
Statistics: newStatistics(&svixHttpClient),
7880
OperationalWebhook: newOperationalWebhook(&svixHttpClient),
81+
Statistics: newStatistics(&svixHttpClient),
7982
OperationalWebhookEndpoint: newOperationalWebhookEndpoint(&svixHttpClient),
8083
}
8184
return &svx, nil

0 commit comments

Comments
 (0)