static async private Task <MatrixRequestResult> getAsync(string uri, string query = "") { string uri_str; if (query != null) { query = (query.Length == 0 ? "" : $"&{query}"); uri_str = $"{uri}?t={rand.Next()}{query}"; } else { uri_str = $"{uri}"; } var obj = await json_req.getAsync(new Uri(uri_str)); var result = new MatrixRequestResult(obj); switch (result.status) { case "UNKNOWN_ERROR": throw new MatrixException.ServerError(); case "NOT_AUTHORIZED": throw new MatrixException.NotLogin(); default: break; } return(result); }
static async public Task <bool> logout() { MatrixRequestResult result = null; result = await PostAsync($"{root}/api/users/logout", new JObject()); return(result.success); }
static async public Task <bool> IsLogin() { MatrixRequestResult result = null; result = await GetAsync($"{root}/api/users/login"); return(result.success); }
static async public Task <bool> logout() { MatrixRequestResult result = null; try { result = await postAsync($"{root}/api/users/logout", new JObject()); } catch (MatrixException.FatalError err) { throw err; } return(result.success); }
static async public Task <bool> isLogin() { MatrixRequestResult result = null; try { result = await getAsync($"{root}/api/users/login"); } catch (MatrixException.FatalError err) { throw err; } return(result.success); }
static async public Task <ObservableCollection <T> > getListAsync <T> (string uri, string query = "") { MatrixRequestResult result = await getAsync(uri, query); if (result.success) { JArray arr = result.data as JArray; ObservableCollection <T> ret = new ObservableCollection <T>(); foreach (JObject one in arr) { // 软爸爸的C#泛型类不能初始化,实在是很蛋疼啊。 // 还好软爸爸留了一条退路。 ret.Add((T)Activator.CreateInstance(typeof(T), one)); } return(ret); } throw new MatrixException.SoftError(result); }
static async private Task <MatrixRequestResult> postAsync(string uri, JObject body) { var obj = await json_req.postAsync(new Uri(uri), body); var result = new MatrixRequestResult(obj); switch (result.status) { case "UNKNOWN_ERROR": throw new MatrixException.ServerError(); case "NOT_AUTHORIZED": throw new MatrixException.NotLogin(); default: break; } return(result); }
static async public Task <List <T> > GetListAsync <T>(string uri, string query = "") where T : class { MatrixRequestResult result = await GetAsync(uri, query); if (result.success) { JArray arr = result.data as JArray; List <T> ret = new List <T>(); foreach (JObject one in arr) { // 软爸爸的C#泛型类不能初始化,实在是很蛋疼啊。 // 还好软爸爸留了一条退路。 // Waiting for proposal: Extend generic type new() constraint with parameter types. https://github.com/dotnet/csharplang/issues/769 ret.Add(Activator.CreateInstance(typeof(T), one) as T); } return(ret); } throw new MatrixException.SoftError(result); }