diff --git a/.github/workflows/_tests-template.yml b/.github/workflows/_tests-template.yml index acdf537..3919405 100644 --- a/.github/workflows/_tests-template.yml +++ b/.github/workflows/_tests-template.yml @@ -7,6 +7,12 @@ on: required: true type: string +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: build: runs-on: ubuntu-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1bee7ce..d8d0927 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,13 @@ on: schedule: - cron: '0 2 * * *' # each day at 2 AM UTC workflow_dispatch: # manual trigger - + +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: build: runs-on: ubuntu-latest diff --git a/.github/workflows/deploy-nuget.yml b/.github/workflows/deploy-nuget.yml index e8db9f9..40eb856 100644 --- a/.github/workflows/deploy-nuget.yml +++ b/.github/workflows/deploy-nuget.yml @@ -3,6 +3,12 @@ name: "Deploy Nuget" on: workflow_dispatch: # Manual trigger +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: build: runs-on: ubuntu-latest diff --git a/.github/workflows/tests-alphavantage.yml b/.github/workflows/tests-alphavantage.yml index ab94dd3..33c39a5 100644 --- a/.github/workflows/tests-alphavantage.yml +++ b/.github/workflows/tests-alphavantage.yml @@ -6,7 +6,13 @@ on: workflow_dispatch: # Manual trigger schedule: - cron: '0 2 * * *' # Runs nightly at 2 AM UTC on main branch - + +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: run-test: uses: ./.github/workflows/_tests-template.yml diff --git a/.github/workflows/tests-datahub.yml b/.github/workflows/tests-datahub.yml index 650910d..fdad057 100644 --- a/.github/workflows/tests-datahub.yml +++ b/.github/workflows/tests-datahub.yml @@ -7,6 +7,12 @@ on: schedule: - cron: '0 2 * * *' # Runs nightly at 2 AM UTC on main branch +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: run-test: uses: ./.github/workflows/_tests-template.yml diff --git a/.github/workflows/tests-xetra.yml b/.github/workflows/tests-xetra.yml index b62b432..75693b4 100644 --- a/.github/workflows/tests-xetra.yml +++ b/.github/workflows/tests-xetra.yml @@ -7,6 +7,12 @@ on: schedule: - cron: '0 2 * * *' # Runs nightly at 2 AM UTC on main branch +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: run-test: uses: ./.github/workflows/_tests-template.yml diff --git a/.github/workflows/tests-yahoo.yml b/.github/workflows/tests-yahoo.yml index 878835b..dd4baf7 100644 --- a/.github/workflows/tests-yahoo.yml +++ b/.github/workflows/tests-yahoo.yml @@ -7,6 +7,12 @@ on: schedule: - cron: '0 2 * * *' # Runs nightly at 2 AM UTC on main branch +permissions: + contents: read + pull-requests: write + checks: write + id-token: write + jobs: run-test: uses: ./.github/workflows/_tests-template.yml diff --git a/release-notes/v1.0.11.md b/release-notes/v1.0.11.md new file mode 100644 index 0000000..474361e --- /dev/null +++ b/release-notes/v1.0.11.md @@ -0,0 +1,5 @@ +### What's Changed + +* Fix: Making Polly AsyncPolicy optional. + + diff --git a/src/Finance.NET.csproj b/src/Finance.NET.csproj index 8d7a0b2..24f0c9c 100644 --- a/src/Finance.NET.csproj +++ b/src/Finance.NET.csproj @@ -12,7 +12,7 @@ true false Finance.NET - 1.0.10 + 1.0.11 Thorsten Alpers Thorsten Alpers A .NET library for retrieving real-time and historical financial data from Yahoo Finance and other popular sources. diff --git a/src/Services/AlphaVantageService.cs b/src/Services/AlphaVantageService.cs index 94f4aa6..3ff3759 100644 --- a/src/Services/AlphaVantageService.cs +++ b/src/Services/AlphaVantageService.cs @@ -18,15 +18,51 @@ namespace Finance.Net.Services; /// -public class AlphaVantageService(ILogger logger, -IHttpClientFactory httpClientFactory, -IOptions options, -IReadOnlyPolicyRegistry policyRegistry) : IAlphaVantageService +public class AlphaVantageService : IAlphaVantageService { - private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - private readonly IHttpClientFactory _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); - private readonly FinanceNetConfiguration _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); - private readonly AsyncPolicy _retryPolicy = policyRegistry?.Get(Constants.DefaultHttpRetryPolicy) ?? throw new ArgumentNullException(nameof(policyRegistry)); + private readonly ILogger _logger; + private readonly IHttpClientFactory _httpClientFactory; + private readonly FinanceNetConfiguration _options; + private readonly IAsyncPolicy _retryPolicy; + + /// + public AlphaVantageService( + ILogger logger, + IHttpClientFactory httpClientFactory, + IOptions options, + IReadOnlyPolicyRegistry? policyRegistry = null) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); + _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); + + if (policyRegistry != null && + policyRegistry.TryGet(Constants.DefaultHttpRetryPolicy, out var found)) + { + _retryPolicy = found; + } + else + { + _retryPolicy = Policy + .Handle() + .Or() + .WaitAndRetryAsync( + 3, + attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt - 1)), + (ex, ts, retryCount, ctx) => + { + _logger.LogWarning( + "Retry {RetryCount} wegen {Reason}, warte {Delay}s", + retryCount, + ex?.Message ?? "Unbekannt", + ts.TotalSeconds); + }); + + _logger.LogWarning( + "Retry-Policy '{PolicyKey}' nicht gefunden – verwende Default (3x Retry, Backoff).", + Constants.DefaultHttpRetryPolicy); + } + } /// public async Task GetOverviewAsync(string symbol, CancellationToken token = default) diff --git a/tests/Services/AlphaVantageServiceTests.cs b/tests/Services/AlphaVantageServiceTests.cs index c6bebee..9e3664f 100644 --- a/tests/Services/AlphaVantageServiceTests.cs +++ b/tests/Services/AlphaVantageServiceTests.cs @@ -70,11 +70,14 @@ public void Constructor_Throws() _mockHttpClientFactory.Object, null, _mockPolicyRegistry.Object)); - Assert.Throws(() => new AlphaVantageService( + + var service = new AlphaVantageService( _mockLogger.Object, _mockHttpClientFactory.Object, _mockOptions.Object, - null)); + null); + + Assert.That(service, Is.Not.Null); } [Test]