From 9131cd8328b96fdbf6d4cbaf2b49a800a20fe7d9 Mon Sep 17 00:00:00 2001 From: Erika Tharp Date: Fri, 1 Apr 2022 15:56:57 -0700 Subject: [PATCH] chore: Make header names that always need to be sent constants (#38) --- Momento/ControlGrpcManager.cs | 2 +- Momento/DataGrpcManager.cs | 2 +- Momento/HeaderInterceptor.cs | 19 +++++++++---------- MomentoIntegrationTest/CacheTest.cs | 5 ++++- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Momento/ControlGrpcManager.cs b/Momento/ControlGrpcManager.cs index f8333faa..183748b9 100644 --- a/Momento/ControlGrpcManager.cs +++ b/Momento/ControlGrpcManager.cs @@ -17,7 +17,7 @@ internal sealed class ControlGrpcManager : IDisposable public ControlGrpcManager(string authToken, string endpoint) { this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); - List
headers = new List
{ new Header(name: "Authorization", value: authToken), new Header(name: "Agent", value: version) }; + List
headers = new List
{ new Header(name: Header.AuthorizationKey, value: authToken), new Header(name: Header.AgentKey, value: version) }; CallInvoker invoker = channel.Intercept(new HeaderInterceptor(headers)); this.client = new ScsControl.ScsControlClient(invoker); } diff --git a/Momento/DataGrpcManager.cs b/Momento/DataGrpcManager.cs index 9b2f546b..d97c0e3d 100644 --- a/Momento/DataGrpcManager.cs +++ b/Momento/DataGrpcManager.cs @@ -18,7 +18,7 @@ internal sealed class DataGrpcManager : IDisposable internal DataGrpcManager(string authToken, string endpoint) { this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); - List
headers = new List
{ new Header(name: "Authorization", value: authToken) , new Header(name: "Agent", value: version)}; + List
headers = new List
{ new Header(name: Header.AuthorizationKey, value: authToken) , new Header(name: Header.AgentKey, value: version)}; CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers)); this.client = new Scs.ScsClient(invoker); } diff --git a/Momento/HeaderInterceptor.cs b/Momento/HeaderInterceptor.cs index e124cd65..94a63b0d 100644 --- a/Momento/HeaderInterceptor.cs +++ b/Momento/HeaderInterceptor.cs @@ -2,11 +2,15 @@ using Grpc.Core; using Grpc.Core.Interceptors; using System.Collections.Generic; +using System.Linq; namespace MomentoSdk { class Header { + public const string AuthorizationKey = "Authorization"; + public const string AgentKey = "Agent"; + public readonly List onceOnlyHeaders = new List{Header.AgentKey}; public string Name; public string Value; public Header(String name, String value) @@ -20,16 +24,11 @@ class HeaderInterceptor : Grpc.Core.Interceptors.Interceptor { private readonly List
headersToAddEveryTime = new List
{}; private readonly List
headersToAddOnce = new List
{}; - private volatile Boolean isUserAgentSent = false; + private volatile Boolean areOnlyOnceHeadersSent = false; public HeaderInterceptor(List
headers) { - foreach(Header header in headers) { - if (header.Name == "Authorization") { - this.headersToAddEveryTime.Add(new Header(name: header.Name, value: header.Value)); - } else { - this.headersToAddOnce.Add(new Header(name: header.Name, value: header.Value)); - } - } + this.headersToAddOnce = headers.Where(header => header.onceOnlyHeaders.Contains(header.Name)).ToList(); + this.headersToAddEveryTime = headers.Where(header => !header.onceOnlyHeaders.Contains(header.Name)).ToList(); } public override TResponse BlockingUnaryCall(TRequest request, ClientInterceptorContext context, BlockingUnaryCallContinuation continuation) @@ -76,12 +75,12 @@ private void AddCallerMetadata(ref ClientInterceptorContext { headers.Add(header.Name, header.Value); } - if (!isUserAgentSent) { + if (!areOnlyOnceHeadersSent) { foreach (Header header in this.headersToAddOnce) { headers.Add(header.Name, header.Value); } - isUserAgentSent = true; + areOnlyOnceHeadersSent = true; } } } diff --git a/MomentoIntegrationTest/CacheTest.cs b/MomentoIntegrationTest/CacheTest.cs index 4368df79..a46aa5c4 100644 --- a/MomentoIntegrationTest/CacheTest.cs +++ b/MomentoIntegrationTest/CacheTest.cs @@ -19,7 +19,10 @@ public CacheTest() { uint defaultTtlSeconds = 10; client = new SimpleCacheClient(authKey, defaultTtlSeconds); - client.CreateCache(cacheName); + try { + client.CreateCache(cacheName); + } catch (AlreadyExistsException) { + } } // Test cleanup