public static async Task <HttpResponseMessage> SendRequestAsync(HttpMethod httpMethod, string uri, string optionalPayload, int timeout, string jwt = null, bool isOnlineCheck = false) { if (!SoftwareCoUtil.isTelemetryOn()) { return(null); } if (!isOnlineCheck && !SoftwareUserSession.isOnline) { return(null); } HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(timeout) }; var cts = new CancellationTokenSource(); HttpResponseMessage response = null; if (jwt == null) { object jwtObj = SoftwareUserSession.GetJwt(); if (jwtObj != null) { jwt = (string)jwtObj; } } if (jwt != null) { // add the authorizationn client.DefaultRequestHeaders.Add("Authorization", jwt); } HttpContent contentPost = null; try { if (optionalPayload != null) { contentPost = new StringContent(optionalPayload, Encoding.UTF8, "application/json"); } } catch (Exception e) { NotifyPostException(e); } bool isPost = (httpMethod.Equals(HttpMethod.Post)); try { string endpoint = Constants.api_endpoint + "" + uri; if (isPost) { response = await client.PostAsync(endpoint, contentPost, cts.Token); } else { response = await client.GetAsync(endpoint, cts.Token); } } catch (HttpRequestException e) { if (isPost) { NotifyPostException(e); } } catch (TaskCanceledException e) { if (e.CancellationToken == cts.Token) { // triggered by the caller if (isPost) { NotifyPostException(e); } } else { // a web request timeout (possibly other things!?) Logger.Info("We are having trouble receiving a response from Software.com"); } } catch (Exception e) { if (isPost) { NotifyPostException(e); } } finally { } return(response); }
// This method is called by the timer delegate. private async void ProcessSoftwareDataTimerCallbackAsync(Object stateInfo) { AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; double offset = 0; long end = 0; long local_end = 0; DateTime now = DateTime.UtcNow; if (_softwareData != null && _softwareData.HasData() && (EnoughTimePassed(now) || timer == null)) { offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes; _softwareData.local_start = _softwareData.start + ((int)offset * 60); _softwareData.offset = Math.Abs((int)offset); if (TimeZone.CurrentTimeZone.DaylightName != null && TimeZone.CurrentTimeZone.DaylightName != TimeZone.CurrentTimeZone.StandardName) { _softwareData.timezone = TimeZone.CurrentTimeZone.DaylightName; } else { _softwareData.timezone = TimeZone.CurrentTimeZone.StandardName; } foreach (KeyValuePair <string, object> sourceFiles in _softwareData.source) { JsonObject fileInfoData = null; fileInfoData = (JsonObject)sourceFiles.Value; object outend; fileInfoData.TryGetValue("end", out outend); if (long.Parse(outend.ToString()) == 0) { end = SoftwareCoUtil.getNowInSeconds(); offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes; local_end = end + ((int)offset * 60); _softwareData.addOrUpdateFileInfo(sourceFiles.Key, "end", end); _softwareData.addOrUpdateFileInfo(sourceFiles.Key, "local_end", local_end); } } try { end = SoftwareCoUtil.getNowInSeconds(); offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes; local_end = end + ((int)offset * 60); _softwareData.end = end; _softwareData.local_end = local_end; } catch (Exception) { } string softwareDataContent = _softwareData.GetAsJson(); Logger.Info("Code Time: sending: " + softwareDataContent); if (SoftwareCoUtil.isTelemetryOn()) { StorePayload(_softwareData); // call the kpm summary /* try * { * Thread.Sleep(1000 * 5); * ProcessFetchDailyKpmTimerCallbackAsync(null); * } * catch (ThreadInterruptedException e) * { * // * }*/ } else { Logger.Info("Code Time metrics are currently paused."); // this.StorePayload(softwareDataContent); } _softwareData.ResetData(); _lastPostTime = now; } }