-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #745 - The new
OptionSnapshotRequest
class added and used for…
… the `ListSnapshotsAsync` method (breaking change). (cherry picked from commit be7b14e)
- Loading branch information
Showing
9 changed files
with
147 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Alpaca.Markets; | ||
|
||
/// <summary> | ||
/// Encapsulates single page response in Alpaca Data API v2. | ||
/// </summary> | ||
/// <typeparam name="TItem">Type of paged item (bar, trade or quote)</typeparam> | ||
public interface IDictionaryPage<TItem> | ||
{ | ||
/// <summary> | ||
/// Gets the next page token for continuation. If value of this property | ||
/// equals to <c>null</c> this page is the last one and no more data is available. | ||
/// </summary> | ||
[UsedImplicitly] | ||
public String? NextPageToken { get; } | ||
|
||
/// <summary> | ||
/// Gets list of items for this response grouped by asset symbols. | ||
/// </summary> | ||
public IReadOnlyDictionary<String, TItem> Items { get; } | ||
} |
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,45 @@ | ||
namespace Alpaca.Markets; | ||
|
||
[SuppressMessage( | ||
"Microsoft.Performance", "CA1812:Avoid uninstantiated internal classes", | ||
Justification = "Object instances of this class will be created by Newtonsoft.JSON library.")] | ||
[DebuggerDisplay("{DebuggerDisplay,nq}", Type = nameof(IDictionaryPage<ISnapshot>) + "<" + nameof(ISnapshot) + ">")] | ||
internal sealed class JsonOptionsSnapshotData : IDictionaryPage<ISnapshot> | ||
{ | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
[JsonProperty(PropertyName = "snapshots", Required = Required.Default)] | ||
public Dictionary<String, JsonOptionSnapshot> ItemsList { get; [ExcludeFromCodeCoverage] set; } = new(); | ||
|
||
[JsonProperty(PropertyName = "next_page_token", Required = Required.Default)] | ||
public String? NextPageToken { get; set; } | ||
|
||
[JsonIgnore] | ||
public IReadOnlyDictionary<String, ISnapshot> Items { get; [ExcludeFromCodeCoverage] private set; } | ||
= new Dictionary<String, ISnapshot>(); | ||
|
||
[OnDeserialized] | ||
[UsedImplicitly] | ||
internal void OnDeserializedMethod( | ||
StreamingContext _) => | ||
Items = (ItemsList ?? []).ToDictionary( | ||
kvp => kvp.Key, | ||
withSymbol<ISnapshot, JsonOptionSnapshot>, | ||
StringComparer.Ordinal); | ||
|
||
[ExcludeFromCodeCoverage] | ||
public override String ToString() => | ||
JsonConvert.SerializeObject(this); | ||
|
||
[ExcludeFromCodeCoverage] | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
private String DebuggerDisplay => | ||
this.ToDebuggerDisplayString(); | ||
|
||
private static TApi withSymbol<TApi, TJson>( | ||
KeyValuePair<String, TJson> kvp) | ||
where TJson : TApi, ISymbolMutable | ||
{ | ||
kvp.Value.SetSymbol(kvp.Key); | ||
return kvp.Value; | ||
} | ||
} |
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,49 @@ | ||
namespace Alpaca.Markets; | ||
|
||
/// <summary> | ||
/// Encapsulates data for latest options data requests on Alpaca Data API v2. | ||
/// </summary> | ||
public sealed class OptionSnapshotRequest : Validation.IRequest | ||
{ | ||
private readonly HashSet<String> _symbols = new(StringComparer.Ordinal); | ||
|
||
/// <summary> | ||
/// Creates new instance of <see cref="OptionSnapshotRequest"/> object. | ||
/// </summary> | ||
/// <param name="symbols">Options symbols list for data retrieval.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// The <paramref name="symbols"/> argument is <c>null</c>. | ||
/// </exception> | ||
public OptionSnapshotRequest( | ||
IEnumerable<String> symbols) => | ||
_symbols.UnionWith(symbols.EnsureNotNull()); | ||
|
||
/// <summary> | ||
/// Gets options symbols list for data retrieval. | ||
/// </summary> | ||
[UsedImplicitly] | ||
public IReadOnlyCollection<String> Symbols => _symbols; | ||
|
||
/// <summary> | ||
/// Gets options feed for data retrieval. | ||
/// </summary> | ||
[UsedImplicitly] | ||
[ExcludeFromCodeCoverage] | ||
public OptionsFeed? OptionsFeed { get; set; } | ||
|
||
internal async ValueTask<UriBuilder> GetUriBuilderAsync( | ||
HttpClient httpClient) => | ||
new UriBuilder(httpClient.BaseAddress!) | ||
{ | ||
Query = await new QueryBuilder() | ||
.AddParameter("symbols", Symbols.ToList()) | ||
.AddParameter("feed", OptionsFeed) | ||
.AsStringAsync().ConfigureAwait(false) | ||
}.AppendPath("snapshots"); | ||
|
||
IEnumerable<RequestValidationException?> Validation.IRequest.GetExceptions() | ||
{ | ||
yield return Symbols.TryValidateSymbolsList(); | ||
yield return Symbols.TryValidateSymbolName(); | ||
} | ||
} |
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