public static async Task <APIResponse> CheckSession(LocalConfig config) { try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(config.Server + "/api/v1/users/me")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", config.Token); bool rateLimited = false; HttpResponseMessage response = null; while (!rateLimited) { response = await client.SendAsync(request); if ((int)response.StatusCode == 429) { await Task.Delay(1000); } else { rateLimited = true; } } string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { APIErrorResponse error = JsonConvert.DeserializeObject <APIErrorResponse>(responseContent); return(new APIResponse() { Success = false, Error = error.Message }); } APIBaseURL = new Uri(config.Server + "/api/v1/"); Token = config.Token; MyID = config.UserID; Team = LocalStorage.GetByID <Team>("teams", config.TeamID); return(new APIResponse() { Success = true }); } catch (Exception e) { return(new APIResponse() { Success = false, Error = e.Message }); } }
static async Task <APIResponse <T> > MakeAPIRequest <T>(string method, string postData) { try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(APIBaseURL, method)); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token); request.Content = new StringContent(postData, Encoding.UTF8, "application/json"); bool rateLimited = false; HttpResponseMessage response = null; while (!rateLimited) { response = await client.SendAsync(request); if ((int)response.StatusCode == 429) { await Task.Delay(1000); } else { rateLimited = true; } } string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { APIErrorResponse error = JsonConvert.DeserializeObject <APIErrorResponse>(responseContent); return(new APIResponse <T>() { Success = false, Error = error.Message }); } return(new APIResponse <T>() { Success = true, Value = JsonConvert.DeserializeObject <T>(responseContent) }); } catch (Exception e) { return(new APIResponse <T>() { Success = false, Error = e.Message }); } }
public static async Task <APIResponse <Bitmap> > GetAvatar(string userID) { // If avatars contains the userID key, the download is either in progress, or has completed if (avatars.ContainsKey(userID)) { object avatar = avatars[userID]; // If the object is an APIResponse, return it if (avatar is APIResponse <Bitmap> ) { return((APIResponse <Bitmap>)avatar); } else { // If not, its a WaitHandle, so cast it and await it WaitHandle handle = (WaitHandle)avatar; await handle.WaitOneAsync(); return((APIResponse <Bitmap>)avatars[userID]); } } EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset); avatars.Add(userID, ewh); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(APIBaseURL, string.Format("users/{0}/image", userID))); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token); bool rateLimited = false; HttpResponseMessage response = null; while (!rateLimited) { response = await client.SendAsync(request); if ((int)response.StatusCode == 429) { await Task.Delay(1000); } else { rateLimited = true; } } APIResponse <Bitmap> retVal; if (response.StatusCode != HttpStatusCode.OK) { string responseContent = await response.Content.ReadAsStringAsync(); APIErrorResponse error = JsonConvert.DeserializeObject <APIErrorResponse>(responseContent); retVal = new APIResponse <Bitmap>() { Success = false, Error = error.Message }; avatars[userID] = retVal; ewh.Set(); return(retVal); } Bitmap avatar = new Bitmap(await response.Content.ReadAsStreamAsync()); retVal = new APIResponse <Bitmap>() { Success = true, Value = avatar.ApplyMask(Mattermost.Properties.Resources.AvatarMask) }; avatars[userID] = retVal; ewh.Set(); return(retVal); } catch (Exception e) { APIResponse <Bitmap> retVal = new APIResponse <Bitmap>() { Success = false, Error = e.Message }; avatars[userID] = retVal; ewh.Set(); return(retVal); } }
public static async Task <APIResponse> Login(string server, string team, string username, SecureString password) { APIBaseURL = new Uri(server + "/api/v1/"); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(APIBaseURL, "users/login")); Dictionary <string, object> postData = new Dictionary <string, object>() { { "name", team }, { "password", ConvertToString(password) } }; if (Regex.IsMatch(username, @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")) { postData.Add("email", username); } else { postData.Add("username", username); } request.Content = new StringContent(JsonConvert.SerializeObject(postData), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(request); string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { APIErrorResponse error = JsonConvert.DeserializeObject <APIErrorResponse>(responseContent); return(new APIResponse() { Success = false, Error = error.Message }); } JObject array = JsonConvert.DeserializeObject <JObject>(responseContent); MyID = array["id"].ToString(); Token = response.Headers.GetValues("Token").First(); APIResponse <Team> teamObj = await MakeAPIRequest <Team>("teams/me"); if (!teamObj.Success) { return new APIResponse() { Success = false, Error = teamObj.Error } } ; Team = teamObj.Value; return(new APIResponse() { Success = true }); } catch (Exception e) { return(new APIResponse() { Success = false, Error = e.Message }); } }
public static async Task <APIResponse> Login(string server, string team, string username, SecureString password) { APIBaseURL = new Uri(server + "/api/v3/"); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(APIBaseURL, "users/login")); Dictionary <string, object> postData = new Dictionary <string, object>() { { "name", team }, { "password", ConvertToString(password) } }; if (Regex.IsMatch(username, @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")) { postData.Add("email", username); } else { postData.Add("login_id", username); } request.Content = new StringContent(JsonConvert.SerializeObject(postData), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.SendAsync(request); string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { APIErrorResponse error = JsonConvert.DeserializeObject <APIErrorResponse>(responseContent); return(new APIResponse() { Success = false, Error = error.Message }); } JObject array = JsonConvert.DeserializeObject <JObject>(responseContent); MyID = array["id"].ToString(); Token = response.Headers.GetValues("Token").First(); //APIResponse<Team> teamObj = await MakeAPIRequest<Team>("teams/" + "ffeg8g9omfbizjp9urjdbc6x5a" + "/me"); APIResponse <JObject> teamObj = await MakeAPIRequest <JObject>("teams/all"); Team t = new Team(); foreach (KeyValuePair <string, JToken> h in teamObj.Value) { t = JsonConvert.DeserializeObject <Team>(h.Value.ToString()); if (t.DisplayName == team) { break; } } try { LocalStorage.Store <Team>("teams", t); } catch (Exception) { LocalStorage.Update <Team>("teams", t); } if (String.IsNullOrEmpty(t.Name)) { return new APIResponse() { Success = false, Error = teamObj.Error } } ; Team = t; return(new APIResponse() { Success = true }); } catch (Exception e) { return(new APIResponse() { Success = false, Error = e.Message }); } }