Skip to content

Commit 0458951

Browse files
refactoring api key parser
1 parent fdc5500 commit 0458951

File tree

6 files changed

+12
-92
lines changed

6 files changed

+12
-92
lines changed

src/SocketLabs/InjectionApi/Core/Enum/ApiKeyParseResultCode.cs renamed to src/SocketLabs/InjectionApi/Core/ApiKeyParseResult.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
5-
namespace SocketLabs.InjectionApi.Core.Enum
1+
namespace SocketLabs.InjectionApi.Core
62
{
73
/// <summary>
84
/// A code describing the result of an attempt to parse an Api key.
95
/// </summary>
10-
public enum ApiKeyParseResultCode
6+
public enum ApiKeyParseResult
117
{
128

139
/// <summary>
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using SocketLabs.InjectionApi.Core.Enum;
5-
using SocketLabs.InjectionApi.Core.Models;
62

73
namespace SocketLabs.InjectionApi.Core
84
{
@@ -18,37 +14,21 @@ public class ApiKeyParser : IApiKeyParser
1814
/// <returns>An ApiKeyParseResult with the api key data and result code from the parse.</returns>
1915
public ApiKeyParseResult Parse(string wholeApiKey)
2016
{
21-
var key = new ApiKey { IsValidFormat = false };
2217
if (string.IsNullOrWhiteSpace(wholeApiKey))
23-
return new ApiKeyParseResult { ApiKey = null, ResultCode = ApiKeyParseResultCode.InvalidEmptyOrWhitespace };
24-
25-
//if (!wholeApiKey.StartsWith("SL.", StringComparison.InvariantCulture))
26-
// return new ApiKeyParseResult {ApiKey = null, ResultCode = ApiKeyParseResultCode.InvalidMissingProperPrefix};
27-
18+
return ApiKeyParseResult.InvalidEmptyOrWhitespace;
19+
2820
//extract public part
2921
var maxCount = Math.Min(50, wholeApiKey.Length);
3022
var publicPartEnd = wholeApiKey.IndexOf('.', startIndex: 0, maxCount); //don't check more than 50 chars
3123
if (publicPartEnd == -1)
32-
return new ApiKeyParseResult { ApiKey = null, ResultCode = ApiKeyParseResultCode.InvalidUnableToExtractPublicPart };
33-
var publicPart = wholeApiKey.Substring(0, publicPartEnd);
34-
35-
24+
return ApiKeyParseResult.InvalidUnableToExtractPublicPart;
25+
3626
//now extract the private part
3727
if (wholeApiKey.Length <= publicPartEnd + 1)
38-
return new ApiKeyParseResult { ApiKey = null, ResultCode = ApiKeyParseResultCode.InvalidUnableToExtractSecretPart };
39-
var privatePart = wholeApiKey.Substring(publicPartEnd + 1);
28+
return ApiKeyParseResult.InvalidUnableToExtractSecretPart;
4029

4130
//success
42-
return new ApiKeyParseResult
43-
{
44-
ApiKey = new ApiKey
45-
{
46-
PublicPart = publicPart,
47-
PrivatePart = privatePart,
48-
IsValidFormat = true,
49-
},
50-
ResultCode = ApiKeyParseResultCode.Success
51-
};
31+
return ApiKeyParseResult.Success;
5232
}
5333
}
5434
}

src/SocketLabs/InjectionApi/Core/IApiKeyParser.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using SocketLabs.InjectionApi.Core.Enum;
2-
using SocketLabs.InjectionApi.Core.Models;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
1+

62

73
namespace SocketLabs.InjectionApi.Core
84
{

src/SocketLabs/InjectionApi/Core/Models/ApiKey.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/SocketLabs/InjectionApi/Core/Models/ApiKeyParseResult.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/SocketLabs/InjectionApi/SocketLabsClient.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using SocketLabs.InjectionApi.Core;
10-
using SocketLabs.InjectionApi.Core.Enum;
1110
using SocketLabs.InjectionApi.Message;
1211

1312
namespace SocketLabs.InjectionApi
@@ -234,7 +233,7 @@ public async Task<SendResponse> SendAsync(IBasicMessage message, CancellationTok
234233
var apiKeyParser = new ApiKeyParser();
235234
var parseResult = apiKeyParser.Parse(_apiKey);
236235

237-
if (parseResult.ResultCode == ApiKeyParseResultCode.Success)
236+
if (parseResult == ApiKeyParseResult.Success)
238237
{
239238
_httpClient.DefaultRequestHeaders.Authorization =
240239
new AuthenticationHeaderValue("Bearer", _apiKey);
@@ -293,12 +292,11 @@ public async Task<SendResponse> SendAsync(IBulkMessage message, CancellationToke
293292

294293
validationResult = validator.ValidateMessage(message);
295294
if (validationResult.Result != SendResult.Success) return validationResult;
296-
297-
295+
298296
var apiKeyParser = new ApiKeyParser();
299297
var parseResult = apiKeyParser.Parse(_apiKey);
300298

301-
if (parseResult.ResultCode == ApiKeyParseResultCode.Success)
299+
if (parseResult == ApiKeyParseResult.Success)
302300
{
303301
_httpClient.DefaultRequestHeaders.Authorization =
304302
new AuthenticationHeaderValue("Bearer", _apiKey);

0 commit comments

Comments
 (0)