static async Task Main(string[] args) { const int oneArgFromConsole = 1; const int twoArgFromConsole = 2; const string configFile = "appsettings.json"; var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(configFile); var configuration = builder.Build(); var apiKey = configuration["ApiKey"]; var asteroidRequest = new AsteroidRequest(); asteroidRequest.StartDate = configuration["NeoWs:StartDate"]; asteroidRequest.EndDate = configuration["NeoWs:EndDate"]; int resultCount = 0; //apiKey = ""; if (args.Length == twoArgFromConsole && args[0] == "apod" && CheckStringForPositiveNumber(args[1], out resultCount)) { await StartApodAsync(apiKey, resultCount); } else if ((args.Length == oneArgFromConsole || args.Length == twoArgFromConsole && CheckStringForPositiveNumber(args[1], out resultCount)) && args[0] == "neows") { await StartNeoWsAsync(apiKey, resultCount, args, asteroidRequest); } else { Console.WriteLine("Incorrect input arguments"); return; } }
public async Task <AsteroidLookup[]> GetAsync(AsteroidRequest request) { var response = await HttpGetAsync <ApiResponse>( $"{ApiUrl}/feed" + $"?start_date={request.StartDate:yyyy-MM-dd}" + $"&end_date={request.EndDate:yyyy-MM-dd}" + $"&api_key={ApiKey}"); List <AsteroidInfo> asteroids = response.NearEarthObjects .SelectMany(asteroid => asteroid.Value) .OrderBy(asteroid => asteroid.Kilometers) .Take(request.ResultCount) .ToList(); return(await Task.WhenAll(asteroids.Select(asteroid => HttpGetAsync <AsteroidLookup>($"{ApiUrl}/neo/{asteroid.Id}?api_key={ApiKey}")))); }
static async Task <bool> StartNeoWsAsync(string apiKey, int resultCount, string[] args, AsteroidRequest asteroidRequest) { if (args.Length == 2) { asteroidRequest.ResultCount = resultCount; } else if (args.Length == 1) { asteroidRequest.ResultCount = -1; } var neoWsClient = new NeoWsClient(apiKey); var asteroid = await neoWsClient.GetAsync(asteroidRequest); if (asteroid != null) { DisplayAsteroid(asteroid); return(false); } return(true); }
public static async Task Main(string[] args) { CultureInfo.CurrentCulture = new CultureInfo("en-GB", false); try { if (args.Length < 1 || (args[0] != "apod" && args[0] != "neows")) { ThrowOnInvalidInput(); } _config = new ConfigurationBuilder() .AddJsonFile(ConfigFile) .Build(); string apiKey = _config["ApiKey"]; if (args[0] == "apod") { if (args.Length < 2) { ThrowOnInvalidInput(); } if (!int.TryParse(args[1], out int count)) { ThrowOnInvalidInput(); } var nasaClient = new ApodClient(apiKey); MediaOfToday[] res = await nasaClient.GetAsync(count); foreach (MediaOfToday media in res) { Console.WriteLine($"{media}{Environment.NewLine}"); } } else { string startDate = _config["NeoWs:StartDate"]; string endDate = _config["NeoWs:EndDate"]; AsteroidRequest request; if (args.Length < 2 || !int.TryParse(args[1], out int count)) { request = new AsteroidRequest(startDate, endDate); } else { request = new AsteroidRequest(startDate, endDate, count); } var nasaClient = new NeoWsClient(apiKey); AsteroidLookup[] res = await nasaClient.GetAsync(request); foreach (AsteroidLookup asteroid in res) { Console.WriteLine($"- {asteroid}{Environment.NewLine}"); } } } catch (Exception e) { Console.WriteLine(e.Message); } }