//Testing stuff. private async void Launch_Click(object sender, RoutedEventArgs e) { string resp = null; //string href = "https://api.teamsnap.com/v3/me"; string href = RunAPI.Text; String temp_token = ApplicationData.Current.LocalSettings.Values["Tokens"] as String; if (!String.IsNullOrEmpty(temp_token)) { Token.Text = "already exists " + temp_token; resp = await LibFunctions.apiCall(temp_token, href); APICallResult.Text = "Response ::: " +resp; Debug.WriteLine("Trying to serialize back"); } else { Token.Text = "No token available"; string token = await LibFunctions.GetAuth(); Token.Text = "Token : " + token; resp = await LibFunctions.apiCall(temp_token, href); } try { aboutMe = Me.meDeserializer(resp); FirstName.Text = "Teams href:"+ aboutMe.collection.items[0].links[1].href; MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObjectMe)); ser.WriteObject(stream1, aboutMe); stream1.Position = 0; StreamReader sr = new StreamReader(stream1); Debug.WriteLine("serializing to json"); //Debug.WriteLine(sr.ReadToEnd()); SerializeOutput.Text = "aboutme in JSON :"+ sr.ReadToEnd(); } catch (Exception) { } try { //resp = await LibFunctions.apiCall(temp_token, FirstName.Text); aboutTeam = Teams.teamDeserializer(resp); //TeamsAPI.Text = "Response of teams call : " + resp; } catch (Exception) { } }
/* When sign in button is clicked 1. Get access token. 2. Make call to me API. 3. Deserialize the response and save it 4. Make call to teamsAPI. 5. Store the returned response (=resp) to a file with the same name (teamsAPI). 6. Desearilize resp to object of type RootObjectTeams. 7. Generate TeamData from that object. 8. Serialize TeamData to string 9. Save to file of the same name (TeamData) I wrote this method to fetch all the data I need for the lifecycle of the app when sign-in occurs and store it to a file/object etc. */ private async void SignIn_Click(object sender, RoutedEventArgs e) { string access_token = await libFunctions.GetAuth(); //getting access token. string resp = null; string href = (string)ApplicationData.Current.LocalSettings.Values["meAPI"]; Debug.WriteLine(access_token); try { //callMeAPI(); resp = await libFunctions.apiCall(access_token, href); // 1. calling meAPI Debug.WriteLine(resp); aboutMe = Me.meDeserializer(resp); // 2. deserializing the response saveMe(); //callSportsAPI() href = (string)ApplicationData.Current.LocalSettings.Values["sportAPI"]; resp = await libFunctions.apiCall(access_token, href); var sportsroot = Sports.sportsDeserializer(resp); //callTeamsAPI(); // 3. saving some parts of the response to local settings. the ones I might need most often are being put in here. href = (string)ApplicationData.Current.LocalSettings.Values["teamsAPI"]; Debug.WriteLine(href); resp = await libFunctions.apiCall(access_token, href); // 4. calling teamsAPI // 5. Writing to teamsAPI file (in next line) await libFunctions.writeToFileAsync(resp, "teamsAPI"); // This would branch off to a separate thread. And the rest of the code would continue. it would return from here much later and would update nothing. So I made it await here. var teamsRootObject = Teams.teamDeserializer(resp); // 6. Deserialize resp to object of type RootObjectTeams. var teamsItems = teamsRootObject.collection.items; tdList = new List<TeamData>(); // creating a new list to hold all the teams. rootTeamDataObject = new RootTeamData(); TeamData teamDataObject; // 7. This list will hold the TeamData objects. foreach (var team in teamsItems) { teamDataObject = new TeamData(team, sportsroot); tdList.Add(teamDataObject); } rootTeamDataObject.RootCollection = tdList; MemoryStream stream2 = new MemoryStream(); // 8. Serialize the result to a string. DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(RootTeamData)); ser2.WriteObject(stream2, rootTeamDataObject); stream2.Position = 0; StreamReader sr2 = new StreamReader(stream2); string jsonstring2 = Encoding.UTF8.GetString(stream2.ToArray()); await libFunctions.writeToFileAsync(jsonstring2, "TeamData"); // 9. Saving jsonstring to file. addLinkToLocalSettings(teamsRootObject, "sport"); } catch (Exception) { } SigninPage.Navigate(typeof(DashBoard)); }