-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
800d38a
commit c2bc6e0
Showing
10 changed files
with
335 additions
and
65 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Google.Apis.Sheets.v4; | ||
using Google.Apis.Sheets.v4.Data; | ||
|
||
namespace BotNet.Services.GoogleSheets { | ||
public sealed class GoogleSheetsClient( | ||
SheetsService sheetsService | ||
) { | ||
private readonly SheetsService _sheetsService = sheetsService; | ||
|
||
public async Task<ImmutableList<T>> GetDataAsync<T>(string spreadsheetId, string range, string firstColumn, CancellationToken cancellationToken) { | ||
int firstColumnIndex = GetColumnIndex(firstColumn); | ||
|
||
// Fetch data | ||
SpreadsheetsResource.ValuesResource.GetRequest getRequest = _sheetsService.Spreadsheets.Values.Get( | ||
spreadsheetId: spreadsheetId, | ||
range: range | ||
); | ||
ValueRange response = await getRequest.ExecuteAsync(cancellationToken); | ||
|
||
// Get type info | ||
ConstructorInfo constructor = typeof(T).GetConstructors().Single(); | ||
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); | ||
|
||
// Map data | ||
ImmutableList<T>.Builder builder = ImmutableList.CreateBuilder<T>(); | ||
foreach (IList<object> row in response.Values) { | ||
if (row.Count < properties.Length) continue; | ||
|
||
object?[] parameters = new object?[properties.Length]; | ||
for (int i = 0; i < properties.Length; i++) { | ||
PropertyInfo property = properties[i]; | ||
|
||
FromColumnAttribute fromColumn = property.GetCustomAttribute<FromColumnAttribute>() | ||
?? throw new InvalidProgramException("Property not decorated with [FromColumn]"); | ||
|
||
int columnIndex = GetColumnIndex(fromColumn.Column) - firstColumnIndex; | ||
if (columnIndex >= row.Count) { | ||
parameters[i] = null; | ||
continue; | ||
} | ||
|
||
if (row[columnIndex] is not string value) { | ||
parameters[i] = null; | ||
continue; | ||
} | ||
|
||
if (property.PropertyType == typeof(string)) { | ||
parameters[i] = value; | ||
} else if (property.PropertyType == typeof(decimal)) { | ||
if (decimal.TryParse(value, out decimal decimalValue)) { | ||
parameters[i] = decimalValue; | ||
} else { | ||
parameters[i] = 0m; | ||
} | ||
} else if (property.PropertyType == typeof(decimal?)) { | ||
if (decimal.TryParse(value, out decimal decimalValue)) { | ||
parameters[i] = decimalValue; | ||
} else { | ||
parameters[i] = (decimal?)null; | ||
} | ||
} else if (property.PropertyType == typeof(int)) { | ||
if (int.TryParse(value, out int intValue)) { | ||
parameters[i] = intValue; | ||
} else { | ||
parameters[i] = 0; | ||
} | ||
} else if (property.PropertyType == typeof(int?)) { | ||
if (int.TryParse(value, out int intValue)) { | ||
parameters[i] = intValue; | ||
} else { | ||
parameters[i] = (int?)null; | ||
} | ||
} else if (property.PropertyType == typeof(double)) { | ||
if (double.TryParse(value, out double doubleValue)) { | ||
parameters[i] = doubleValue; | ||
} else { | ||
parameters[i] = 0.0; | ||
} | ||
} else if (property.PropertyType == typeof(double?)) { | ||
if (double.TryParse(value, out double doubleValue)) { | ||
parameters[i] = doubleValue; | ||
} else { | ||
parameters[i] = (double?)null; | ||
} | ||
} else { | ||
parameters[i] = Convert.ChangeType(value, property.PropertyType); | ||
} | ||
} | ||
|
||
builder.Add((T)constructor.Invoke(parameters)); | ||
} | ||
|
||
return builder.ToImmutable(); | ||
} | ||
|
||
public static int GetColumnIndex(string columnName) { | ||
int index = 0; | ||
for (int i = 0; i < columnName.Length; i++) { | ||
index *= 26; | ||
index += (columnName[i] - 'A' + 1); | ||
} | ||
return index - 1; | ||
} | ||
} | ||
} |
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,5 @@ | ||
namespace BotNet.Services.GoogleSheets.Options { | ||
public class GoogleSheetsOptions { | ||
public string? ApiKey { get; set; } | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
BotNet.Services/GoogleSheets/ServiceCollectionExtensions.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,20 @@ | ||
using BotNet.Services.GoogleSheets.Options; | ||
using Google.Apis.Services; | ||
using Google.Apis.Sheets.v4; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace BotNet.Services.GoogleSheets { | ||
public static class ServiceCollectionExtensions { | ||
public static IServiceCollection AddGoogleSheets(this IServiceCollection services) { | ||
services.AddSingleton(serviceProvider => { | ||
GoogleSheetsOptions options = serviceProvider.GetRequiredService<IOptions<GoogleSheetsOptions>>().Value; | ||
return new SheetsService(new BaseClientService.Initializer { | ||
ApiKey = options.ApiKey | ||
}); | ||
}); | ||
services.AddTransient<GoogleSheetsClient>(); | ||
return services; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
BotNet.Services/KokizzuVPSBenchmark/ServiceCollectionExtensions.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,11 @@ | ||
using BotNet.Services.SQL; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BotNet.Services.KokizzuVPSBenchmark { | ||
public static class ServiceCollectionExtensions { | ||
public static IServiceCollection AddKokizzuVPSBenchmarkDataSource(this IServiceCollection services) { | ||
services.AddKeyedTransient<IScopedDataSource, VPSBenchmarkDataSource>("vps"); | ||
return services; | ||
} | ||
} | ||
} |
7 changes: 3 additions & 4 deletions
7
BotNet.Services/VPS/VPSBenchmark.cs → ...vices/KokizzuVPSBenchmark/VPSBenchmark.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
Oops, something went wrong.