public string Login(string username, string password) { try { var memberLogin = new MemberLogin() { email = username, password = password }; if (!ValidaTeMemberLogin(memberLogin)) { throw new Exception("login fails!"); } //lấy token từ api var token = GetTokenFromApi(memberLogin); CreateReadFile createReadFile = new CreateReadFile(); createReadFile.SaveTokenToLocalStorage(token); return(token); } catch (Exception e) { Debug.WriteLine(e.Message); return(null); } }
public Song PostSong(Song song) { string token = null; HttpContent content = new StringContent(JsonConvert.SerializeObject(song), Encoding.UTF8, "application/json"); HttpClient httpClient = new HttpClient(); CreateReadFile createReadFile = new CreateReadFile(); token = createReadFile.GetToken(); httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + token); var response = httpClient.PostAsync(ApiUrl.POST_SONG_URL, content).Result.Content.ReadAsStringAsync().Result; Debug.WriteLine(response); return(song); }
public static ObservableCollection <Song> GetMySongs() { string token = null; ObservableCollection <Song> songs = new ObservableCollection <Song>(); CreateReadFile createReadFile = new CreateReadFile(); token = createReadFile.GetToken(); // thực hiện request lên api lấy token về. var client = new HttpClient(); client.DefaultRequestHeaders.Add("Authorization", "Basic " + token); var responseContent = client.GetAsync(ApiUrl.MY_SONG_URL).Result.Content.ReadAsStringAsync().Result; songs = JsonConvert.DeserializeObject <ObservableCollection <Song> >(responseContent); return(songs); }