|
| 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") -%} |
0 commit comments