Skip to content

Commit 7dba3f8

Browse files
fredbiclaude
andcommitted
refactor(client): promote ContextualTransport to runtime package
Move ContextualTransport from client/ to the runtime root package so it sits side-by-side with ClientTransport, its canonical home. Update WithOpenTelemetry/WithOpenTracing return types and the submitWrapped type assertion to use the qualified name. Mark the cached Context fields on runtime.ClientOperation and client.Runtime as Deprecated, redirecting callers to ContextualTransport.SubmitContext. The internal ensureContext fallback keeps reading the legacy field under an explicit //nolint:staticcheck. Refresh stale commented signatures on ClientTransport.Submit and ContextualTransport.SubmitContext with short modern docstrings. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 1002219 commit 7dba3f8

3 files changed

Lines changed: 31 additions & 26 deletions

File tree

‎client/opentelemetry.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ const (
2929
// A new client span is created for each request.
3030
// The provided opts are applied to each spans - for example to add global tags.
3131
//
32-
// The returned transport satisfies [ContextualTransport]: callers
32+
// The returned transport satisfies [runtime.ContextualTransport]: callers
3333
// should prefer [openTelemetryTransport.SubmitContext] over the
3434
// legacy [runtime.ClientOperation.Context] field. Setting that
3535
// field is still honored on the [openTelemetryTransport.Submit]
3636
// compatibility path.
37-
func (r *Runtime) WithOpenTelemetry(opts ...OpenTelemetryOpt) ContextualTransport {
37+
func (r *Runtime) WithOpenTelemetry(opts ...OpenTelemetryOpt) runtime.ContextualTransport {
3838
return newOpenTelemetryTransport(r, r.Host, opts)
3939
}
4040

@@ -58,7 +58,7 @@ func (r *Runtime) WithOpenTelemetry(opts ...OpenTelemetryOpt) ContextualTranspor
5858
// usual opentracing options and opentracing-enabled transport.
5959
//
6060
// Passed options are ignored unless they are of type [OpenTelemetryOpt].
61-
func (r *Runtime) WithOpenTracing(opts ...any) ContextualTransport {
61+
func (r *Runtime) WithOpenTracing(opts ...any) runtime.ContextualTransport {
6262
otelOpts := make([]OpenTelemetryOpt, 0, len(opts))
6363
for _, o := range opts {
6464
otelOpt, ok := o.(OpenTelemetryOpt)
@@ -179,7 +179,7 @@ func (t *openTelemetryTransport) Submit(op *runtime.ClientOperation) (any, error
179179
// transport's SubmitContext call. The legacy
180180
// [runtime.ClientOperation.Context] field is not consulted.
181181
//
182-
// When the wrapped transport implements [ContextualTransport], ctx is
182+
// When the wrapped transport implements [runtime.ContextualTransport], ctx is
183183
// forwarded directly via its SubmitContext. Otherwise, the legacy
184184
// Submit path is used: ctx is stamped onto op.Context for the
185185
// duration of that call and restored afterwards, so the wrapped
@@ -228,7 +228,7 @@ func (t *openTelemetryTransport) SubmitContext(ctx context.Context, op *runtime.
228228

229229
//nolint:contextcheck // ctx is forwarded verbatim; the legacy Submit branch only stamps it onto op.Context for the wrapped transport.
230230
func (t *openTelemetryTransport) submitWrapped(ctx context.Context, op *runtime.ClientOperation) (any, error) {
231-
if sc, ok := t.transport.(ContextualTransport); ok {
231+
if sc, ok := t.transport.(runtime.ContextualTransport); ok {
232232
return sc.SubmitContext(ctx, op)
233233
}
234234
prev := op.Context

‎client/runtime.go‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ type Runtime struct {
4444
Host string
4545
BasePath string
4646
Formats strfmt.Registry
47-
Context context.Context //nolint:containedctx // we precisely want this type to contain the request context
47+
// Deprecated: prefer [runtime.ContextualTransport.SubmitContext] to pass the request context explicitly.
48+
Context context.Context //nolint:containedctx // we precisely want this type to contain the request context
4849

4950
Debug bool
5051

@@ -85,6 +86,8 @@ type Runtime struct {
8586
response ClientResponseFunc
8687
}
8788

89+
var _ runtime.ContextualTransport = &Runtime{}
90+
8891
// New creates a new default runtime for a swagger api runtime.Client.
8992
func New(host, basePath string, schemes []string) *Runtime {
9093
var rt Runtime
@@ -188,22 +191,6 @@ func (r *Runtime) CreateHttpRequest(operation *runtime.ClientOperation) (req *ht
188191
return
189192
}
190193

191-
// ContextualTransport extends [runtime.ClientTransport] with an
192-
// explicit context-aware submission method.
193-
//
194-
// [Runtime] satisfies it via [Runtime.SubmitContext]. Wrappers such
195-
// as the OpenTelemetry transport type-assert to this interface so
196-
// they can forward an explicit context to the underlying transport
197-
// without setting the cached [runtime.ClientOperation.Context] field.
198-
//
199-
// In v2, SubmitContext will be folded into [runtime.ClientTransport]
200-
// itself and the cached context field removed; this interface is the
201-
// v0.x bridge.
202-
type ContextualTransport interface {
203-
runtime.ClientTransport
204-
SubmitContext(context.Context, *runtime.ClientOperation) (any, error)
205-
}
206-
207194
// Submit a request and when there is a body on success it will turn that into the result
208195
// all other things are turned into an api error for swagger which retains the status code.
209196
//
@@ -309,7 +296,7 @@ func (r *Runtime) SetResponseReader(f ClientResponseFunc) {
309296

310297
func (r *Runtime) ensureContext(operation *runtime.ClientOperation) context.Context {
311298
switch {
312-
case operation.Context != nil:
299+
case operation.Context != nil: //nolint:staticcheck // kept for backward compatibility
313300
return operation.Context
314301
case r.Context != nil:
315302
return r.Context

‎client_operation.go‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,30 @@ type ClientOperation struct {
1919
AuthInfo ClientAuthInfoWriter
2020
Params ClientRequestWriter
2121
Reader ClientResponseReader
22-
Context context.Context //nolint:containedctx // we precisely want this type to contain the request context
23-
Client *http.Client
22+
// Deprecated: prefer [ContextualTransport.SubmitContext] to pass the request context explicitly.
23+
Context context.Context //nolint:containedctx // we precisely want this type to contain the request context
24+
Client *http.Client
2425
}
2526

2627
// A ClientTransport implementor knows how to submit Request objects to some destination.
2728
type ClientTransport interface {
28-
// Submit(string, RequestWriter, ResponseReader, AuthInfoWriter) (interface{}, error)
29+
// Submit the operation and return the deserialized response or an error.
2930
Submit(*ClientOperation) (any, error)
3031
}
32+
33+
// ContextualTransport extends [ClientTransport] with an explicit
34+
// context-aware submission method.
35+
//
36+
// Wrappers such as the OpenTelemetry transport type-assert to this
37+
// interface so they can forward an explicit context to the underlying
38+
// transport without setting the cached [ClientOperation.Context] field.
39+
//
40+
// In v2, SubmitContext will be folded into [ClientTransport] itself
41+
// and the cached [ClientOperation.Context] field removed; this interface
42+
// is the v0.x bridge.
43+
type ContextualTransport interface {
44+
ClientTransport
45+
46+
// SubmitContext submits the operation using ctx as the request context.
47+
SubmitContext(ctx context.Context, operation *ClientOperation) (any, error)
48+
}

0 commit comments

Comments
 (0)