-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f383e2f
commit fc7bcfb
Showing
7 changed files
with
86 additions
and
113 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
File renamed without changes.
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,71 @@ | ||
using System.Text.Json; | ||
using Azure.Storage.Blobs; | ||
using Azure.Storage.Blobs.Models; | ||
using Azure; | ||
|
||
namespace WebApi.Services; | ||
|
||
public interface IQuoteService | ||
{ | ||
Task<IEnumerable<Quote>> Get(); | ||
Task<IEnumerable<Quote>> Get(string symbol); | ||
} | ||
|
||
public partial class QuoteService( | ||
ILogger<QuoteService> logger, | ||
IStorage storage) : IQuoteService | ||
{ | ||
private readonly ILogger<QuoteService> _logger = logger; | ||
private readonly IStorage _storage = storage; | ||
|
||
/// <summary> | ||
/// Get default quotes | ||
/// </summary> | ||
/// <returns cref="Quote">List of default quotes</returns> | ||
public async Task<IEnumerable<Quote>> Get() | ||
=> await Get("QQQ"); | ||
|
||
/// <summary> | ||
/// Get quotes for a specific symbol. | ||
/// </summary> | ||
/// <param name="symbol">"SPY" or "QQQ" only, for now</param> | ||
public async Task<IEnumerable<Quote>> Get(string symbol) | ||
{ | ||
string blobName = $"{symbol}-DAILY.json"; | ||
|
||
try | ||
{ | ||
BlobClient blob = _storage.GetBlobClient(blobName); | ||
|
||
if (!await blob.ExistsAsync()) | ||
{ | ||
_logger.LogWarning("Blob {BlobName} not found, using backup data", blobName); | ||
return backupQuotes; | ||
} | ||
|
||
Response<BlobDownloadInfo> response = await blob.DownloadAsync(); | ||
using Stream? stream = response?.Value.Content; | ||
|
||
if (stream == null) | ||
{ | ||
_logger.LogError("Download stream was null for {BlobName}", blobName); | ||
return backupQuotes; | ||
} | ||
|
||
List<Quote>? quotes = await JsonSerializer.DeserializeAsync<List<Quote>>(stream); | ||
|
||
if (quotes == null || quotes.Count == 0) | ||
{ | ||
_logger.LogWarning("No quotes found in {BlobName}", blobName); | ||
return backupQuotes; | ||
} | ||
|
||
return quotes.OrderBy(x => x.Date); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to retrieve quotes for {Symbol}", symbol); | ||
return backupQuotes; | ||
} | ||
} | ||
} |
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
File renamed without changes.