-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
* Adjusted Project's Structure and Exposed AuthenticationApi * Adding SampleApi for Testing Purposes - to be removed * Created a boilerplate to be reviewed * Selectors and Builders Structure in progress. Expression parsing method almost done. * Expression parsing done getting customer info is in progress * Done with customer builder and selector code needs to be cleaned and structured properly * adjusted code structure and refactored some parts * This branch has a sample API and some tests for the selector * back up * removing sampleApi and Tests * removing SampleApi and Tests * removed logger dependency * Adding multiple dotnet target frameworks * Adding Multiple Target Frameworks * removing unnecessary files * removing unnecessary files * cleaning the code in progress * changing constructors to become internal * removed nuget.exe * adding .net5.0
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using SubsBase.SDK.Authentication.Service; | ||
using SubsBase.SDK.Common.Contracts; | ||
|
||
namespace SubsBase.SDK.Authentication.Client; | ||
|
||
public class AuthenticationClient | ||
{ | ||
private readonly AuthenticationService _authService; | ||
private readonly SubsBaseSdkOptions _options; | ||
|
||
private static string _token; | ||
private static DateTime _lastTokenRequest; | ||
|
||
public Task<string> ServerToken => GetServerTokenAsync(); | ||
|
||
internal AuthenticationClient(AuthenticationService auth, SubsBaseSdkOptions options) | ||
{ | ||
_authService = auth; | ||
_options = options; | ||
} | ||
|
||
private async Task<string> GetServerTokenAsync() | ||
{ | ||
if (_lastTokenRequest > DateTime.Now.AddHours(-4) && _lastTokenRequest <= DateTime.Now) | ||
{ | ||
return _token; | ||
} | ||
|
||
_token = await _authService.GenerateTokenAsync(_options.SiteId, _options.ApiSecret); | ||
|
||
if (_token != string.Empty) | ||
{ | ||
// only update the timestamp if the request was successful | ||
_lastTokenRequest = DateTime.Now; | ||
} | ||
|
||
return _token; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace SubsBase.SDK.Authentication; | ||
|
||
public static class Constants | ||
{ | ||
public const string AuthQuery = @"query GetApiToken($siteId: String!, $apiSecret: String!){ getApiToken(siteId: $siteId, apiSecret: $apiSecret) { isSuccess, value, message} }"; | ||
public const string AuthEndpoint = "https://api.subsbase.xyz/auth"; | ||
public const string AuthOperationName = "GetApiToken"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SubsBase.SDK.Authentication.Contracts; | ||
|
||
public class AuthResponse | ||
{ | ||
public class ApiTokenBody | ||
{ | ||
[JsonProperty("isSuccess")] | ||
public bool IsSuccess { get; set; } | ||
|
||
[JsonProperty("value")] | ||
public string Value { get; set; } | ||
} | ||
|
||
[JsonProperty("getApiToken")] | ||
public ApiTokenBody GetApiToken { get; set; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SubsBase.SDK.Authentication.Client; | ||
using SubsBase.SDK.Authentication.Service; | ||
|
||
namespace SubsBase.SDK.Authentication; | ||
|
||
public static class Install | ||
{ | ||
public static IServiceCollection AddAuthenticationSdk(this IServiceCollection services) | ||
{ | ||
services.AddScoped<AuthenticationClient>(); | ||
services.AddTransient<AuthenticationService>(); | ||
|
||
return services; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Newtonsoft.Json; | ||
using SubsBase.SDK.Authentication.Contracts; | ||
using SubsBase.SDK.Common.Clients; | ||
|
||
namespace SubsBase.SDK.Authentication.Service; | ||
|
||
public class AuthenticationService | ||
{ | ||
private readonly GraphQlClient _graphQlClient; | ||
|
||
internal AuthenticationService(GraphQlClient graphQlClient) | ||
{ | ||
_graphQlClient = graphQlClient; | ||
} | ||
|
||
public async Task<string> GenerateTokenAsync(string siteId, string apiSecret) | ||
{ | ||
var variables = new | ||
{ | ||
siteId = siteId, | ||
apiSecret = apiSecret | ||
}; | ||
|
||
var response = await _graphQlClient.SendAsync(Constants.AuthEndpoint, Constants.AuthQuery, variables, | ||
Constants.AuthOperationName, string.Empty); | ||
|
||
if (response == null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var deserializedResponse = JsonConvert.DeserializeObject<AuthResponse>(response.Data.ToString()); | ||
return deserializedResponse?.GetApiToken.Value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net462;net5.0;net6.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>10</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SubsBase.SDK.Common\SubsBase.SDK.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using GraphQL; | ||
using GraphQL.Client.Http; | ||
using GraphQL.Client.Serializer.Newtonsoft; | ||
using Newtonsoft.Json.Linq; | ||
using SubsBase.SDK.Common.Contracts; | ||
|
||
namespace SubsBase.SDK.Common.Clients; | ||
|
||
public class GraphQlClient | ||
{ | ||
private readonly SubsBaseSdkOptions _options; | ||
private GraphQLHttpClient _client; | ||
|
||
internal GraphQlClient(SubsBaseSdkOptions options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public async Task<GraphQLResponse<JObject>?> SendAsync(string endpointUrl, | ||
string query, | ||
object variables, | ||
string operationName, | ||
string token) | ||
{ | ||
_client = new GraphQLHttpClient(endpointUrl, new NewtonsoftJsonSerializer()); | ||
|
||
SetRequestHeaders(token); | ||
|
||
var request = new GraphQLRequest() | ||
{ | ||
Query = query, | ||
Variables = variables, | ||
OperationName = operationName | ||
}; | ||
|
||
try | ||
{ | ||
var response = await _client.SendQueryAsync<JObject>(request); | ||
return response; | ||
} | ||
catch (Exception e) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private void SetRequestHeaders(string token) | ||
{ | ||
_client.HttpClient.DefaultRequestHeaders.Add("x-site-id", _options.SiteId); | ||
_client.HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace SubsBase.SDK.Common.Contracts; | ||
|
||
public class Result<T> | ||
{ | ||
public Result(T value) | ||
{ | ||
IsSuccess = value != null; | ||
Value = value; | ||
} | ||
|
||
public T? Value { get; } | ||
|
||
public bool IsSuccess { get; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SubsBase.SDK.Common.Contracts; | ||
|
||
public class SubsBaseSdkOptions | ||
{ | ||
public string SiteId; | ||
public string ApiSecret; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SubsBase.SDK.Common.Clients; | ||
using SubsBase.SDK.Common.Contracts; | ||
|
||
namespace SubsBase.SDK.Common; | ||
|
||
public static class Install | ||
{ | ||
public static IServiceCollection AddCommonSdk(this IServiceCollection services, | ||
Action<SubsBaseSdkOptions> sdkOptions) | ||
{ | ||
var options = new SubsBaseSdkOptions(); | ||
sdkOptions(options); | ||
|
||
services.AddScoped<GraphQlClient>(); | ||
services.AddSingleton<SubsBaseSdkOptions>(options); | ||
|
||
return services; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace SubsBase.SDK.Common.Selectors; | ||
|
||
public interface IFieldSelector<TModel, out TSelector> | ||
{ | ||
TSelector Select<T>(Expression<Func<TModel, T>> selector); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net462;net5.0;net6.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>10</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="GraphQL.Client" Version="5.1.0"/> | ||
<PackageReference Include="GraphQL.Client.Serializer.Newtonsoft" Version="5.1.0"/> | ||
<PackageReference Include="IndexRange" Version="1.0.2"/> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2"/> | ||
</ItemGroup> | ||
|
||
</Project> |