When dealing with abstraction layers wrapping multiple different API calls it is very difficult to gain access to the body as each different response object as a different GetBodyAsync method returning a different type.
Modifications
Add a common IOperationResponse<TBody> interface, inherited from IOperationResponse, which includes the GetBodyAsync method. Include this interface on all response classes which include a body.
Results
Abstraction layers in consumers can use type checking to find the interface they want. This is particularly useful for error handling if the API in question returns all errors with an Error schema or similar. You can simply response as IOperationResponse<Error> to test and then get the Error object from the GetBodyAsync method.
Motivation
When dealing with abstraction layers wrapping multiple different API calls it is very difficult to gain access to the body as each different response object as a different
GetBodyAsync
method returning a different type.Modifications
Add a common
IOperationResponse<TBody>
interface, inherited fromIOperationResponse
, which includes theGetBodyAsync
method. Include this interface on all response classes which include a body.Results
Abstraction layers in consumers can use type checking to find the interface they want. This is particularly useful for error handling if the API in question returns all errors with an
Error
schema or similar. You can simplyresponse as IOperationResponse<Error>
to test and then get theError
object from theGetBodyAsync
method.