Class ApodClient
A client for interfacing with NASA's Astronomy Picture of the Day API.
Namespace: Apod
Assembly: Apod.dll
Syntax
public class ApodClient : object, IApodClient, IDisposable
Remarks
Remember to call Dispose() on the client after you're done using it. Read https://github.com/LeMorrow/APOD.Net#disposing-the-client to learn more.
NASA's Astronomy Picture of the Day API can be found at https://api.nasa.gov#apod.
Constructors
| Improve this Doc View SourceApodClient()
Creates a new instance of an Astronomy Picture of the Day client, using the demo API key.
Declaration
public ApodClient()
Remarks
The API key "DEMO_KEY" has an hourly limit of 30 requests per IP adress and a daily limit of 50 requests per IP address. To prevent rate limiting, it is recommended to sign up for your own API key at https://api.nasa.gov and use the other constructor.
Examples
var client = new ApodClient();
|
Improve this Doc
View Source
ApodClient(String)
Creates a new instance of an Astronomy Picture of the Day client.
Declaration
public ApodClient(string apiKey)
Parameters
Type | Name | Description |
---|---|---|
String | apiKey | Your API key from https://api.nasa.gov. |
Examples
var client = new ApodClient("YOUR_API_KEY_HERE");
|
Improve this Doc
View Source
ApodClient(String, IHttpRequester, IHttpResponseParser, IErrorHandler)
Creates a new instance of an Astronomy Picture of the Day client, overriding the internal logic.
Declaration
public ApodClient(string apiKey, IHttpRequester httpRequester = null, IHttpResponseParser httpResponseParser = null, IErrorHandler errorHandler = null)
Parameters
Type | Name | Description |
---|---|---|
String | apiKey | Your API key from https://api.nasa.gov. |
IHttpRequester | httpRequester | The IHttpRequester to use for interacting with the API. |
IHttpResponseParser | httpResponseParser | The IHttpResponseParser to use for parsing the data from the |
IErrorHandler | errorHandler | The IErrorHandler to handle any errors with the request. |
Remarks
This constructor can be used for overriding internal logic in the client. The average user should not need to call this constructor.
Examples
See Override the IErrorHandler for an example on how to implement and use any of these interfaces.
Methods
| Improve this Doc View SourceDispose()
Releases the unmanaged resources and disposes of the managed resources used by the
Declaration
public void Dispose()
Remarks
Remember to call this method once you are done using the client.
Read https://github.com/LeMorrow/APOD.Net#disposing-the-client to learn more.
FetchApodAsync()
Fetch the Astronomy Picture of the Day for today's date.
Declaration
public ValueTask<ApodResponse> FetchApodAsync()
Returns
Type | Description |
---|---|
ValueTask<ApodResponse> | The response. |
Remarks
"Today's date" refers to the current date in the Eastern Time Zone. Read more: https://github.com/LeMorrow/APOD.Net#when-do-new-apods-get-published-by-nasa.
Examples
using var client = new ApodClient();
var response = await client.FetchApodAsync();
if (response.StatusCode != ApodStatusCode.OK) { return; }
Console.WriteLine(response.Content.Date);
Console.WriteLine(response.Content.Title);
|
Improve this Doc
View Source
FetchApodAsync(DateTime)
Fetch the Astronomy Picture of the Day for a specific date.
Declaration
public ValueTask<ApodResponse> FetchApodAsync(DateTime dateTime)
Parameters
Type | Name | Description |
---|---|---|
DateTime | dateTime | The date to request the APOD for. Must be between June 16th 1995 and today's date (inclusive). |
Returns
Type | Description |
---|---|
ValueTask<ApodResponse> | The response. |
Remarks
You do not need to consider time zone differences between your application and NASA's API.
If dateTime
has today's date (according to your local time zone), the method will take care of it accordingly.
Note that this means that if you are ahead of the Eastern Time Zone and ask for today's date after midnight you would get an APOD with (for you) yesterday's date, since that is the most recent one.
Examples
using var client = new ApodClient();
var date = new DateTime(2004, 04, 09);
var response = await client.FetchApodAsync(date);
if (response.StatusCode != ApodStatusCode.OK) { return; }
var content = response.Content;
Console.WriteLine(content.Date);
Console.WriteLine(content.Title);
|
Improve this Doc
View Source
FetchApodAsync(DateTime, DateTime)
Fetch all the Astronomy Pictures of the Day between two dates (inclusive).
Declaration
public ValueTask<ApodResponse> FetchApodAsync(DateTime startDate, DateTime endDate = null)
Parameters
Type | Name | Description |
---|---|---|
DateTime | startDate | The start date. Must be between June 16th 1995 and today's date (inclusive). |
DateTime | endDate | The end date. Must be between the |
Returns
Type | Description |
---|---|
ValueTask<ApodResponse> | The response. |
Remarks
You do not need to consider time zone differences between your application and NASA's API.
If endDate
has today's date (according to your local time zone), the method will take care of it accordingly.
Note that this means that if you are ahead of the Eastern Time Zone and ask for all APODs between yesterday and today just after midnight (locally), you would only get one APOD with (for you) yesterday's date, since a picture for your "today" doesn't exist yet.
It is therefore not possible to safely assume the amount of APODs this method will return, since it depends on time zone differences and the current time.
Examples
using var client = new ApodClient();
var startDate = new DateTime(2008, 10, 29);
var endDate = new DateTime(2008, 11, 02);
var response = await client.FetchApodAsync(startDate, endDate);
if (response.StatusCode != ApodStatusCode.OK) { return; }
foreach (var apod in response.AllContent)
{
Console.WriteLine($"{apod.Date}: {apod.Title}");
}
|
Improve this Doc
View Source
FetchApodAsync(Int32)
Fetch an amount of random Astronomy Pictures of the Day.
Declaration
public ValueTask<ApodResponse> FetchApodAsync(int count)
Parameters
Type | Name | Description |
---|---|---|
Int32 | count | The amount of APODs to fetch. Must be positive and cannot exceed 100. |
Returns
Type | Description |
---|---|
ValueTask<ApodResponse> | The response. |
Examples
using var client = new ApodClient();
var amount = 3;
var response = await client.FetchApodAsync(amount);
if (response.StatusCode != ApodStatusCode.OK) { return; }
foreach (var apod in response.AllContent)
{
Console.WriteLine($"{apod.Date}: {apod.Title}");
}
|
Improve this Doc
View Source
GetPermalink(ApodContent)
Get the apod.nasa.gov permalink for an Astronomy Picture of the Day. Example response: https://apod.nasa.gov/apod/ap191208.html.
Declaration
public string GetPermalink(ApodContent apodContent)
Parameters
Type | Name | Description |
---|---|---|
ApodContent | apodContent | The Astronomy Picture of the Day to get the permalink for. |
Returns
Type | Description |
---|---|
String | The permalink URL to this APOD. |