|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package email_routing |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "slices" |
| 12 | + |
| 13 | + "github.com/cloudflare/cloudflare-go/v7/internal/apijson" |
| 14 | + "github.com/cloudflare/cloudflare-go/v7/internal/apiquery" |
| 15 | + "github.com/cloudflare/cloudflare-go/v7/internal/param" |
| 16 | + "github.com/cloudflare/cloudflare-go/v7/internal/requestconfig" |
| 17 | + "github.com/cloudflare/cloudflare-go/v7/option" |
| 18 | + "github.com/cloudflare/cloudflare-go/v7/packages/pagination" |
| 19 | +) |
| 20 | + |
| 21 | +// AccountRuleService contains methods and other services that help with |
| 22 | +// interacting with the cloudflare API. |
| 23 | +// |
| 24 | +// Note, unlike clients, this service does not read variables from the environment |
| 25 | +// automatically. You should not instantiate this service directly, and instead use |
| 26 | +// the [NewAccountRuleService] method instead. |
| 27 | +type AccountRuleService struct { |
| 28 | + Options []option.RequestOption |
| 29 | +} |
| 30 | + |
| 31 | +// NewAccountRuleService generates a new service that applies the given options to |
| 32 | +// each request. These options are applied after the parent client's options (if |
| 33 | +// there is one), and before any request-specific options. |
| 34 | +func NewAccountRuleService(opts ...option.RequestOption) (r *AccountRuleService) { |
| 35 | + r = &AccountRuleService{} |
| 36 | + r.Options = opts |
| 37 | + return |
| 38 | +} |
| 39 | + |
| 40 | +// Lists existing routing rules across all zones in the account or zone. |
| 41 | +func (r *AccountRuleService) List(ctx context.Context, params AccountRuleListParams, opts ...option.RequestOption) (res *pagination.V4PagePaginationArray[AccountRule], err error) { |
| 42 | + var raw *http.Response |
| 43 | + opts = slices.Concat(r.Options, opts) |
| 44 | + opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...) |
| 45 | + var accountOrZone string |
| 46 | + var accountOrZoneID param.Field[string] |
| 47 | + if params.AccountID.Value != "" && params.ZoneID.Value != "" { |
| 48 | + err = errors.New("account ID and zone ID are mutually exclusive") |
| 49 | + return |
| 50 | + } |
| 51 | + if params.AccountID.Value == "" && params.ZoneID.Value == "" { |
| 52 | + err = errors.New("either account ID or zone ID must be provided") |
| 53 | + return |
| 54 | + } |
| 55 | + if params.AccountID.Value != "" { |
| 56 | + accountOrZone = "accounts" |
| 57 | + accountOrZoneID = params.AccountID |
| 58 | + } |
| 59 | + if params.ZoneID.Value != "" { |
| 60 | + accountOrZone = "zones" |
| 61 | + accountOrZoneID = params.ZoneID |
| 62 | + } |
| 63 | + path := fmt.Sprintf("%s/%s/email/routing/rules", accountOrZone, accountOrZoneID) |
| 64 | + cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + err = cfg.Execute() |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + res.SetPageConfig(cfg, raw) |
| 73 | + return res, nil |
| 74 | +} |
| 75 | + |
| 76 | +// Lists existing routing rules across all zones in the account or zone. |
| 77 | +func (r *AccountRuleService) ListAutoPaging(ctx context.Context, params AccountRuleListParams, opts ...option.RequestOption) *pagination.V4PagePaginationArrayAutoPager[AccountRule] { |
| 78 | + return pagination.NewV4PagePaginationArrayAutoPager(r.List(ctx, params, opts...)) |
| 79 | +} |
| 80 | + |
| 81 | +type AccountRule struct { |
| 82 | + // Routing rule identifier. |
| 83 | + ID string `json:"id"` |
| 84 | + // List actions patterns. |
| 85 | + Actions []Action `json:"actions"` |
| 86 | + // Routing rule status. |
| 87 | + Enabled AccountRuleEnabled `json:"enabled"` |
| 88 | + // Matching patterns to forward to your actions. |
| 89 | + Matchers []Matcher `json:"matchers"` |
| 90 | + // Routing rule name. |
| 91 | + Name string `json:"name"` |
| 92 | + // Priority of the routing rule. |
| 93 | + Priority float64 `json:"priority"` |
| 94 | + // Who manages the rule. `api` covers dashboard, generic API, and Terraform; |
| 95 | + // `wrangler` means the rule is managed by a Worker's wrangler.jsonc. Defaults to |
| 96 | + // `api` when omitted on write. |
| 97 | + Source AccountRuleSource `json:"source"` |
| 98 | + // Routing rule tag. (Deprecated, replaced by routing rule identifier) |
| 99 | + // |
| 100 | + // Deprecated: deprecated |
| 101 | + Tag string `json:"tag"` |
| 102 | + // Zone information for the routing rule. |
| 103 | + Zone AccountRuleZone `json:"zone"` |
| 104 | + JSON accountRuleJSON `json:"-"` |
| 105 | +} |
| 106 | + |
| 107 | +// accountRuleJSON contains the JSON metadata for the struct [AccountRule] |
| 108 | +type accountRuleJSON struct { |
| 109 | + ID apijson.Field |
| 110 | + Actions apijson.Field |
| 111 | + Enabled apijson.Field |
| 112 | + Matchers apijson.Field |
| 113 | + Name apijson.Field |
| 114 | + Priority apijson.Field |
| 115 | + Source apijson.Field |
| 116 | + Tag apijson.Field |
| 117 | + Zone apijson.Field |
| 118 | + raw string |
| 119 | + ExtraFields map[string]apijson.Field |
| 120 | +} |
| 121 | + |
| 122 | +func (r *AccountRule) UnmarshalJSON(data []byte) (err error) { |
| 123 | + return apijson.UnmarshalRoot(data, r) |
| 124 | +} |
| 125 | + |
| 126 | +func (r accountRuleJSON) RawJSON() string { |
| 127 | + return r.raw |
| 128 | +} |
| 129 | + |
| 130 | +// Routing rule status. |
| 131 | +type AccountRuleEnabled bool |
| 132 | + |
| 133 | +const ( |
| 134 | + AccountRuleEnabledTrue AccountRuleEnabled = true |
| 135 | + AccountRuleEnabledFalse AccountRuleEnabled = false |
| 136 | +) |
| 137 | + |
| 138 | +func (r AccountRuleEnabled) IsKnown() bool { |
| 139 | + switch r { |
| 140 | + case AccountRuleEnabledTrue, AccountRuleEnabledFalse: |
| 141 | + return true |
| 142 | + } |
| 143 | + return false |
| 144 | +} |
| 145 | + |
| 146 | +// Who manages the rule. `api` covers dashboard, generic API, and Terraform; |
| 147 | +// `wrangler` means the rule is managed by a Worker's wrangler.jsonc. Defaults to |
| 148 | +// `api` when omitted on write. |
| 149 | +type AccountRuleSource string |
| 150 | + |
| 151 | +const ( |
| 152 | + AccountRuleSourceAPI AccountRuleSource = "api" |
| 153 | + AccountRuleSourceWrangler AccountRuleSource = "wrangler" |
| 154 | +) |
| 155 | + |
| 156 | +func (r AccountRuleSource) IsKnown() bool { |
| 157 | + switch r { |
| 158 | + case AccountRuleSourceAPI, AccountRuleSourceWrangler: |
| 159 | + return true |
| 160 | + } |
| 161 | + return false |
| 162 | +} |
| 163 | + |
| 164 | +// Zone information for the routing rule. |
| 165 | +type AccountRuleZone struct { |
| 166 | + // Zone name. |
| 167 | + Name string `json:"name"` |
| 168 | + // Zone tag. |
| 169 | + Tag string `json:"tag"` |
| 170 | + JSON accountRuleZoneJSON `json:"-"` |
| 171 | +} |
| 172 | + |
| 173 | +// accountRuleZoneJSON contains the JSON metadata for the struct [AccountRuleZone] |
| 174 | +type accountRuleZoneJSON struct { |
| 175 | + Name apijson.Field |
| 176 | + Tag apijson.Field |
| 177 | + raw string |
| 178 | + ExtraFields map[string]apijson.Field |
| 179 | +} |
| 180 | + |
| 181 | +func (r *AccountRuleZone) UnmarshalJSON(data []byte) (err error) { |
| 182 | + return apijson.UnmarshalRoot(data, r) |
| 183 | +} |
| 184 | + |
| 185 | +func (r accountRuleZoneJSON) RawJSON() string { |
| 186 | + return r.raw |
| 187 | +} |
| 188 | + |
| 189 | +type AccountRuleListParams struct { |
| 190 | + // The Account ID to use for this endpoint. Mutually exclusive with the Zone ID. |
| 191 | + AccountID param.Field[string] `path:"account_id"` |
| 192 | + // The Zone ID to use for this endpoint. Mutually exclusive with the Account ID. |
| 193 | + ZoneID param.Field[string] `path:"zone_id"` |
| 194 | + // Filter by enabled routing rules. |
| 195 | + Enabled param.Field[AccountRuleListParamsEnabled] `query:"enabled"` |
| 196 | + // Page number of paginated results. |
| 197 | + Page param.Field[float64] `query:"page"` |
| 198 | + // Maximum number of results per page. |
| 199 | + PerPage param.Field[float64] `query:"per_page"` |
| 200 | +} |
| 201 | + |
| 202 | +// URLQuery serializes [AccountRuleListParams]'s query parameters as `url.Values`. |
| 203 | +func (r AccountRuleListParams) URLQuery() (v url.Values) { |
| 204 | + return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{ |
| 205 | + ArrayFormat: apiquery.ArrayQueryFormatRepeat, |
| 206 | + NestedFormat: apiquery.NestedQueryFormatDots, |
| 207 | + }) |
| 208 | +} |
| 209 | + |
| 210 | +// Filter by enabled routing rules. |
| 211 | +type AccountRuleListParamsEnabled bool |
| 212 | + |
| 213 | +const ( |
| 214 | + AccountRuleListParamsEnabledTrue AccountRuleListParamsEnabled = true |
| 215 | + AccountRuleListParamsEnabledFalse AccountRuleListParamsEnabled = false |
| 216 | +) |
| 217 | + |
| 218 | +func (r AccountRuleListParamsEnabled) IsKnown() bool { |
| 219 | + switch r { |
| 220 | + case AccountRuleListParamsEnabledTrue, AccountRuleListParamsEnabledFalse: |
| 221 | + return true |
| 222 | + } |
| 223 | + return false |
| 224 | +} |
0 commit comments