using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.Collections.ObjectModel; namespace DeskHubSharp { class Request { // TODO: this is rudimentary, fix it // i.e. try catch statements\ // TODO: find how to access a list index // TODO: find how to access the deserialized private string _apiEndpoint = "https://api.github.com/"; private string _query; public List RepoDetail { get; set; } public Request(string query) { _query = query; } //public Request(string query, Object function) //{ // _query = query; //} /// /// Calls API for repo and user data an stores it in RepoList /// /// public void SearchRequest() { try { var client = new RestClient(_apiEndpoint); RestRequest requestRepo = new RestRequest($"users/{_query}/repos", Method.GET); var response = client.Execute(requestRepo); var x = response.Content; var deserialized = JsonConvert.DeserializeObject>(x); //ObservableCollection test = new ObservableCollection() //{ // new RepoDetail() // { // Login = "John", // Password = "pw" // } //}; if (deserialized.Count() == 0) { throw new Exception(); } else { RepoList.repoDetail = deserialized; } } catch (Exception) { ErrorWindow err = new ErrorWindow(); err.txtblk_error.Text = "We couldn't gather repository data. Please try again"; } } /// /// Calls API for detailed user data /// public void UserRequest() { try { var client = new RestClient(_apiEndpoint); RestRequest requestUser = new RestRequest($"users/{_query}", Method.GET); var response = client.Execute(requestUser); string x = response.Content; var deserailized = JsonConvert.DeserializeObject(x); if (deserailized == null) { throw new Exception(); } else { RepoList.userDetail = deserailized; } } catch (Exception) { ErrorWindow err = new ErrorWindow(); err.txtblk_error.Text = "We couldn't gather user data. Please try again."; } } } }