-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add async methods to the ResolverClient #18814
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
067cdcd
Changes to fix issues.
azabbasi bfed035
Update RepositoryHandler.cs
azabbasi 04ce075
Update CultureInfo to use CurrentCulture
azabbasi 651835b
Formatting updates
azabbasi 5aec2b6
Add ClientDiagnostics - Add sync methods.
azabbasi 7ce0464
Merge branch 'feature/modelsRepo' into azabbasi/RemoteFetcher
azabbasi 57f86a4
Remove extra line
azabbasi e54f752
Add clientDiagnostics to LocalModelFetcher
azabbasi 901cf6d
Throw if cancellation is requested.
azabbasi c753287
Update LocalModelFetcher.cs
azabbasi b4efb9a
Update ResolverEventSource.cs
azabbasi 5a29be0
Merge branch 'feature/modelsRepo' into azabbasi/RemoteFetcher
azabbasi 30f1817
Add Async APIs to the ResolverClient
azabbasi 63204bb
Update ModelQuery.cs
azabbasi 9d03859
Make DefaultModelRepository public.
azabbasi b1eaeae
Use async bool pattern.
azabbasi 9d8c3d2
Make ModelSRepository plural
azabbasi bb10fd2
Address comments
azabbasi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placeholder/reminder that there is a cleaner implementation of model parsing logic recently merged in the DMR tools repo, which I will update here after this PR is merged. |
||
|
||
namespace Azure.Iot.ModelsRepository | ||
{ | ||
|
@@ -29,16 +28,14 @@ public ModelMetadata GetMetadata() | |
|
||
public string GetId() | ||
{ | ||
using (JsonDocument document = JsonDocument.Parse(_content, _parseOptions)) | ||
{ | ||
JsonElement _root = document.RootElement; | ||
using JsonDocument document = JsonDocument.Parse(_content, _parseOptions); | ||
JsonElement _root = document.RootElement; | ||
|
||
if (_root.ValueKind == JsonValueKind.Object && _root.TryGetProperty("@id", out JsonElement id)) | ||
if (_root.ValueKind == JsonValueKind.Object && _root.TryGetProperty("@id", out JsonElement id)) | ||
{ | ||
if (id.ValueKind == JsonValueKind.String) | ||
{ | ||
if (id.ValueKind == JsonValueKind.String) | ||
{ | ||
return id.GetString(); | ||
} | ||
return id.GetString(); | ||
} | ||
} | ||
|
||
|
@@ -49,34 +46,32 @@ public IList<string> GetExtends() | |
{ | ||
List<string> dependencies = new List<string>(); | ||
|
||
using (JsonDocument document = JsonDocument.Parse(_content, _parseOptions)) | ||
{ | ||
JsonElement _root = document.RootElement; | ||
using JsonDocument document = JsonDocument.Parse(_content, _parseOptions); | ||
JsonElement _root = document.RootElement; | ||
|
||
if (_root.ValueKind == JsonValueKind.Object && _root.TryGetProperty("extends", out JsonElement extends)) | ||
if (_root.ValueKind == JsonValueKind.Object && _root.TryGetProperty("extends", out JsonElement extends)) | ||
{ | ||
if (extends.ValueKind == JsonValueKind.Array) | ||
{ | ||
if (extends.ValueKind == JsonValueKind.Array) | ||
foreach (JsonElement extendElement in extends.EnumerateArray()) | ||
{ | ||
foreach (JsonElement extendElement in extends.EnumerateArray()) | ||
if (extendElement.ValueKind == JsonValueKind.String) | ||
{ | ||
if (extendElement.ValueKind == JsonValueKind.String) | ||
{ | ||
dependencies.Add(extendElement.GetString()); | ||
} | ||
else if (extendElement.ValueKind == JsonValueKind.Object) | ||
{ | ||
// extends can have multiple levels and can contain components. | ||
// TODO: Support object ctor - inefficient serialize. | ||
ModelMetadata nested_interface = new ModelQuery(JsonSerializer.Serialize(extendElement)).GetMetadata(); | ||
dependencies.AddRange(nested_interface.Dependencies); | ||
} | ||
dependencies.Add(extendElement.GetString()); | ||
} | ||
else if (extendElement.ValueKind == JsonValueKind.Object) | ||
{ | ||
// extends can have multiple levels and can contain components. | ||
// TODO: Support object ctor - inefficient serialize. | ||
ModelMetadata nested_interface = new ModelQuery(JsonSerializer.Serialize(extendElement)).GetMetadata(); | ||
dependencies.AddRange(nested_interface.Dependencies); | ||
} | ||
} | ||
else if (extends.ValueKind == JsonValueKind.String) | ||
{ | ||
dependencies.Add(extends.GetString()); | ||
} | ||
} | ||
else if (extends.ValueKind == JsonValueKind.String) | ||
{ | ||
dependencies.Add(extends.GetString()); | ||
} | ||
} | ||
|
||
return dependencies; | ||
|
@@ -87,44 +82,42 @@ public IList<string> GetComponentSchemas() | |
{ | ||
List<string> componentSchemas = new List<string>(); | ||
|
||
using (JsonDocument document = JsonDocument.Parse(_content, _parseOptions)) | ||
{ | ||
JsonElement _root = document.RootElement; | ||
using JsonDocument document = JsonDocument.Parse(_content, _parseOptions); | ||
JsonElement _root = document.RootElement; | ||
|
||
if (_root.ValueKind == JsonValueKind.Object && _root.TryGetProperty("contents", out JsonElement contents)) | ||
if (_root.ValueKind == JsonValueKind.Object && _root.TryGetProperty("contents", out JsonElement contents)) | ||
{ | ||
if (contents.ValueKind == JsonValueKind.Array) | ||
{ | ||
if (contents.ValueKind == JsonValueKind.Array) | ||
foreach (JsonElement element in contents.EnumerateArray()) | ||
{ | ||
foreach (JsonElement element in contents.EnumerateArray()) | ||
if (element.TryGetProperty("@type", out JsonElement type)) | ||
{ | ||
if (element.TryGetProperty("@type", out JsonElement type)) | ||
if (type.ValueKind == JsonValueKind.String && type.GetString() == "Component") | ||
{ | ||
if (type.ValueKind == JsonValueKind.String && type.GetString() == "Component") | ||
if (element.TryGetProperty("schema", out JsonElement schema)) | ||
{ | ||
if (element.TryGetProperty("schema", out JsonElement schema)) | ||
if (schema.ValueKind == JsonValueKind.String) | ||
{ | ||
if (schema.ValueKind == JsonValueKind.String) | ||
{ | ||
componentSchemas.Add(schema.GetString()); | ||
} | ||
else if (schema.ValueKind == JsonValueKind.Array) | ||
componentSchemas.Add(schema.GetString()); | ||
} | ||
else if (schema.ValueKind == JsonValueKind.Array) | ||
{ | ||
foreach (JsonElement schemaElement in schema.EnumerateArray()) | ||
{ | ||
foreach (JsonElement schemaElement in schema.EnumerateArray()) | ||
if (schemaElement.ValueKind == JsonValueKind.String) | ||
{ | ||
if (schemaElement.ValueKind == JsonValueKind.String) | ||
{ | ||
componentSchemas.Add(schemaElement.GetString()); | ||
} | ||
componentSchemas.Add(schemaElement.GetString()); | ||
} | ||
} | ||
else if (schema.ValueKind == JsonValueKind.Object) | ||
} | ||
else if (schema.ValueKind == JsonValueKind.Object) | ||
{ | ||
if (schema.TryGetProperty("extends", out JsonElement schemaObjExtends)) | ||
{ | ||
if (schema.TryGetProperty("extends", out JsonElement schemaObjExtends)) | ||
if (schemaObjExtends.ValueKind == JsonValueKind.String) | ||
{ | ||
if (schemaObjExtends.ValueKind == JsonValueKind.String) | ||
{ | ||
componentSchemas.Add(schemaObjExtends.GetString()); | ||
} | ||
componentSchemas.Add(schemaObjExtends.GetString()); | ||
} | ||
} | ||
} | ||
|
@@ -138,39 +131,43 @@ public IList<string> GetComponentSchemas() | |
return componentSchemas; | ||
} | ||
|
||
public async Task<Dictionary<string, string>> ListToDictAsync() | ||
public Dictionary<string, string> ListToDict() | ||
{ | ||
Dictionary<string, string> result = new Dictionary<string, string>(); | ||
|
||
using (JsonDocument document = JsonDocument.Parse(_content, _parseOptions)) | ||
{ | ||
JsonElement _root = document.RootElement; | ||
using JsonDocument document = JsonDocument.Parse(_content, _parseOptions); | ||
JsonElement _root = document.RootElement; | ||
|
||
if (_root.ValueKind == JsonValueKind.Array) | ||
if (_root.ValueKind == JsonValueKind.Array) | ||
{ | ||
foreach (JsonElement element in _root.EnumerateArray()) | ||
{ | ||
foreach (JsonElement element in _root.EnumerateArray()) | ||
if (element.ValueKind == JsonValueKind.Object) | ||
{ | ||
if (element.ValueKind == JsonValueKind.Object) | ||
{ | ||
using (MemoryStream stream = new MemoryStream()) | ||
{ | ||
await JsonSerializer.SerializeAsync(stream, element).ConfigureAwait(false); | ||
stream.Position = 0; | ||
using MemoryStream stream = WriteJsonElementToStream(element); | ||
|
||
using (StreamReader streamReader = new StreamReader(stream)) | ||
{ | ||
string serialized = await streamReader.ReadToEndAsync().ConfigureAwait(false); | ||
using StreamReader streamReader = new StreamReader(stream); | ||
string serialized = streamReader.ReadToEnd(); | ||
|
||
string id = new ModelQuery(serialized).GetId(); | ||
result.Add(id, serialized); | ||
} | ||
} | ||
} | ||
string id = new ModelQuery(serialized).GetId(); | ||
result.Add(id, serialized); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static MemoryStream WriteJsonElementToStream(JsonElement item) | ||
{ | ||
var memoryStream = new MemoryStream(); | ||
using var writer = new Utf8JsonWriter(memoryStream); | ||
|
||
item.WriteTo(writer); | ||
writer.Flush(); | ||
memoryStream.Seek(0, SeekOrigin.Begin); | ||
|
||
return memoryStream; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on
RequestFailedException
documentation, it only applies to requests that have an HTTP status.This exception is caught at
RepositoryHandler
level and converted to aResolverException
which is currently the Exception that is exposed to users.We will have a discussion about the Exception that is thrown from the ResolverException point of view during the initial API review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add doc comments to the client that indicate what exception types can be thrown and when.