-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API Test project; Add DI support
- Loading branch information
1 parent
3dbd5d6
commit 43f2e40
Showing
11 changed files
with
279 additions
and
5 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/SharpConnector.Api/Controllers/ConnectorController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// (c) 2020 Francesco Del Re <[email protected]> | ||
// This code is licensed under MIT license (see LICENSE.txt for details) | ||
using Microsoft.AspNetCore.Mvc; | ||
using SharpConnector.Interfaces; | ||
|
||
namespace SharpConnector.Api.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class ConnectorController : ControllerBase | ||
{ | ||
private readonly ISharpConnectorClient<string> _connectorClient; | ||
|
||
private readonly ILogger<ConnectorController> _logger; | ||
|
||
public ConnectorController(ILogger<ConnectorController> logger, ISharpConnectorClient<string> connectorClient) | ||
{ | ||
_logger = logger; | ||
_connectorClient = connectorClient; | ||
} | ||
|
||
[HttpGet("{key}", Name = "Get")] | ||
public ActionResult<string> Get(string key) | ||
{ | ||
var result = _connectorClient.Get(key); | ||
return result != null ? Ok(result) : NotFound(); | ||
} | ||
|
||
[HttpGet("async/{key}")] | ||
public async Task<ActionResult<string>> GetAsync(string key) | ||
{ | ||
var result = await _connectorClient.GetAsync(key); | ||
return result != null ? Ok(result) : NotFound(); | ||
} | ||
|
||
[HttpGet("all")] | ||
public ActionResult<IEnumerable<string>> GetAll() | ||
{ | ||
var results = _connectorClient.GetAll(); | ||
return Ok(results); | ||
} | ||
|
||
[HttpPost("insert")] | ||
public ActionResult<bool> Insert(string key, [FromBody] string value) | ||
{ | ||
var success = _connectorClient.Insert(key, value); | ||
return success ? Ok(success) : BadRequest("Insert failed."); | ||
} | ||
|
||
[HttpPost("insert/with-expiration")] | ||
public ActionResult<bool> Insert(string key, [FromBody] string value, TimeSpan expiration) | ||
{ | ||
var success = _connectorClient.Insert(key, value, expiration); | ||
return success ? Ok(success) : BadRequest("Insert with expiration failed."); | ||
} | ||
|
||
[HttpPost("insert-async")] | ||
public async Task<ActionResult<bool>> InsertAsync(string key, [FromBody] string value) | ||
{ | ||
var success = await _connectorClient.InsertAsync(key, value); | ||
return success ? Ok(success) : BadRequest("Async insert failed."); | ||
} | ||
|
||
[HttpPost("insert-async/with-expiration")] | ||
public async Task<ActionResult<bool>> InsertAsync(string key, [FromBody] string value, TimeSpan expiration) | ||
{ | ||
var success = await _connectorClient.InsertAsync(key, value, expiration); | ||
return success ? Ok(success) : BadRequest("Async insert with expiration failed."); | ||
} | ||
|
||
[HttpPost("insert-many")] | ||
public ActionResult<bool> InsertMany([FromBody] Dictionary<string, string> values) | ||
{ | ||
var success = _connectorClient.InsertMany(values); | ||
return success ? Ok(success) : BadRequest("Insert many failed."); | ||
} | ||
|
||
[HttpPost("insert-many/with-expiration")] | ||
public ActionResult<bool> InsertMany([FromBody] Dictionary<string, string> values, TimeSpan expiration) | ||
{ | ||
var success = _connectorClient.InsertMany(values, expiration); | ||
return success ? Ok(success) : BadRequest("Insert many with expiration failed."); | ||
} | ||
|
||
[HttpDelete("{key}")] | ||
public ActionResult<bool> Delete(string key) | ||
{ | ||
var success = _connectorClient.Delete(key); | ||
return success ? Ok(success) : NotFound("Delete failed."); | ||
} | ||
|
||
[HttpDelete("async/{key}")] | ||
public async Task<ActionResult<bool>> DeleteAsync(string key) | ||
{ | ||
var success = await _connectorClient.DeleteAsync(key); | ||
return success ? Ok(success) : NotFound("Async delete failed."); | ||
} | ||
|
||
[HttpPut("{key}")] | ||
public ActionResult<bool> Update(string key, [FromBody] string value) | ||
{ | ||
var success = _connectorClient.Update(key, value); | ||
return success ? Ok(success) : NotFound("Update failed."); | ||
} | ||
|
||
[HttpPut("async/{key}")] | ||
public async Task<ActionResult<bool>> UpdateAsync(string key, [FromBody] string value) | ||
{ | ||
var success = await _connectorClient.UpdateAsync(key, value); | ||
return success ? Ok(success) : NotFound("Async update failed."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// (c) 2020 Francesco Del Re <[email protected]> | ||
// This code is licensed under MIT license (see LICENSE.txt for details) | ||
using SharpConnector.Middleware; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
// Register the SharpConnector services with string payload type. | ||
builder.Services.AddSharpConnectorServices<string>(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:44453", | ||
"sslPort": 44308 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5124", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7158;http://localhost:5124", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SharpConnector\SharpConnector.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@SharpConnector.Api_HostAddress = http://localhost:5124 | ||
|
||
GET {{SharpConnector.Api_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"ConnectorConfig": { | ||
"Instance": "Redis", | ||
"DatabaseNumber": 0, | ||
"ConnectionString": "redis_connectionstring_here" | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
31 changes: 31 additions & 0 deletions
31
src/SharpConnector/Middleware/SharpConnectorServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// (c) 2020 Francesco Del Re <[email protected]> | ||
// This code is licensed under MIT license (see LICENSE.txt for details) | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SharpConnector.Interfaces; | ||
|
||
namespace SharpConnector.Middleware | ||
{ | ||
public static class SharpConnectorServiceExtensions | ||
{ | ||
/// <summary> | ||
/// Registers the SharpConnector services in the dependency injection container. | ||
/// This allows for a generic SharpConnectorClient to be used with any specified type T. | ||
/// </summary> | ||
/// <typeparam name="T">The type parameter that specifies the type the client will handle.</typeparam> | ||
/// <param name="services">The service collection to which the SharpConnector services will be added.</param> | ||
/// <returns>The updated service collection with the SharpConnector services registered.</returns> | ||
public static IServiceCollection AddSharpConnectorServices<T>(this IServiceCollection services) | ||
{ | ||
services.AddControllers(); | ||
|
||
services.AddSingleton<ISharpConnectorClient<T>>(sp => | ||
{ | ||
var configuration = sp.GetRequiredService<IConfiguration>(); | ||
return new SharpConnectorClient<T>(configuration); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters