MarcusOtter / APOD.Net

An unofficial .NET wrapper for NASA's Astronomy Picture of the Day API.
https://marcusotter.github.io/APOD.Net/
MIT License
9 stars 2 forks source link

Can't call FetchApodAsync more than once #18

Closed MarcusOtter closed 4 years ago

MarcusOtter commented 4 years ago

Describe the bug Like the title says, it is not possible to call any overload of FetchApodAsync twice without running into weird errors or exceptions, which is likely due to http objects not being disposed of correctly.

To Reproduce Steps to reproduce the behavior:

  1. Pick one of the overloads of ApodClient.FetchApodAsync()
  2. Call it twice
  3. See errors (different errors depending on which overload)

Expected behavior No errors.

Environment (please complete the following information):

Additional context Calling FetchApodAsync(DateTime date) or FetchApodAsync(DateTime startDate, DateTime endDate) more than once:

{"code":400,"msg":"unconverted data remains: https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY","service_version":"v1"}

Calling FetchApodAsync(int amount) more than once (where the number before the URL is the first number you passed into amount, in this case 3:

{"code":400,"msg":"invalid literal for int() with base 10: '3https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY'","service_version":"v1"}

Calling FetchApodAsync() more than once:

Unhandled exception. System.ObjectDisposedException: Cannot access a closed Stream.
   at System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.MemoryStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Text.Json.JsonSerializer.ReadAsync[TValue](Stream utf8Json, Type returnType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at Apod.Logic.Net.HttpResponseParser.ParseSingleApodAsync(HttpResponseMessage httpResponse) in C:\Projects\APOD.Net\src\Apod\Logic\Net\HttpResponseParser.cs:line 34
   at Apod.ApodClient.FetchApodAsync() in C:\Projects\APOD.Net\src\Apod\ApodClient.cs:line 47
   at ApodExample.Program.Main() in C:\Projects\APOD.Net\src\ExampleUsage\Program.cs:line 21
   at ApodExample.Program.<Main>()