Correct JSON
All checks were successful
Build, Push and Run Container / build (push) Successful in 24s
All checks were successful
Build, Push and Run Container / build (push) Successful in 24s
This commit is contained in:
@@ -1,15 +1,13 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace ProofOfConcept.Models;
|
namespace ProofOfConcept.Models;
|
||||||
|
|
||||||
public record Token
|
public record Token
|
||||||
{
|
{
|
||||||
public string AccessToken { get; init; }
|
public string AccessToken { get; init; } = String.Empty;
|
||||||
public DateTimeOffset Expires { get; init; }
|
public DateTimeOffset Expires { get; init; } = DateTimeOffset.Now;
|
||||||
public string TokenType { get; init; }
|
public string TokenType { get; init; } = String.Empty;
|
||||||
|
|
||||||
public Token(string AccessToken, int ExpiresIn, string TokenType, DateTimeOffset? received = null)
|
//For JSON deserialization
|
||||||
{
|
public int ExpiresIn { get => (int)DateTimeOffset.Now.Subtract(Expires).TotalSeconds; init => Expires = DateTimeOffset.Now.AddSeconds(value); }
|
||||||
this.AccessToken = AccessToken;
|
|
||||||
this.Expires = received?.AddSeconds(ExpiresIn) ?? DateTimeOffset.Now.AddSeconds(ExpiresIn);
|
|
||||||
this.TokenType = TokenType;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
|
using System.Text.Json;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using ProofOfConcept.Models;
|
||||||
using ProofOfConcept.Services;
|
using ProofOfConcept.Services;
|
||||||
using ProofOfConcept.Utilities;
|
using ProofOfConcept.Utilities;
|
||||||
|
|
||||||
@@ -33,6 +35,20 @@ if (app.Environment.IsDevelopment())
|
|||||||
{
|
{
|
||||||
app.MapOpenApi();
|
app.MapOpenApi();
|
||||||
app.MapGet("/GetPartnerAuthenticationToken", ([FromServices] TeslaAuthenticatorService service) => service.GetPartnerAuthenticationTokenAsync());
|
app.MapGet("/GetPartnerAuthenticationToken", ([FromServices] TeslaAuthenticatorService service) => service.GetPartnerAuthenticationTokenAsync());
|
||||||
|
app.MapGet("/PartnerToken", ([FromQueryAttribute] string json, [FromServices] IMemoryCache memoryCache) =>
|
||||||
|
{
|
||||||
|
var serializerOptions = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||||
|
};
|
||||||
|
|
||||||
|
Token? token = JsonSerializer.Deserialize<Token>(json, serializerOptions);
|
||||||
|
if (token is not null)
|
||||||
|
memoryCache.Set(Keys.TeslaPartnerToken, token, token.Expires.Subtract(TimeSpan.FromSeconds(5)));
|
||||||
|
|
||||||
|
return JsonSerializer.Serialize(token, new JsonSerializerOptions() { WriteIndented = true });
|
||||||
|
});
|
||||||
app.MapGet("/CheckRegisteredApplication", ([FromServices] TeslaAuthenticatorService service) => service.CheckApplicationRegistrationAsync());
|
app.MapGet("/CheckRegisteredApplication", ([FromServices] TeslaAuthenticatorService service) => service.CheckApplicationRegistrationAsync());
|
||||||
app.MapGet("/RegisterApplication", ([FromServices] TeslaAuthenticatorService service) => service.RegisterApplicationAsync());
|
app.MapGet("/RegisterApplication", ([FromServices] TeslaAuthenticatorService service) => service.RegisterApplicationAsync());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,12 +18,20 @@ public class TeslaAuthenticatorService
|
|||||||
const string authEndpointURL = "https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token";
|
const string authEndpointURL = "https://fleet-auth.prd.vn.cloud.tesla.com/oauth2/v3/token";
|
||||||
const string euBaseURL = "https://fleet-api.prd.eu.vn.cloud.tesla.com";
|
const string euBaseURL = "https://fleet-api.prd.eu.vn.cloud.tesla.com";
|
||||||
|
|
||||||
|
private JsonSerializerOptions serializerOptions;
|
||||||
|
|
||||||
public TeslaAuthenticatorService(ILogger<TeslaAuthenticatorService> logger, IOptions<TeslaAuthenticatorServiceConfiguration> options, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache)
|
public TeslaAuthenticatorService(ILogger<TeslaAuthenticatorService> logger, IOptions<TeslaAuthenticatorServiceConfiguration> options, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.httpClientFactory = httpClientFactory;
|
this.httpClientFactory = httpClientFactory;
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.configuration = options.Value;
|
this.configuration = options.Value;
|
||||||
|
|
||||||
|
serializerOptions = new JsonSerializerOptions()
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronously retrieves an authentication token from the Tesla partner API.
|
/// Asynchronously retrieves an authentication token from the Tesla partner API.
|
||||||
|
|||||||
Reference in New Issue
Block a user