Skip to content

Support passing a context for cancellation #12

@abdusco

Description

@abdusco

Currently, there is no way to pass a context.Context to be used when sending requests.

Proper context.Context propagation is essential for allowing applications to cancel outbound API calls or enforce deadlines. It is a strong convention in the Go ecosystem for functions that perform I/O operations or may block for an extended period to accept a context.Context as their first argument.

I would file a PR to bring this enhancement, but it looks like the files are generated. The code generator needs to be adjusted.

A valid approach could be adding an ExecContext method

// kept for backwards-compatibility
func (t *CreateTrackingRequest) Execute() (*model.CreateTrackingResponse, error) {...}

// new
func (t *CreateTrackingRequest) ExecuteContext(ctx context.Context) (*model.CreateTrackingResponse, error) {
	req, err := t.build()
	if err != nil {...}
+	req = req.WithContext(ctx)
	var data model.CreateTrackingResponse
	return &data, t.sender.Do(req, &data)
}

or another method on the request builder for attaching context:

client, _ := tracking.New()
req := client.Tracking.
	CreateTracking().
+	WithContext(ctx).
	BuildBody(params)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions