Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RepositoryHandler and ResolverClient #18857

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Iot.ModelsRepository.Fetchers;
using System;
Expand All @@ -16,19 +17,22 @@ internal class RepositoryHandler
private readonly IModelFetcher _modelFetcher;
private readonly Guid _clientId;
private readonly ClientDiagnostics _clientDiagnostics;
private readonly Uri _repositoryUri;
private readonly ResolverClientOptions _clientOptions;

public Uri RepositoryUri { get; }
Copy link
Contributor Author

@azabbasi azabbasi Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other place that these properties are accessed is in the ResolverClient that actually passes these down to the RepositoryHandler itself, no need to have backward references as ResolverClient knows about these values already.

public ResolverClientOptions ClientOptions { get; }

public RepositoryHandler(Uri repositoryUri, ClientDiagnostics clientdiagnostics, ResolverClientOptions options = null)
public RepositoryHandler(Uri repositoryUri, ClientDiagnostics clientDiagnostics, ResolverClientOptions options)
{
ClientOptions = options ?? new ResolverClientOptions();
RepositoryUri = repositoryUri;
_clientDiagnostics = clientdiagnostics;
Argument.AssertNotNull(options, nameof(options));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options shouldn't be null and we will not create a new instance if it is, the ResolverClient ctor will take care of it.


_clientOptions = options;
_clientDiagnostics = clientDiagnostics;
_modelFetcher = repositoryUri.Scheme == "file"
? _modelFetcher = new LocalModelFetcher(_clientDiagnostics, ClientOptions)
: _modelFetcher = new RemoteModelFetcher(_clientDiagnostics, ClientOptions);
? _modelFetcher = new LocalModelFetcher(_clientDiagnostics, _clientOptions)
: _modelFetcher = new RemoteModelFetcher(_clientDiagnostics, _clientOptions);
_clientId = Guid.NewGuid();

_repositoryUri = repositoryUri;

ResolverEventSource.Instance.InitFetcher(_clientId, repositoryUri.Scheme);
}

Expand Down Expand Up @@ -91,7 +95,7 @@ private async Task<IDictionary<string, string>> ProcessAsync(IEnumerable<string>

ModelMetadata metadata = new ModelQuery(result.Definition).GetMetadata();

if (ClientOptions.DependencyResolution >= DependencyResolutionOption.Enabled)
if (_clientOptions.DependencyResolution >= DependencyResolutionOption.Enabled)
{
IList<string> dependencies = metadata.Dependencies;

Expand Down Expand Up @@ -124,7 +128,7 @@ private async Task<FetchResult> FetchAsync(string dtmi, CancellationToken cancel
{
try
{
return await _modelFetcher.FetchAsync(dtmi, RepositoryUri, cancellationToken).ConfigureAwait(false);
return await _modelFetcher.FetchAsync(dtmi, _repositoryUri, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand All @@ -136,7 +140,7 @@ private FetchResult Fetch(string dtmi, CancellationToken cancellationToken)
{
try
{
return _modelFetcher.Fetch(dtmi, RepositoryUri, cancellationToken);
return _modelFetcher.Fetch(dtmi, _repositoryUri, cancellationToken);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ResolverClient
{
private readonly RepositoryHandler _repositoryHandler;
private readonly ClientDiagnostics _clientDiagnostics;
private readonly ResolverClientOptions _clientOptions;
private readonly Uri _repositoryUri;

/// <summary>
/// Initializes the <c>ResolverClient</c> with default client options while pointing to
Expand Down Expand Up @@ -80,8 +82,10 @@ public ResolverClient(Uri repositoryUri, ResolverClientOptions options)
{
Argument.AssertNotNull(options, nameof(options));

_clientOptions = options;
_clientDiagnostics = new ClientDiagnostics(options);
_repositoryHandler = new RepositoryHandler(repositoryUri, _clientDiagnostics, options);
_repositoryUri = repositoryUri;
_repositoryHandler = new RepositoryHandler(_repositoryUri, _clientDiagnostics, _clientOptions);
}

/// <summary>
Expand Down Expand Up @@ -205,12 +209,12 @@ public virtual IDictionary<string, string> Resolve(IEnumerable<string> dtmis, Ca
/// <summary>
/// Gets the <c>Uri</c> associated with the ResolverClient instance.
/// </summary>
public Uri RepositoryUri => _repositoryHandler.RepositoryUri;
public Uri RepositoryUri => _repositoryUri;

/// <summary>
/// Gets the <c>ResolverClientOptions</c> associated with the ResolverClient instance.
/// </summary>
public ResolverClientOptions ClientOptions => _repositoryHandler.ClientOptions;
public ResolverClientOptions ClientOptions => _clientOptions;

/// <summary>
/// Azure Device Models Repository used by default.
Expand Down