Skip to content

Commit

Permalink
Use CleanClientAsync in DadataAddressResolver.ResolveLocationAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
o.nadymov committed Sep 19, 2024
1 parent 0f590c2 commit 0f18128
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Spoleto.AddressResolver.Dadata/DadataAddressResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace Spoleto.AddressResolver.Dadata
public class DadataAddressResolver : IAddressResolver
{
private readonly DadataOptions _options;
private readonly SuggestClientAsync _dadataClient;
private readonly CleanClientAsync _cleanDadataClient;
private readonly SuggestClientAsync _suggestDadataClient;

public DadataAddressResolver(DadataOptions options)
{
Expand All @@ -19,7 +20,8 @@ public DadataAddressResolver(DadataOptions options)

_options = options;

_dadataClient = new SuggestClientAsync(_options.Token);
_cleanDadataClient = new CleanClientAsync(options.Token, options.Secret);
_suggestDadataClient = new SuggestClientAsync(_options.Token);
}

/// <inheritdoc/>
Expand All @@ -32,8 +34,9 @@ public async Task<AddressLocation> ResolveLocationAsync(string originalLocationA
if (String.IsNullOrEmpty(originalLocationAddress))
throw new ArgumentNullException(nameof(originalLocationAddress));

var fullAddress = await GetFullAddressAsync(originalLocationAddress, countryIsoCode).ConfigureAwait(false);
if (fullAddress?.data is not Address dadataAddress)
var fullAddress = await _cleanDadataClient.Clean<Address>(originalLocationAddress).ConfigureAwait(false);

if (fullAddress is not Address dadataAddress)
throw new ArgumentException($"Could not find the full address for <{originalLocationAddress}>.", nameof(fullAddress));

var location = ConvertToAddressLocation(originalLocationAddress, dadataAddress);
Expand Down Expand Up @@ -86,7 +89,7 @@ private async Task<IList<Suggestion<Address>>> GetAddressAsync(string searchText

}

var response = await _dadataClient.SuggestAddress(request).ConfigureAwait(false);
var response = await _suggestDadataClient.SuggestAddress(request).ConfigureAwait(false);

return response?.suggestions;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Spoleto.AddressResolver.Dadata/Options/DadataOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ public record DadataOptions
/// </summary>
public string Token { get; set; }

/// <summary>
/// Gets or sets the Dadata secret key.
/// </summary>
public string Secret { get; set; }

/// <summary>
/// Checks that all the settings within the options are configured properly.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <see cref="Token"/> is null.</exception>
/// <exception cref="ArgumentNullException">Thrown when <see cref="Token"/> or <see cref="Secret"/> are null.</exception>
public void Validate()
{
if (String.IsNullOrEmpty(Token))
throw new ArgumentNullException(nameof(Token));

if (String.IsNullOrEmpty(Secret))
throw new ArgumentNullException(nameof(Secret));
}
}
}

0 comments on commit 0f18128

Please sign in to comment.