Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 33adc3c

Browse files
committedMar 13, 2025·
feat(client): improve default client options support (#4021)
1 parent b434a36 commit 33adc3c

18 files changed

+239
-53
lines changed
 

‎client.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,10 @@ type Client struct {
208208
CustomPages *custom_pages.CustomPageService
209209
}
210210

211-
// NewClient generates a new client with the default option read from the
212-
// environment (CLOUDFLARE_API_TOKEN, CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL,
213-
// CLOUDFLARE_API_USER_SERVICE_KEY). The option passed in as arguments are applied
214-
// after these default arguments, and all option will be passed down to the
215-
// services and requests that this client makes.
216-
func NewClient(opts ...option.RequestOption) (r *Client) {
211+
// DefaultClientOptions read from the environment (CLOUDFLARE_API_TOKEN,
212+
// CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL, CLOUDFLARE_API_USER_SERVICE_KEY). This
213+
// should be used to initialize new clients.
214+
func DefaultClientOptions() []option.RequestOption {
217215
defaults := []option.RequestOption{option.WithEnvironmentProduction()}
218216
if o, ok := os.LookupEnv("CLOUDFLARE_API_TOKEN"); ok {
219217
defaults = append(defaults, option.WithAPIToken(o))
@@ -227,7 +225,16 @@ func NewClient(opts ...option.RequestOption) (r *Client) {
227225
if o, ok := os.LookupEnv("CLOUDFLARE_API_USER_SERVICE_KEY"); ok {
228226
defaults = append(defaults, option.WithUserServiceKey(o))
229227
}
230-
opts = append(defaults, opts...)
228+
return defaults
229+
}
230+
231+
// NewClient generates a new client with the default option read from the
232+
// environment (CLOUDFLARE_API_TOKEN, CLOUDFLARE_API_KEY, CLOUDFLARE_EMAIL,
233+
// CLOUDFLARE_API_USER_SERVICE_KEY). The option passed in as arguments are applied
234+
// after these default arguments, and all option will be passed down to the
235+
// services and requests that this client makes.
236+
func NewClient(opts ...option.RequestOption) (r *Client) {
237+
opts = append(DefaultClientOptions(), opts...)
231238

232239
r = &Client{Options: opts}
233240

‎cloudforce_one/threatevent.go

+20
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func NewThreatEventService(opts ...option.RequestOption) (r *ThreatEventService)
6161
// Filter and list events
6262
func (r *ThreatEventService) New(ctx context.Context, params ThreatEventNewParams, opts ...option.RequestOption) (res *[]ThreatEventNewResponse, err error) {
6363
opts = append(r.Options[:], opts...)
64+
if !params.AccountID.Present {
65+
err = errors.New("missing required account_id parameter")
66+
return
67+
}
6468
path := fmt.Sprintf("accounts/%v/cloudforce-one/events", params.AccountID)
6569
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
6670
return
@@ -69,6 +73,10 @@ func (r *ThreatEventService) New(ctx context.Context, params ThreatEventNewParam
6973
// Deletes an event
7074
func (r *ThreatEventService) Delete(ctx context.Context, eventID string, body ThreatEventDeleteParams, opts ...option.RequestOption) (res *ThreatEventDeleteResponse, err error) {
7175
opts = append(r.Options[:], opts...)
76+
if !body.AccountID.Present {
77+
err = errors.New("missing required account_id parameter")
78+
return
79+
}
7280
if eventID == "" {
7381
err = errors.New("missing required event_id parameter")
7482
return
@@ -81,6 +89,10 @@ func (r *ThreatEventService) Delete(ctx context.Context, eventID string, body Th
8189
// Creates bulk events
8290
func (r *ThreatEventService) BulkNew(ctx context.Context, params ThreatEventBulkNewParams, opts ...option.RequestOption) (res *[]ThreatEventBulkNewResponse, err error) {
8391
opts = append(r.Options[:], opts...)
92+
if !params.AccountID.Present {
93+
err = errors.New("missing required account_id parameter")
94+
return
95+
}
8496
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/create/bulk", params.AccountID)
8597
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
8698
return
@@ -89,6 +101,10 @@ func (r *ThreatEventService) BulkNew(ctx context.Context, params ThreatEventBulk
89101
// Updates an event
90102
func (r *ThreatEventService) Edit(ctx context.Context, eventID string, params ThreatEventEditParams, opts ...option.RequestOption) (res *ThreatEventEditResponse, err error) {
91103
opts = append(r.Options[:], opts...)
104+
if !params.AccountID.Present {
105+
err = errors.New("missing required account_id parameter")
106+
return
107+
}
92108
if eventID == "" {
93109
err = errors.New("missing required event_id parameter")
94110
return
@@ -101,6 +117,10 @@ func (r *ThreatEventService) Edit(ctx context.Context, eventID string, params Th
101117
// Reads an event
102118
func (r *ThreatEventService) Get(ctx context.Context, eventID string, query ThreatEventGetParams, opts ...option.RequestOption) (res *ThreatEventGetResponse, err error) {
103119
opts = append(r.Options[:], opts...)
120+
if !query.AccountID.Present {
121+
err = errors.New("missing required account_id parameter")
122+
return
123+
}
104124
if eventID == "" {
105125
err = errors.New("missing required event_id parameter")
106126
return

‎cloudforce_one/threateventattacker.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cloudforce_one
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"net/http"
910

@@ -35,6 +36,10 @@ func NewThreatEventAttackerService(opts ...option.RequestOption) (r *ThreatEvent
3536
// Lists attackers
3637
func (r *ThreatEventAttackerService) List(ctx context.Context, query ThreatEventAttackerListParams, opts ...option.RequestOption) (res *ThreatEventAttackerListResponse, err error) {
3738
opts = append(r.Options[:], opts...)
39+
if !query.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3843
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/attackers", query.AccountID)
3944
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4045
return

‎cloudforce_one/threateventcategory.go

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func NewThreatEventCategoryService(opts ...option.RequestOption) (r *ThreatEvent
3636
// Creates a new category
3737
func (r *ThreatEventCategoryService) New(ctx context.Context, params ThreatEventCategoryNewParams, opts ...option.RequestOption) (res *ThreatEventCategoryNewResponse, err error) {
3838
opts = append(r.Options[:], opts...)
39+
if !params.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3943
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/categories/create", params.AccountID)
4044
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
4145
return
@@ -44,6 +48,10 @@ func (r *ThreatEventCategoryService) New(ctx context.Context, params ThreatEvent
4448
// Lists categories
4549
func (r *ThreatEventCategoryService) List(ctx context.Context, query ThreatEventCategoryListParams, opts ...option.RequestOption) (res *[]ThreatEventCategoryListResponse, err error) {
4650
opts = append(r.Options[:], opts...)
51+
if !query.AccountID.Present {
52+
err = errors.New("missing required account_id parameter")
53+
return
54+
}
4755
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/categories", query.AccountID)
4856
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4957
return
@@ -52,6 +60,10 @@ func (r *ThreatEventCategoryService) List(ctx context.Context, query ThreatEvent
5260
// Deletes a category
5361
func (r *ThreatEventCategoryService) Delete(ctx context.Context, categoryID string, body ThreatEventCategoryDeleteParams, opts ...option.RequestOption) (res *ThreatEventCategoryDeleteResponse, err error) {
5462
opts = append(r.Options[:], opts...)
63+
if !body.AccountID.Present {
64+
err = errors.New("missing required account_id parameter")
65+
return
66+
}
5567
if categoryID == "" {
5668
err = errors.New("missing required category_id parameter")
5769
return
@@ -64,6 +76,10 @@ func (r *ThreatEventCategoryService) Delete(ctx context.Context, categoryID stri
6476
// Updates a category
6577
func (r *ThreatEventCategoryService) Edit(ctx context.Context, categoryID string, params ThreatEventCategoryEditParams, opts ...option.RequestOption) (res *ThreatEventCategoryEditResponse, err error) {
6678
opts = append(r.Options[:], opts...)
79+
if !params.AccountID.Present {
80+
err = errors.New("missing required account_id parameter")
81+
return
82+
}
6783
if categoryID == "" {
6884
err = errors.New("missing required category_id parameter")
6985
return
@@ -76,6 +92,10 @@ func (r *ThreatEventCategoryService) Edit(ctx context.Context, categoryID string
7692
// Reads a category
7793
func (r *ThreatEventCategoryService) Get(ctx context.Context, categoryID string, query ThreatEventCategoryGetParams, opts ...option.RequestOption) (res *ThreatEventCategoryGetResponse, err error) {
7894
opts = append(r.Options[:], opts...)
95+
if !query.AccountID.Present {
96+
err = errors.New("missing required account_id parameter")
97+
return
98+
}
7999
if categoryID == "" {
80100
err = errors.New("missing required category_id parameter")
81101
return

‎cloudforce_one/threateventcountry.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cloudforce_one
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"net/http"
910

@@ -35,6 +36,10 @@ func NewThreatEventCountryService(opts ...option.RequestOption) (r *ThreatEventC
3536
// Retrieves countries information for all countries
3637
func (r *ThreatEventCountryService) List(ctx context.Context, query ThreatEventCountryListParams, opts ...option.RequestOption) (res *[]ThreatEventCountryListResponse, err error) {
3738
opts = append(r.Options[:], opts...)
39+
if !query.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3843
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/countries", query.AccountID)
3944
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4045
return

‎cloudforce_one/threateventcron.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cloudforce_one
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"net/http"
910

@@ -35,6 +36,10 @@ func NewThreatEventCronService(opts ...option.RequestOption) (r *ThreatEventCron
3536
// Reads the last cron update time
3637
func (r *ThreatEventCronService) List(ctx context.Context, query ThreatEventCronListParams, opts ...option.RequestOption) (res *ThreatEventCronListResponse, err error) {
3738
opts = append(r.Options[:], opts...)
39+
if !query.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3843
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/cron", query.AccountID)
3944
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4045
return
@@ -43,6 +48,10 @@ func (r *ThreatEventCronService) List(ctx context.Context, query ThreatEventCron
4348
// Reads the last cron update time
4449
func (r *ThreatEventCronService) Edit(ctx context.Context, body ThreatEventCronEditParams, opts ...option.RequestOption) (res *ThreatEventCronEditResponse, err error) {
4550
opts = append(r.Options[:], opts...)
51+
if !body.AccountID.Present {
52+
err = errors.New("missing required account_id parameter")
53+
return
54+
}
4655
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/cron", body.AccountID)
4756
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, nil, &res, opts...)
4857
return

‎cloudforce_one/threateventdataset.go

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func NewThreatEventDatasetService(opts ...option.RequestOption) (r *ThreatEventD
3636
// Creates a dataset
3737
func (r *ThreatEventDatasetService) New(ctx context.Context, params ThreatEventDatasetNewParams, opts ...option.RequestOption) (res *ThreatEventDatasetNewResponse, err error) {
3838
opts = append(r.Options[:], opts...)
39+
if !params.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3943
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/dataset/create", params.AccountID)
4044
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
4145
return
@@ -44,6 +48,10 @@ func (r *ThreatEventDatasetService) New(ctx context.Context, params ThreatEventD
4448
// Lists all datasets in an account
4549
func (r *ThreatEventDatasetService) List(ctx context.Context, query ThreatEventDatasetListParams, opts ...option.RequestOption) (res *[]ThreatEventDatasetListResponse, err error) {
4650
opts = append(r.Options[:], opts...)
51+
if !query.AccountID.Present {
52+
err = errors.New("missing required account_id parameter")
53+
return
54+
}
4755
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/dataset", query.AccountID)
4856
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4957
return
@@ -52,6 +60,10 @@ func (r *ThreatEventDatasetService) List(ctx context.Context, query ThreatEventD
5260
// Updates an existing dataset
5361
func (r *ThreatEventDatasetService) Edit(ctx context.Context, datasetID string, params ThreatEventDatasetEditParams, opts ...option.RequestOption) (res *ThreatEventDatasetEditResponse, err error) {
5462
opts = append(r.Options[:], opts...)
63+
if !params.AccountID.Present {
64+
err = errors.New("missing required account_id parameter")
65+
return
66+
}
5567
if datasetID == "" {
5668
err = errors.New("missing required dataset_id parameter")
5769
return
@@ -64,6 +76,10 @@ func (r *ThreatEventDatasetService) Edit(ctx context.Context, datasetID string,
6476
// Reads a dataset
6577
func (r *ThreatEventDatasetService) Get(ctx context.Context, datasetID string, query ThreatEventDatasetGetParams, opts ...option.RequestOption) (res *ThreatEventDatasetGetResponse, err error) {
6678
opts = append(r.Options[:], opts...)
79+
if !query.AccountID.Present {
80+
err = errors.New("missing required account_id parameter")
81+
return
82+
}
6783
if datasetID == "" {
6884
err = errors.New("missing required dataset_id parameter")
6985
return
@@ -76,6 +92,10 @@ func (r *ThreatEventDatasetService) Get(ctx context.Context, datasetID string, q
7692
// Reads data for a raw event
7793
func (r *ThreatEventDatasetService) Raw(ctx context.Context, datasetID string, eventID string, query ThreatEventDatasetRawParams, opts ...option.RequestOption) (res *ThreatEventDatasetRawResponse, err error) {
7894
opts = append(r.Options[:], opts...)
95+
if !query.AccountID.Present {
96+
err = errors.New("missing required account_id parameter")
97+
return
98+
}
7999
if datasetID == "" {
80100
err = errors.New("missing required dataset_id parameter")
81101
return

‎cloudforce_one/threateventeventtag.go

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func NewThreatEventEventTagService(opts ...option.RequestOption) (r *ThreatEvent
3737
func (r *ThreatEventEventTagService) New(ctx context.Context, eventID string, params ThreatEventEventTagNewParams, opts ...option.RequestOption) (res *ThreatEventEventTagNewResponse, err error) {
3838
var env ThreatEventEventTagNewResponseEnvelope
3939
opts = append(r.Options[:], opts...)
40+
if !params.AccountID.Present {
41+
err = errors.New("missing required account_id parameter")
42+
return
43+
}
4044
if eventID == "" {
4145
err = errors.New("missing required event_id parameter")
4246
return
@@ -54,6 +58,10 @@ func (r *ThreatEventEventTagService) New(ctx context.Context, eventID string, pa
5458
func (r *ThreatEventEventTagService) Delete(ctx context.Context, eventID string, body ThreatEventEventTagDeleteParams, opts ...option.RequestOption) (res *ThreatEventEventTagDeleteResponse, err error) {
5559
var env ThreatEventEventTagDeleteResponseEnvelope
5660
opts = append(r.Options[:], opts...)
61+
if !body.AccountID.Present {
62+
err = errors.New("missing required account_id parameter")
63+
return
64+
}
5765
if eventID == "" {
5866
err = errors.New("missing required event_id parameter")
5967
return

‎cloudforce_one/threateventindicatortype.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cloudforce_one
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"net/http"
910

@@ -35,6 +36,10 @@ func NewThreatEventIndicatorTypeService(opts ...option.RequestOption) (r *Threat
3536
// Lists all indicator types
3637
func (r *ThreatEventIndicatorTypeService) List(ctx context.Context, query ThreatEventIndicatorTypeListParams, opts ...option.RequestOption) (res *ThreatEventIndicatorTypeListResponse, err error) {
3738
opts = append(r.Options[:], opts...)
39+
if !query.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3843
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/indicatorTypes", query.AccountID)
3944
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4045
return

‎cloudforce_one/threateventinsight.go

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func NewThreatEventInsightService(opts ...option.RequestOption) (r *ThreatEventI
3737
func (r *ThreatEventInsightService) Delete(ctx context.Context, eventID string, insightID string, body ThreatEventInsightDeleteParams, opts ...option.RequestOption) (res *ThreatEventInsightDeleteResponse, err error) {
3838
var env ThreatEventInsightDeleteResponseEnvelope
3939
opts = append(r.Options[:], opts...)
40+
if !body.AccountID.Present {
41+
err = errors.New("missing required account_id parameter")
42+
return
43+
}
4044
if eventID == "" {
4145
err = errors.New("missing required event_id parameter")
4246
return
@@ -58,6 +62,10 @@ func (r *ThreatEventInsightService) Delete(ctx context.Context, eventID string,
5862
func (r *ThreatEventInsightService) Creat(ctx context.Context, eventID string, params ThreatEventInsightCreatParams, opts ...option.RequestOption) (res *ThreatEventInsightCreatResponse, err error) {
5963
var env ThreatEventInsightCreatResponseEnvelope
6064
opts = append(r.Options[:], opts...)
65+
if !params.AccountID.Present {
66+
err = errors.New("missing required account_id parameter")
67+
return
68+
}
6169
if eventID == "" {
6270
err = errors.New("missing required event_id parameter")
6371
return
@@ -75,6 +83,10 @@ func (r *ThreatEventInsightService) Creat(ctx context.Context, eventID string, p
7583
func (r *ThreatEventInsightService) Edit(ctx context.Context, eventID string, insightID string, params ThreatEventInsightEditParams, opts ...option.RequestOption) (res *ThreatEventInsightEditResponse, err error) {
7684
var env ThreatEventInsightEditResponseEnvelope
7785
opts = append(r.Options[:], opts...)
86+
if !params.AccountID.Present {
87+
err = errors.New("missing required account_id parameter")
88+
return
89+
}
7890
if eventID == "" {
7991
err = errors.New("missing required event_id parameter")
8092
return
@@ -96,6 +108,10 @@ func (r *ThreatEventInsightService) Edit(ctx context.Context, eventID string, in
96108
func (r *ThreatEventInsightService) Get(ctx context.Context, eventID string, insightID string, query ThreatEventInsightGetParams, opts ...option.RequestOption) (res *ThreatEventInsightGetResponse, err error) {
97109
var env ThreatEventInsightGetResponseEnvelope
98110
opts = append(r.Options[:], opts...)
111+
if !query.AccountID.Present {
112+
err = errors.New("missing required account_id parameter")
113+
return
114+
}
99115
if eventID == "" {
100116
err = errors.New("missing required event_id parameter")
101117
return

‎cloudforce_one/threateventraw.go

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func NewThreatEventRawService(opts ...option.RequestOption) (r *ThreatEventRawSe
3636
// Updates a raw event
3737
func (r *ThreatEventRawService) Edit(ctx context.Context, eventID string, rawID string, params ThreatEventRawEditParams, opts ...option.RequestOption) (res *ThreatEventRawEditResponse, err error) {
3838
opts = append(r.Options[:], opts...)
39+
if !params.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3943
if eventID == "" {
4044
err = errors.New("missing required event_id parameter")
4145
return
@@ -52,6 +56,10 @@ func (r *ThreatEventRawService) Edit(ctx context.Context, eventID string, rawID
5256
// Reads data for a raw event
5357
func (r *ThreatEventRawService) Get(ctx context.Context, eventID string, rawID string, query ThreatEventRawGetParams, opts ...option.RequestOption) (res *ThreatEventRawGetResponse, err error) {
5458
opts = append(r.Options[:], opts...)
59+
if !query.AccountID.Present {
60+
err = errors.New("missing required account_id parameter")
61+
return
62+
}
5563
if eventID == "" {
5664
err = errors.New("missing required event_id parameter")
5765
return

‎cloudforce_one/threateventrelate.go

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func NewThreatEventRelateService(opts ...option.RequestOption) (r *ThreatEventRe
3737
func (r *ThreatEventRelateService) Delete(ctx context.Context, eventID string, body ThreatEventRelateDeleteParams, opts ...option.RequestOption) (res *ThreatEventRelateDeleteResponse, err error) {
3838
var env ThreatEventRelateDeleteResponseEnvelope
3939
opts = append(r.Options[:], opts...)
40+
if !body.AccountID.Present {
41+
err = errors.New("missing required account_id parameter")
42+
return
43+
}
4044
if eventID == "" {
4145
err = errors.New("missing required event_id parameter")
4246
return

‎cloudforce_one/threateventtag.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cloudforce_one
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"net/http"
910

@@ -35,6 +36,10 @@ func NewThreatEventTagService(opts ...option.RequestOption) (r *ThreatEventTagSe
3536
// Creates a new tag
3637
func (r *ThreatEventTagService) New(ctx context.Context, params ThreatEventTagNewParams, opts ...option.RequestOption) (res *ThreatEventTagNewResponse, err error) {
3738
opts = append(r.Options[:], opts...)
39+
if !params.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3843
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/tags/create", params.AccountID)
3944
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
4045
return

‎cloudforce_one/threateventtargetindustry.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cloudforce_one
44

55
import (
66
"context"
7+
"errors"
78
"fmt"
89
"net/http"
910

@@ -35,6 +36,10 @@ func NewThreatEventTargetIndustryService(opts ...option.RequestOption) (r *Threa
3536
// Lists all target industries
3637
func (r *ThreatEventTargetIndustryService) List(ctx context.Context, query ThreatEventTargetIndustryListParams, opts ...option.RequestOption) (res *ThreatEventTargetIndustryListResponse, err error) {
3738
opts = append(r.Options[:], opts...)
39+
if !query.AccountID.Present {
40+
err = errors.New("missing required account_id parameter")
41+
return
42+
}
3843
path := fmt.Sprintf("accounts/%v/cloudforce-one/events/targetIndustries", query.AccountID)
3944
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
4045
return

‎internal/requestconfig/requestconfig.go

+38-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/cloudflare/cloudflare-go/v4/internal/apierror"
2323
"github.com/cloudflare/cloudflare-go/v4/internal/apiform"
2424
"github.com/cloudflare/cloudflare-go/v4/internal/apiquery"
25+
"github.com/cloudflare/cloudflare-go/v4/internal/param"
2526
)
2627

2728
func getDefaultHeaders() map[string]string {
@@ -77,7 +78,17 @@ func getPlatformProperties() map[string]string {
7778
}
7879
}
7980

80-
func NewRequestConfig(ctx context.Context, method string, u string, body interface{}, dst interface{}, opts ...func(*RequestConfig) error) (*RequestConfig, error) {
81+
type RequestOption interface {
82+
Apply(*RequestConfig) error
83+
}
84+
85+
type RequestOptionFunc func(*RequestConfig) error
86+
type PreRequestOptionFunc func(*RequestConfig) error
87+
88+
func (s RequestOptionFunc) Apply(r *RequestConfig) error { return s(r) }
89+
func (s PreRequestOptionFunc) Apply(r *RequestConfig) error { return s(r) }
90+
91+
func NewRequestConfig(ctx context.Context, method string, u string, body interface{}, dst interface{}, opts ...RequestOption) (*RequestConfig, error) {
8192
var reader io.Reader
8293

8394
contentType := "application/json"
@@ -174,10 +185,17 @@ func NewRequestConfig(ctx context.Context, method string, u string, body interfa
174185
return &cfg, nil
175186
}
176187

188+
func UseDefaultParam[T any](dst *param.Field[T], src *T) {
189+
if !dst.Present && src != nil {
190+
dst.Value = *src
191+
dst.Present = true
192+
}
193+
}
194+
177195
// RequestConfig represents all the state related to one request.
178196
//
179197
// Editing the variables inside RequestConfig directly is unstable api. Prefer
180-
// composing func(\*RequestConfig) error instead if possible.
198+
// composing the RequestOption instead if possible.
181199
type RequestConfig struct {
182200
MaxRetries int
183201
RequestTimeout time.Duration
@@ -520,7 +538,7 @@ func (cfg *RequestConfig) Execute() (err error) {
520538
return nil
521539
}
522540

523-
func ExecuteNewRequest(ctx context.Context, method string, u string, body interface{}, dst interface{}, opts ...func(*RequestConfig) error) error {
541+
func ExecuteNewRequest(ctx context.Context, method string, u string, body interface{}, dst interface{}, opts ...RequestOption) error {
524542
cfg, err := NewRequestConfig(ctx, method, u, body, dst, opts...)
525543
if err != nil {
526544
return err
@@ -557,12 +575,27 @@ func (cfg *RequestConfig) Clone(ctx context.Context) *RequestConfig {
557575
return new
558576
}
559577

560-
func (cfg *RequestConfig) Apply(opts ...func(*RequestConfig) error) error {
578+
func (cfg *RequestConfig) Apply(opts ...RequestOption) error {
561579
for _, opt := range opts {
562-
err := opt(cfg)
580+
err := opt.Apply(cfg)
563581
if err != nil {
564582
return err
565583
}
566584
}
567585
return nil
568586
}
587+
588+
func PreRequestOptions(opts ...RequestOption) (RequestConfig, error) {
589+
cfg := RequestConfig{}
590+
for _, opt := range opts {
591+
if _, ok := opt.(PreRequestOptionFunc); !ok {
592+
continue
593+
}
594+
595+
err := opt.Apply(&cfg)
596+
if err != nil {
597+
return cfg, err
598+
}
599+
}
600+
return cfg, nil
601+
}

‎magic_transit/connectorevent.go

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func NewConnectorEventService(opts ...option.RequestOption) (r *ConnectorEventSe
4141
func (r *ConnectorEventService) List(ctx context.Context, connectorID string, params ConnectorEventListParams, opts ...option.RequestOption) (res *ConnectorEventListResponse, err error) {
4242
var env ConnectorEventListResponseEnvelope
4343
opts = append(r.Options[:], opts...)
44+
if !params.AccountID.Present {
45+
err = errors.New("missing required account_id parameter")
46+
return
47+
}
4448
if connectorID == "" {
4549
err = errors.New("missing required connector_id parameter")
4650
return
@@ -58,6 +62,10 @@ func (r *ConnectorEventService) List(ctx context.Context, connectorID string, pa
5862
func (r *ConnectorEventService) Get(ctx context.Context, connectorID string, eventT float64, eventN float64, query ConnectorEventGetParams, opts ...option.RequestOption) (res *ConnectorEventGetResponse, err error) {
5963
var env ConnectorEventGetResponseEnvelope
6064
opts = append(r.Options[:], opts...)
65+
if !query.AccountID.Present {
66+
err = errors.New("missing required account_id parameter")
67+
return
68+
}
6169
if connectorID == "" {
6270
err = errors.New("missing required connector_id parameter")
6371
return

‎magic_transit/connectorsnapshot.go

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func NewConnectorSnapshotService(opts ...option.RequestOption) (r *ConnectorSnap
3939
func (r *ConnectorSnapshotService) List(ctx context.Context, connectorID string, params ConnectorSnapshotListParams, opts ...option.RequestOption) (res *ConnectorSnapshotListResponse, err error) {
4040
var env ConnectorSnapshotListResponseEnvelope
4141
opts = append(r.Options[:], opts...)
42+
if !params.AccountID.Present {
43+
err = errors.New("missing required account_id parameter")
44+
return
45+
}
4246
if connectorID == "" {
4347
err = errors.New("missing required connector_id parameter")
4448
return
@@ -56,6 +60,10 @@ func (r *ConnectorSnapshotService) List(ctx context.Context, connectorID string,
5660
func (r *ConnectorSnapshotService) Get(ctx context.Context, connectorID string, snapshotT float64, query ConnectorSnapshotGetParams, opts ...option.RequestOption) (res *ConnectorSnapshotGetResponse, err error) {
5761
var env ConnectorSnapshotGetResponseEnvelope
5862
opts = append(r.Options[:], opts...)
63+
if !query.AccountID.Present {
64+
err = errors.New("missing required account_id parameter")
65+
return
66+
}
5967
if connectorID == "" {
6068
err = errors.New("missing required connector_id parameter")
6169
return

‎option/requestoption.go

+41-41
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
// options pattern in our [README].
2222
//
2323
// [README]: https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4#readme-requestoptions
24-
type RequestOption = func(*requestconfig.RequestConfig) error
24+
type RequestOption = requestconfig.RequestOption
2525

2626
// WithAPIVersion returns a RequestOption that defines the API version the client is attempting
2727
// to use. While any value can be set here, invalid values are ignored and will recieve the
2828
// default value of today.
2929
func WithAPIVersion(value string) RequestOption {
3030
return func(r *requestconfig.RequestConfig) error {
3131
r.Request.Header.Set("api-version", value)
3232
return nil
3333
}
@@ -39,22 +39,22 @@
3939
if err != nil {
4040
log.Fatalf("failed to parse BaseURL: %s\n", err)
4141
}
42-
return func(r *requestconfig.RequestConfig) error {
42+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
4343
if u.Path != "" && !strings.HasSuffix(u.Path, "/") {
4444
u.Path += "/"
4545
}
4646
r.BaseURL = u
4747
return nil
48-
}
48+
})
4949
}
5050

5151
// WithHTTPClient returns a RequestOption that changes the underlying [http.Client] used to make this
5252
// request, which by default is [http.DefaultClient].
5353
func WithHTTPClient(client *http.Client) RequestOption {
54-
return func(r *requestconfig.RequestConfig) error {
54+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
5555
r.HTTPClient = client
5656
return nil
57-
}
57+
})
5858
}
5959

6060
// MiddlewareNext is a function which is called by a middleware to pass an HTTP request
@@ -69,10 +69,10 @@
6969
// WithMiddleware returns a RequestOption that applies the given middleware
7070
// to the requests made. Each middleware will execute in the order they were given.
7171
func WithMiddleware(middlewares ...Middleware) RequestOption {
72-
return func(r *requestconfig.RequestConfig) error {
72+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
7373
r.Middlewares = append(r.Middlewares, middlewares...)
7474
return nil
75-
}
75+
})
7676
}
7777

7878
// WithMaxRetries returns a RequestOption that sets the maximum number of retries that the client
@@ -84,76 +84,76 @@
8484
if retries < 0 {
8585
panic("option: cannot have fewer than 0 retries")
8686
}
87-
return func(r *requestconfig.RequestConfig) error {
87+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
8888
r.MaxRetries = retries
8989
return nil
90-
}
90+
})
9191
}
9292

9393
// WithHeader returns a RequestOption that sets the header value to the associated key. It overwrites
9494
// any value if there was one already present.
9595
func WithHeader(key, value string) RequestOption {
96-
return func(r *requestconfig.RequestConfig) error {
96+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
9797
r.Request.Header.Set(key, value)
9898
return nil
99-
}
99+
})
100100
}
101101

102102
// WithHeaderAdd returns a RequestOption that adds the header value to the associated key. It appends
103103
// onto any existing values.
104104
func WithHeaderAdd(key, value string) RequestOption {
105-
return func(r *requestconfig.RequestConfig) error {
105+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
106106
r.Request.Header.Add(key, value)
107107
return nil
108-
}
108+
})
109109
}
110110

111111
// WithHeaderDel returns a RequestOption that deletes the header value(s) associated with the given key.
112112
func WithHeaderDel(key string) RequestOption {
113-
return func(r *requestconfig.RequestConfig) error {
113+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
114114
r.Request.Header.Del(key)
115115
return nil
116-
}
116+
})
117117
}
118118

119119
// WithQuery returns a RequestOption that sets the query value to the associated key. It overwrites
120120
// any value if there was one already present.
121121
func WithQuery(key, value string) RequestOption {
122-
return func(r *requestconfig.RequestConfig) error {
122+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
123123
query := r.Request.URL.Query()
124124
query.Set(key, value)
125125
r.Request.URL.RawQuery = query.Encode()
126126
return nil
127-
}
127+
})
128128
}
129129

130130
// WithQueryAdd returns a RequestOption that adds the query value to the associated key. It appends
131131
// onto any existing values.
132132
func WithQueryAdd(key, value string) RequestOption {
133-
return func(r *requestconfig.RequestConfig) error {
133+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
134134
query := r.Request.URL.Query()
135135
query.Add(key, value)
136136
r.Request.URL.RawQuery = query.Encode()
137137
return nil
138-
}
138+
})
139139
}
140140

141141
// WithQueryDel returns a RequestOption that deletes the query value(s) associated with the key.
142142
func WithQueryDel(key string) RequestOption {
143-
return func(r *requestconfig.RequestConfig) error {
143+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
144144
query := r.Request.URL.Query()
145145
query.Del(key)
146146
r.Request.URL.RawQuery = query.Encode()
147147
return nil
148-
}
148+
})
149149
}
150150

151151
// WithJSONSet returns a RequestOption that sets the body's JSON value associated with the key.
152152
// The key accepts a string as defined by the [sjson format].
153153
//
154154
// [sjson format]: https://github.com/tidwall/sjson
155155
func WithJSONSet(key string, value interface{}) RequestOption {
156-
return func(r *requestconfig.RequestConfig) (err error) {
156+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) (err error) {
157157
if buffer, ok := r.Body.(*bytes.Buffer); ok {
158158
b := buffer.Bytes()
159159
b, err = sjson.SetBytes(b, key, value)
@@ -165,15 +165,15 @@
165165
}
166166

167167
return fmt.Errorf("cannot use WithJSONSet on a body that is not serialized as *bytes.Buffer")
168-
}
168+
})
169169
}
170170

171171
// WithJSONDel returns a RequestOption that deletes the body's JSON value associated with the key.
172172
// The key accepts a string as defined by the [sjson format].
173173
//
174174
// [sjson format]: https://github.com/tidwall/sjson
175175
func WithJSONDel(key string) RequestOption {
176-
return func(r *requestconfig.RequestConfig) (err error) {
176+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) (err error) {
177177
if buffer, ok := r.Body.(*bytes.Buffer); ok {
178178
b := buffer.Bytes()
179179
b, err = sjson.DeleteBytes(b, key)
@@ -185,32 +185,32 @@
185185
}
186186

187187
return fmt.Errorf("cannot use WithJSONDel on a body that is not serialized as *bytes.Buffer")
188-
}
188+
})
189189
}
190190

191191
// WithResponseBodyInto returns a RequestOption that overwrites the deserialization target with
192192
// the given destination. If provided, we don't deserialize into the default struct.
193193
func WithResponseBodyInto(dst any) RequestOption {
194-
return func(r *requestconfig.RequestConfig) error {
194+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
195195
r.ResponseBodyInto = dst
196196
return nil
197-
}
197+
})
198198
}
199199

200200
// WithResponseInto returns a RequestOption that copies the [*http.Response] into the given address.
201201
func WithResponseInto(dst **http.Response) RequestOption {
202-
return func(r *requestconfig.RequestConfig) error {
202+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
203203
r.ResponseInto = dst
204204
return nil
205-
}
205+
})
206206
}
207207

208208
// WithRequestBody returns a RequestOption that provides a custom serialized body with the given
209209
// content type.
210210
//
211211
// body accepts an io.Reader or raw []bytes.
212212
func WithRequestBody(contentType string, body any) RequestOption {
213-
return func(r *requestconfig.RequestConfig) error {
213+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
214214
if reader, ok := body.(io.Reader); ok {
215215
r.Body = reader
216216
return r.Apply(WithHeader("Content-Type", contentType))
@@ -222,17 +222,17 @@
222222
}
223223

224224
return fmt.Errorf("body must be a byte slice or implement io.Reader")
225-
}
225+
})
226226
}
227227

228228
// WithRequestTimeout returns a RequestOption that sets the timeout for
229229
// each request attempt. This should be smaller than the timeout defined in
230230
// the context, which spans all retries.
231231
func WithRequestTimeout(dur time.Duration) RequestOption {
232-
return func(r *requestconfig.RequestConfig) error {
232+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
233233
r.RequestTimeout = dur
234234
return nil
235-
}
235+
})
236236
}
237237

238238
// WithEnvironmentProduction returns a RequestOption that sets the current
@@ -244,32 +244,32 @@
244244

245245
// WithAPIToken returns a RequestOption that sets the client setting "api_token".
246246
func WithAPIToken(value string) RequestOption {
247-
return func(r *requestconfig.RequestConfig) error {
247+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
248248
r.APIToken = value
249249
return r.Apply(WithHeader("authorization", fmt.Sprintf("Bearer %s", r.APIToken)))
250-
}
250+
})
251251
}
252252

253253
// WithAPIKey returns a RequestOption that sets the client setting "api_key".
254254
func WithAPIKey(value string) RequestOption {
255-
return func(r *requestconfig.RequestConfig) error {
255+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
256256
r.APIKey = value
257257
return r.Apply(WithHeader("X-Auth-Key", r.APIKey))
258-
}
258+
})
259259
}
260260

261261
// WithAPIEmail returns a RequestOption that sets the client setting "api_email".
262262
func WithAPIEmail(value string) RequestOption {
263-
return func(r *requestconfig.RequestConfig) error {
263+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
264264
r.APIEmail = value
265265
return r.Apply(WithHeader("X-Auth-Email", r.APIEmail))
266-
}
266+
})
267267
}
268268

269269
// WithUserServiceKey returns a RequestOption that sets the client setting "user_service_key".
270270
func WithUserServiceKey(value string) RequestOption {
271-
return func(r *requestconfig.RequestConfig) error {
271+
return requestconfig.RequestOptionFunc(func(r *requestconfig.RequestConfig) error {
272272
r.UserServiceKey = value
273273
return r.Apply(WithHeader("X-Auth-User-Service-Key", r.UserServiceKey))
274-
}
274+
})
275275
}

0 commit comments

Comments
 (0)
Please sign in to comment.