/// <summary> /// Attempts to send a request to logging server asynchronously /// </summary> /// <param name="newLog">A BaseLogDTO object that will be sent</param> /// <returns>Returns the status code of the request</returns> public async Task <System.Net.HttpStatusCode> sendLogAsync(BaseLogDTO newLog) { string tempURL = getURL(newLog); try { using (var client = new HttpClient()) { //Must await to check if it should attempt to retry request if the current request failed var result = await client.PostAsync(tempURL, getLogContent(newLog)).ConfigureAwait(false); int attempt = 1; while (!result.IsSuccessStatusCode) { result = await client.PostAsync(tempURL, getLogContent(newLog)).ConfigureAwait(false); attempt++; if (attempt >= 100) { return(result.StatusCode); } } return(result.StatusCode); } } catch (System.AggregateException) { return(System.Net.HttpStatusCode.BadRequest); } catch (Exception) { return(System.Net.HttpStatusCode.InternalServerError); } }
/// <summary> /// Attempts to send a request to logging server synchronously /// </summary> /// <param name="newLog">A BaseLogDTO object that will be sent</param> /// <returns>Returns the status code of the request</returns> public System.Net.HttpStatusCode sendLogSync(BaseLogDTO newLog) //Sends a log synchronously { string tempURL = getURL(newLog); try { using (var client = new HttpClient()) { var result = client.PostAsync(tempURL, getLogContent(newLog)).Result; //.Result make it synchronous int attempt = 1; while (!result.IsSuccessStatusCode) //Will resend if request fails up to 100 times { //Must reinitalize logContent everytime a post attempt is made result = client.PostAsync(tempURL, getLogContent(newLog)).Result; attempt++; if (attempt >= 100) { return(result.StatusCode); } } return(result.StatusCode); } } catch (Exception) { return(System.Net.HttpStatusCode.InternalServerError); } }
/// <summary> /// Takes the content inside the BaseLogDTO derived object and converts to json /// </summary> /// <param name="newLog">A BaseLogDTO object that will be converted</param> /// <returns>A StringContent containing the stringified json</returns> public StringContent getLogContent(BaseLogDTO newLog) { var jsonContent = new JavaScriptSerializer().Serialize(newLog); var content = new StringContent(jsonContent.ToString(), Encoding.UTF8, "application/json"); return(content); }
/// <summary> /// Sends a derivative object of BaseLogDTO to the logging server synchronously /// </summary> /// <param name="newLog">A derivative of the BaseLogDTO object</param> /// <returns>A boolean whether the object passed was a valid object</returns> public bool sendLogSync(BaseLogDTO newLog) { newLog = getContent(newLog); if (newLog.isValid()) { var responseStatusCode = _ls.sendLogSync(newLog); _ls.notifyAdmin(responseStatusCode, _ls.getLogContent(newLog)); } return(newLog.isValid()); }
/// <summary> /// Sends a derivative object of BaseLogDTO to the logging server asynchronously /// </summary> /// <param name="newLog">A derivative of the BaseLogDTO object</param> /// <returns>A boolean whether the object passed was a valid object</returns> public async Task <bool> sendLogAsync(BaseLogDTO newLog) { newLog = getContent(newLog); if (newLog.isValid()) //Only send if the object it validated { var responseStatusCode = await _ls.sendLogAsync(newLog); _ls.notifyAdmin(responseStatusCode, _ls.getLogContent(newLog)); } return(newLog.isValid()); }
/// <summary> /// Retrieves and appends authentication content required for all derivatives of BaseLogDTO objects /// </summary> /// <param name="newLog">A derivative of the BaseLogDTO object</param> /// <returns>The appended BaseLogDTO object</returns> private BaseLogDTO getContent(BaseLogDTO newLog) { string timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString(); string salt = _ls.GetSalt(); string plaintext = "timestamp=" + timestamp + ";salt=" + salt; string signature = _ls.GenerateSignature(plaintext); newLog.timestamp = timestamp; newLog.salt = salt; newLog.signature = signature; return(newLog); }
/// <summary> /// Helper function which returns the correct url based on the object type of newLog /// </summary> /// <param name="newLog">A BaseLogDTO derived object</param> /// <returns>The URL which the HTTP client should send a POST request to</returns> private string getURL(BaseLogDTO newLog) { string tempURL; if (newLog.GetType().Equals(typeof(LogRequestDTO))) { tempURL = LOG_POST_URL; } else { tempURL = ERROR_POST_URL; } return(tempURL); }