using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using isn.Abstract;

namespace isn
{
    public static class SourceHelpers
    {
        public static ApiIndexViewModel GetServerResources(string url)
        {
            HttpClient client = new HttpClient();
            ApiIndexViewModel result = null;
            // var json = await client.GetStringAsync(new System.Uri(url));
            
            Task.Run(async ()=> {
                try {
                var response = await client.GetStringAsync(url);
                    result = JsonConvert.DeserializeObject<ApiIndexViewModel>(response);

                } catch (HttpRequestException ex)
                {
                    if (ex.StatusCode==HttpStatusCode.NotFound)
                    {
                        Console.Error.WriteLine("Not found ... server's down ?");
                    }
                    else
                    {
                        Console.Error.WriteLine($"{ex.StatusCode} : {ex.Message}");
                    }
                }
            }).Wait();
          
            return result;
        }
    }
}