Skip to content

Commit

Permalink
Feature/sdk 1 subscription api (#1)
Browse files Browse the repository at this point in the history
* 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
edinana authored May 14, 2024
1 parent e337dad commit 94e526c
Show file tree
Hide file tree
Showing 42 changed files with 1,773 additions and 0 deletions.
Empty file.
8 changes: 8 additions & 0 deletions .idea/.idea.subsbase-sdk-dotnet.dir/.idea/indexLayout.xml

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.

6 changes: 6 additions & 0 deletions .idea/.idea.subsbase-sdk-dotnet.dir/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions .idea/.idea.subsbase-sdk-dotnet.dir/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/.idea/.idea.SubsBase.SDK/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/.idea/.idea.SubsBase.SDK/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/.idea/.idea.SubsBase.SDK/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/.idea/.idea.SubsBase.SDK/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

987 changes: 987 additions & 0 deletions src/.idea/config/applicationhost.config

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions src/SubsBase.SDK.Authentication/Client/AuthenticationClient.cs
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;
}
}
8 changes: 8 additions & 0 deletions src/SubsBase.SDK.Authentication/Constants.cs
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";
}
18 changes: 18 additions & 0 deletions src/SubsBase.SDK.Authentication/Contracts/AuthResponse.cs
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; }
}
16 changes: 16 additions & 0 deletions src/SubsBase.SDK.Authentication/Install.cs
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;
}
}
35 changes: 35 additions & 0 deletions src/SubsBase.SDK.Authentication/Service/AuthenticationService.cs
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;
}
}
14 changes: 14 additions & 0 deletions src/SubsBase.SDK.Authentication/SubsBase.SDK.Authentication.csproj
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>
52 changes: 52 additions & 0 deletions src/SubsBase.SDK.Common/Clients/GraphQLClient.cs
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}");
}
}
14 changes: 14 additions & 0 deletions src/SubsBase.SDK.Common/Contracts/Result.cs
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; }
}
7 changes: 7 additions & 0 deletions src/SubsBase.SDK.Common/Contracts/SubsBaseSdkOptions.cs
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;
}
20 changes: 20 additions & 0 deletions src/SubsBase.SDK.Common/Install.cs
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;
}
}
8 changes: 8 additions & 0 deletions src/SubsBase.SDK.Common/Selectors/IFieldSelector.cs
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);
}
18 changes: 18 additions & 0 deletions src/SubsBase.SDK.Common/SubsBase.SDK.Common.csproj
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>
Loading

0 comments on commit 94e526c

Please sign in to comment.