Skip to content

Commit

Permalink
chore: DRY up middleware request wrapping via extension fn
Browse files Browse the repository at this point in the history
  • Loading branch information
cprice404 committed Oct 7, 2022
1 parent 3e10ed2 commit a88d690
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 69 deletions.
30 changes: 3 additions & 27 deletions src/Momento.Sdk/Internal/ControlGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,21 @@ public ControlClientWithMiddleware(ScsControl.ScsControlClient generatedClient,

public async Task<_CreateCacheResponse> CreateCacheAsync(_CreateCacheRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.CreateCacheAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.CreateCacheAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_DeleteCacheResponse> DeleteCacheAsync(_DeleteCacheRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DeleteCacheAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DeleteCacheAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListCachesResponse> ListCachesAsync(_ListCachesRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListCachesAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListCachesAsync(r, o));
return await wrapped.ResponseAsync;
}

private async Task<MiddlewareResponseState<TResponse>> WrapWithMiddleware<TRequest, TResponse>(
TRequest request,
CallOptions callOptions,
Func<TRequest, CallOptions, AsyncUnaryCall<TResponse>> continuation
)
{
Func<TRequest, CallOptions, Task<MiddlewareResponseState<TResponse>>> continuationWithMiddlewareResponseState = (r, o) =>
{
var result = continuation(r, o);
return Task.FromResult(new MiddlewareResponseState<TResponse>(
ResponseAsync: result.ResponseAsync,
ResponseHeadersAsync: result.ResponseHeadersAsync,
GetStatus: result.GetStatus,
GetTrailers: result.GetTrailers
));
};

var wrapped = _middlewares.Aggregate(continuationWithMiddlewareResponseState, (acc, middleware) =>
{
return (r, o) => middleware.WrapRequest(r, o, acc);
});
return await wrapped(request, callOptions);
}
}

internal sealed class ControlGrpcManager : IDisposable
Expand Down
60 changes: 18 additions & 42 deletions src/Momento.Sdk/Internal/DataGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,135 +60,111 @@ public DataClientWithMiddleware(Scs.ScsClient generatedClient, IList<IMiddleware

public async Task<_DeleteResponse> DeleteAsync(_DeleteRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DeleteAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DeleteAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_GetResponse> GetAsync(_GetRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.GetAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.GetAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_SetResponse> SetAsync(_SetRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.SetAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.SetAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_DictionarySetResponse> DictionarySetAsync(_DictionarySetRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DictionarySetAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DictionarySetAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_DictionaryIncrementResponse> DictionaryIncrementAsync(_DictionaryIncrementRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DictionaryIncrementAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DictionaryIncrementAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_DictionaryGetResponse> DictionaryGetAsync(_DictionaryGetRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DictionaryGetAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DictionaryGetAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_DictionaryFetchResponse> DictionaryFetchAsync(_DictionaryFetchRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DictionaryFetchAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DictionaryFetchAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_DictionaryDeleteResponse> DictionaryDeleteAsync(_DictionaryDeleteRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.DictionaryDeleteAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.DictionaryDeleteAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_SetUnionResponse> SetUnionAsync(_SetUnionRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.SetUnionAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.SetUnionAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_SetDifferenceResponse> SetDifferenceAsync(_SetDifferenceRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.SetDifferenceAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.SetDifferenceAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_SetFetchResponse> SetFetchAsync(_SetFetchRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.SetFetchAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.SetFetchAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListPushFrontResponse> ListPushFrontAsync(_ListPushFrontRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListPushFrontAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListPushFrontAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListPushBackResponse> ListPushBackAsync(_ListPushBackRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListPushBackAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListPushBackAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListPopFrontResponse> ListPopFrontAsync(_ListPopFrontRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListPopFrontAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListPopFrontAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListPopBackResponse> ListPopBackAsync(_ListPopBackRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListPopBackAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListPopBackAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListFetchResponse> ListFetchAsync(_ListFetchRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListFetchAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListFetchAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListRemoveResponse> ListRemoveAsync(_ListRemoveRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListRemoveAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListRemoveAsync(r, o));
return await wrapped.ResponseAsync;
}

public async Task<_ListLengthResponse> ListLengthAsync(_ListLengthRequest request, CallOptions callOptions)
{
var wrapped = await WrapWithMiddleware(request, callOptions, (r, o) => _generatedClient.ListLengthAsync(r, o));
var wrapped = await _middlewares.WrapRequest(request, callOptions, (r, o) => _generatedClient.ListLengthAsync(r, o));
return await wrapped.ResponseAsync;
}

private async Task<MiddlewareResponseState<TResponse>> WrapWithMiddleware<TRequest, TResponse>(
TRequest request,
CallOptions callOptions,
Func<TRequest, CallOptions, AsyncUnaryCall<TResponse>> continuation
)
{
Func<TRequest, CallOptions, Task<MiddlewareResponseState<TResponse>>> continuationWithMiddlewareResponseState = (r, o) =>
{
var result = continuation(r, o);
return Task.FromResult(new MiddlewareResponseState<TResponse>(
ResponseAsync: result.ResponseAsync,
ResponseHeadersAsync: result.ResponseHeadersAsync,
GetStatus: result.GetStatus,
GetTrailers: result.GetTrailers
));
};

var wrapped = _middlewares.Aggregate(continuationWithMiddlewareResponseState, (acc, middleware) =>
{
return (r, o) => middleware.WrapRequest(r, o, acc);
});
return await wrapped(request, callOptions);
}
}

public class DataGrpcManager : IDisposable
Expand Down

0 comments on commit a88d690

Please sign in to comment.