-
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.
Merge pull request #2 from yusuf-cirak/feat/attestation-vehicle-crede…
…ntials Implement Vehicle VIN Credential Retrieval via Attestation API
- Loading branch information
Showing
11 changed files
with
283 additions
and
17 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
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,19 @@ | ||
namespace Dimo.Client.Models | ||
{ | ||
|
||
#if NETSTANDARD | ||
public class VehicleVinVc | ||
{ | ||
public string VcUrl { get; set; } | ||
public string VcQuery { get; set; } | ||
public string Message { get; set; } | ||
} | ||
#elif NET6_0_OR_GREATER | ||
public record VehicleVinVc | ||
{ | ||
public string VcUrl { get; init; } | ||
public string VcQuery { get; init; } | ||
public string Message { get; init; } | ||
} | ||
#endif | ||
} |
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,48 @@ | ||
using System; | ||
|
||
#if NETSTANDARD | ||
using Newtonsoft.Json; | ||
#elif NET6_0_OR_GREATER | ||
using System.Text.Json.Serialization; | ||
#endif | ||
|
||
namespace Dimo.Client.Models | ||
{ | ||
#if NETSTANDARD | ||
public class VinVcLatestScheme<T> | ||
{ | ||
[JsonProperty("vinVCLatest")] | ||
public T VinVcLatest { get; set; } | ||
} | ||
|
||
public class VehicleVinVcLatest | ||
{ | ||
public long VehicleTokenId { get; set; } | ||
public string Vin { get; set; } | ||
public string RecordedBy { get; set; } | ||
public DateTime RecordedAt { get; set; } | ||
public string CountryCode { get; set; } | ||
public string VehicleContractAddress { get; set; } | ||
public DateTime ValidFrom { get; set; } | ||
public DateTime ValidTo { get; set; } | ||
|
||
[JsonProperty("rawVC")] | ||
public string RawVc { get; set; } | ||
} | ||
#elif NET6_0_OR_GREATER | ||
|
||
public record VinVcLatestScheme<T>(T VinVcLatest); | ||
|
||
public record VehicleVinVcLatest( | ||
long VehicleTokenId, | ||
string Vin, | ||
string RecordedBy, | ||
DateTime RecordedAt, | ||
string CountryCode, | ||
string VehicleContractAddress, | ||
DateTime ValidFrom, | ||
DateTime ValidTo, | ||
string RawVc); | ||
|
||
#endif | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Dimo.Client/Services/Attestation/AttestationService.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,57 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dimo.Client.Models; | ||
|
||
#if NETSTANDARD | ||
using Newtonsoft.Json; | ||
#elif NET6_0_OR_GREATER | ||
using System.Net.Http.Json; | ||
#endif | ||
|
||
namespace Dimo.Client.Services.Attestation | ||
{ | ||
public sealed class AttestationService : IAttestationService | ||
{ | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public AttestationService(IHttpClientFactory httpClientFactory) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
public async Task<VehicleVinVc> CreateVinVcAsync(long tokenId, string vehicleToken, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (tokenId <= 0) | ||
{ | ||
throw new ArgumentException("TokenId must be greater than 0", nameof(tokenId)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(vehicleToken)) | ||
throw new ArgumentException("Vehicle token must not be null or empty", nameof(vehicleToken)); | ||
|
||
|
||
const string path = "/v1/vc/vin/{0}"; | ||
|
||
using (var client = _httpClientFactory.CreateClient(ApiNames.Attestation)) | ||
{ | ||
client.DefaultRequestHeaders.Authorization = | ||
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", vehicleToken); | ||
|
||
string requestUri = string.Format(path, tokenId); | ||
|
||
var response = await client.PostAsync(requestUri, null, cancellationToken).ConfigureAwait(false); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
#if NETSTANDARD | ||
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
return JsonConvert.DeserializeObject<VehicleVinVc>(json); | ||
#elif NET6_0_OR_GREATER | ||
return await response.Content.ReadFromJsonAsync<VehicleVinVc>(cancellationToken: cancellationToken).ConfigureAwait(false); | ||
#endif | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Dimo.Client/Services/Attestation/IAttestationService.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,35 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Dimo.Client.Models; | ||
|
||
namespace Dimo.Client.Services.Attestation | ||
{ | ||
public interface IAttestationService | ||
{ | ||
/// <summary> | ||
/// Creates or retrieves a Vehicle Identification Number (VIN) Verifiable Credential (VC) for a specific vehicle. | ||
/// If a VC has never been created or has expired, generates a new one. Otherwise, returns the existing unexpired VC. | ||
/// The resulting rawVC can be used for querying the Telemetry API to obtain vehicle VIN information. | ||
/// </summary> | ||
/// <param name="tokenId">The vehicle's NFT token ID. The token must have permission to access the vehicle VIN data.</param> | ||
/// <param name="vehicleToken">The vehicle's JWT token for authentication.</param> | ||
/// /// <param name="cancellationToken">Token for signalling the cancellation of the operation.</param> | ||
/// <returns> | ||
/// Returns a VinVcResponse containing: | ||
/// - vcUrl: The GraphQL endpoint URL for querying the VC | ||
/// - vcQuery: The GraphQL query template to retrieve the rawVC | ||
/// - message: Success confirmation message with retrieval instructions | ||
/// </returns> | ||
/// <exception cref="ArgumentException">Thrown when tokenId or vehicleToken is null or empty.</exception> | ||
/// <exception cref="HttpRequestException">Thrown when the HTTP response was unsuccessful.</exception> | ||
/// <exception cref="Exception"> | ||
/// Thrown when: | ||
/// - The request is invalid (400) | ||
/// - Authentication fails | ||
/// - The token lacks necessary permissions | ||
/// </exception> | ||
Task<VehicleVinVc> CreateVinVcAsync(long tokenId, string vehicleToken,CancellationToken cancellationToken = default); | ||
} | ||
} |
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.