Skip to content

Extract shared _dispatch helper for annotation service _send_* methods #14

Description

@craigmcchesney

The annotation-service feature clients now contain many near-identical _send_* methods that differ only in the stub method called, the success oneof field name, the result class, and the operation name used in log/error messages. PvMetadataClient has 4; MachineConfigClient has 9. Once #6 (annotations) lands there will be ~14+ of these across the annotation clients.

Each body is the same three-tier error-handling shape:

try:
    response = self._stub.<op>(request)
    if response.HasField('exceptionalResult'):
        return <ResultCls>(is_error=True, message=response.exceptionalResult.message)
    elif response.HasField('<successField>'):
        return <ResultCls>(is_error=False, message="", response=response)
    else:
        return <ResultCls>(is_error=True, message="Unexpected response format: ...")
except grpc.RpcError as e:
    return <ResultCls>(is_error=True, message=f"gRPC error: {e.details()}")
except Exception as e:
    return <ResultCls>(is_error=True, message=f"Unexpected error: {e}")

Proposal: extract a shared helper (e.g. on ServiceApiClientBase) roughly:

def _dispatch(self, stub_call, request, result_cls, success_field, op_name): ...

that runs the RPC, applies the three-tier handling, and returns the built result object. Each _send_* collapses to a one-line delegation.

Why not now: during the #9 code review (PR #12) this was deliberately deferred — refactoring in that PR would have made it inconsistent with the existing house style in IngestionClient/PvMetadataClient, and the full extent of the duplication isn't visible until the third annotation feature client (#6) exists. This issue tracks doing it as a dedicated cleanup once #6 is merged.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions