Skip to content

Commit

Permalink
feat: Code Cleanup (#11)
Browse files Browse the repository at this point in the history
* feat: Code Cleanup

1. Adjust visibility of classes
2. Follow naming conventions for method naming

* merge conflicts

* more merge conflicts

* fix build

* fix typo

* visibility

* formatting

* visbility
  • Loading branch information
gautamomento authored Nov 1, 2021
1 parent f495c08 commit 40dba3c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 57 deletions.
18 changes: 9 additions & 9 deletions Momento/JwtUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

namespace MomentoSdk
{
public class JwtUtils
{
class JwtUtils
{
/// <summary>
/// extracts the controlEndpoint and cacheEndpoint
/// from the jwt
/// </summary>
/// <param name="jwt"></param>
/// <returns></returns>
public static Claims decodeJwt(string jwt)
public static Claims DecodeJwt(string jwt)
{
IJsonSerializer serializer = new JsonNetSerializer();

IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
JwtDecoder decoder = new JWT.JwtDecoder(serializer, urlEncoder);
JwtDecoder decoder = new JwtDecoder(serializer, urlEncoder);
try
{
var decodedJwt = decoder.Decode(jwt);
Expand All @@ -31,17 +31,17 @@ public static Claims decodeJwt(string jwt)
}
}

public class Claims
class Claims
{
[JsonProperty(PropertyName = "cp", Required = Required.Always)]
public string controlEndpoint { get; set; }
public string ControlEndpoint { get; private set; }
[JsonProperty(PropertyName = "c", Required = Required.Always)]
public string cacheEndpoint { get; set; }
public string CacheEndpoint { get; private set; }

public Claims(string cacheEndpoint, string controlEndpoint)
{
this.cacheEndpoint = cacheEndpoint;
this.controlEndpoint = controlEndpoint;
this.CacheEndpoint = cacheEndpoint;
this.ControlEndpoint = controlEndpoint;
}
}
}
16 changes: 7 additions & 9 deletions Momento/Momento.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using ControlClient;
using CacheClient;
using static CacheClient.Scs;
using static ControlClient.ScsControl;
using Grpc.Core;
using Grpc.Core.Interceptors;
Expand All @@ -24,13 +22,13 @@ public class Momento : IDisposable
/// <param name="authToken">Momento jwt</param>
public Momento(string authToken)
{
Claims claims = JwtUtils.decodeJwt(authToken);
this.channel = GrpcChannel.ForAddress("https://" + claims.controlEndpoint + ":443", new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
Claims claims = JwtUtils.DecodeJwt(authToken);
this.channel = GrpcChannel.ForAddress("https://" + claims.ControlEndpoint + ":443", new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
Header[] headers = { new Header(name: "Authorization", value: authToken) };
CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers));
this.client = new ScsControlClient(invoker);
this.authToken = authToken;
this.cacheEndpoint = "https://" + claims.cacheEndpoint + ":443";
this.cacheEndpoint = "https://" + claims.CacheEndpoint + ":443";
}

/// <summary>
Expand Down Expand Up @@ -62,9 +60,9 @@ public Responses.CreateCacheResponse CreateCache (String cacheName)
try
{
CreateCacheRequest request = new CreateCacheRequest() { CacheName = cacheName };
this.client.CreateCache(request);
client.CreateCache(request);
}
catch (Grpc.Core.RpcException e)
catch (RpcException e)
{
if (e.StatusCode == StatusCode.AlreadyExists)
{
Expand All @@ -89,7 +87,7 @@ public Responses.CreateCacheResponse CreateCache (String cacheName)
public MomentoCache GetCache(String cacheName, uint defaultTtlSeconds)
{
CheckValidCacheName(cacheName);
return MomentoCache.Init(this.authToken, cacheName, this.cacheEndpoint, defaultTtlSeconds);
return MomentoCache.Init(authToken, cacheName, cacheEndpoint, defaultTtlSeconds);
}

/// <summary>
Expand All @@ -102,7 +100,7 @@ public Responses.DeleteCacheResponse DeleteCache(String cacheName)
DeleteCacheRequest request = new DeleteCacheRequest() { CacheName = cacheName };
try
{
this.client.DeleteCache(request);
client.DeleteCache(request);
return new Responses.DeleteCacheResponse();
} catch(Exception e)
{
Expand Down
8 changes: 4 additions & 4 deletions Momento/Responses/CacheGetResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace MomentoSdk.Responses
{
public class CacheGetResponse : BaseCacheResponse
{
public MomentoCacheResult result { get; private set; }
public MomentoCacheResult Result { get; private set; }
private readonly ByteString body;

public CacheGetResponse(GetResponse response)
{
body = response.CacheBody;
result = this.ResultMapper(response.Result);
Result = this.ResultMapper(response.Result);
}

public String String()
Expand All @@ -22,7 +22,7 @@ public String String()

public String String(Encoding encoding)
{
if (result == MomentoCacheResult.Hit)
if (Result == MomentoCacheResult.Hit)
{
return body.ToString(encoding);
}
Expand All @@ -31,7 +31,7 @@ public String String(Encoding encoding)

public byte[] Bytes()
{
if (result == MomentoCacheResult.Hit)
if (Result == MomentoCacheResult.Hit)
{
return this.body.ToByteArray();
}
Expand Down
4 changes: 2 additions & 2 deletions Momento/Responses/CacheSetResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace MomentoSdk.Responses
{
public class CacheSetResponse : BaseCacheResponse
{
public MomentoCacheResult result { get; private set; }
public MomentoCacheResult Result { get; private set; }

public CacheSetResponse(SetResponse response)
{
result = this.ResultMapper(response.Result);
Result = this.ResultMapper(response.Result);
}
}
}
6 changes: 3 additions & 3 deletions MomentoIntegrationTest/CacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async void HappyPathExpiredTtl()
cache.Set(cacheKey, cacheValue, 1);
await Task.Delay(1100);
CacheGetResponse result = cache.Get(cacheKey);
Assert.Equal(MomentoCacheResult.Miss, result.result);
Assert.Equal(MomentoCacheResult.Miss, result.Result);
}

[Fact]
Expand All @@ -48,7 +48,7 @@ public async void HappyPathAsync()
MomentoCache cache = momento.CreateOrGetCache(cacheName, defaultTtlSeconds);
await cache.SetAsync(cacheKey, cacheValue, defaultTtlSeconds);
CacheGetResponse result = await cache.GetAsync(cacheKey);
Assert.Equal(MomentoCacheResult.Hit, result.result);
Assert.Equal(MomentoCacheResult.Hit, result.Result);
Assert.Equal(cacheValue, result.String());
}

Expand All @@ -59,7 +59,7 @@ public void HappyPathMiss()
Momento momento = new Momento(authKey);
MomentoCache cache = momento.CreateOrGetCache(cacheName, defaultTtlSeconds);
CacheGetResponse result = cache.Get(Guid.NewGuid().ToString());
Assert.Equal(MomentoCacheResult.Miss, result.result);
Assert.Equal(MomentoCacheResult.Miss, result.Result);
Assert.Null(result.String());
Assert.Null(result.Bytes());
}
Expand Down
9 changes: 8 additions & 1 deletion MomentoIntegrationTest/MomentoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ namespace MomentoIntegrationTest
public class MomentoTest
{
private String authKey = Environment.GetEnvironmentVariable("TEST_AUTH_TOKEN");
private String cacheName = Environment.GetEnvironmentVariable("TEST_CACHE_NAME");

[Fact]
public void DeleteCacheThatDoesntExist()
{
Momento momento = new Momento(authKey);
Assert.Throws<CacheNotFoundException>(() => momento.DeleteCache("non existant cache"));
}


[Fact]
public void InvalidJwtException()
{
Assert.Throws<InvalidJwtException>( () => new Momento("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpbnRlZ3JhdGlvbiJ9.ZOgkTs"));
}
}
}
26 changes: 0 additions & 26 deletions MomentoTest/JwtUtilsTest.cs

This file was deleted.

6 changes: 3 additions & 3 deletions MomentoTest/Responses/CacheGetResponseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ public void CorrectResultMapping()
ByteString body = ByteString.CopyFromUtf8(cacheBody);
GetResponse serverResponseHit = new GetResponse() { CacheBody = body, Result = ECacheResult.Hit };
CacheGetResponse responseHit = new CacheGetResponse(serverResponseHit);
Assert.Equal(MomentoCacheResult.Hit, responseHit.result);
Assert.Equal(MomentoCacheResult.Hit, responseHit.Result);
Assert.Equal(cacheBody, responseHit.String());

GetResponse serverResponseMiss = new GetResponse() { Result = ECacheResult.Miss };
CacheGetResponse responseMiss = new CacheGetResponse(serverResponseMiss);
Assert.Equal(MomentoCacheResult.Miss, responseMiss.result);
Assert.Equal(MomentoCacheResult.Miss, responseMiss.Result);

GetResponse serverResponseBadRequest = new GetResponse() { Result = ECacheResult.BadRequest };
CacheGetResponse responseBadRequest = new CacheGetResponse(serverResponseBadRequest);
Assert.Equal(MomentoCacheResult.Unknown, responseBadRequest.result);
Assert.Equal(MomentoCacheResult.Unknown, responseBadRequest.Result);
}
}
}

0 comments on commit 40dba3c

Please sign in to comment.