Skip to content
Open

1 #3

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 68 additions & 20 deletions Lesson04/FetchApi/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Net.Http;
using System.Text;
Expand All @@ -26,17 +27,15 @@ private async void Button_Click(object sender, RoutedEventArgs e)
string country = inputTextBox.Text;

try
{
{
Loader.Text = "Loading...";

var list =await FetchUniversities(country);

var list = await FetchUniversities(country).ConfigureAwait(false);

Thread.Sleep(2000);
await SaveUniversitiesFile(list);

Dispatcher.Invoke(() =>
{
dataGrid.ItemsSource = list;
});
dataGrid.ItemsSource = list;

}
catch (Exception ex)
{
Expand All @@ -53,21 +52,25 @@ private async void Button_Click(object sender, RoutedEventArgs e)

private async Task<List<University>> FetchUniversities(string country)
{
HttpClient client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"http://universities.hipolabs.com/search?country={country}")
{
Content = new StringContent("", Encoding.UTF8, "application/json")
};

var response = await client.SendAsync(request);
using var streamReader = new StreamReader(response.Content.ReadAsStream());
List<University> result = new List<University>();
//ThreadPool.SetMaxThreads(5, 5);
//ThreadPool.QueueUserWorkItem(state =>
//{
HttpClient client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"http://universities.hipolabs.com/search?country={country}")
{
Content = new StringContent("", Encoding.UTF8, "application/json")
};

var result = JsonConvert.DeserializeObject<List<University>>(streamReader.ReadToEnd());
var response = await client.SendAsync(request);
using var streamReader = new StreamReader(response.Content.ReadAsStream());

result = JsonConvert.DeserializeObject<List<University>>(streamReader.ReadToEnd());
//});
return result;
}

private async Task<CoinDesk> FetchCoinAsync()
private async Task<CoinDesk> FetchCoin()
{
var client = new HttpClient();

Expand All @@ -76,7 +79,7 @@ private async Task<CoinDesk> FetchCoinAsync()
Content = new StringContent("", Encoding.UTF8, "application/json")
};

var response = await client.SendAsync(webRequest);
var response =await client.SendAsync(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string json = reader.ReadToEnd();
Expand All @@ -92,9 +95,11 @@ private async void Button_Click_1(object sender, RoutedEventArgs e)
{
Loader1.Text = "Loading...";

var result = await FetchCoinAsync();
var result = await FetchCoin();
coinDesks.Add(result);

await SaveFile();

dataGrid1.ItemsSource = null;
dataGrid1.ItemsSource = coinDesks;
}
Expand All @@ -107,6 +112,49 @@ private async void Button_Click_1(object sender, RoutedEventArgs e)
Loader1.Text = "Finished loading";
}
}
private async Task SaveFile()
{
string path = (Directory.GetParent(Directory.GetCurrentDirectory()).Parent).FullName +
"\\Coindeskdata.txt";

if(!File.Exists(path))
{
File.Create(path).Close();
}

using StreamWriter sw = new(path,true);

string json = JsonConvert.SerializeObject(coinDesks);

await sw.WriteAsync(json).ConfigureAwait(true);
await sw.WriteAsync(json).ConfigureAwait(true);

sw.WriteLine();
sw.WriteLine(DateTime.Now);
sw.WriteLine();
}
private async Task SaveUniversitiesFile(List<University>list)
{
string path = (Directory.GetParent(Directory.GetCurrentDirectory()).Parent).FullName +
"\\dataUniversities.txt";

if (!File.Exists(path))
{
File.Create(path).Close();
}

using StreamWriter sw = new(path,true);

string json = JsonConvert.SerializeObject(list);

await sw.WriteAsync(json).ConfigureAwait(true);

sw.WriteLine();
sw.WriteLine(DateTime.Now);
sw.WriteLine();
}


}

class University
Expand Down