/// <summary> /// Get Platform Resource from PlatformService directly. /// </summary> /// <typeparam name="TResource">The type of related platform resource.</typeparam> /// <param name="requestUri">The request uri for the related platform resource.</param> /// <param name="loggingContext">The logging context.</param> /// <returns>The related platform resource.</returns> protected async Task <TResource> GetLatestPlatformServiceResourceAsync <TResource>( Uri requestUri, LoggingContext loggingContext) where TResource : Resource { LoggingContext localLoggingContext = loggingContext == null ? new LoggingContext() : loggingContext.Clone() as LoggingContext; var customerHeaders = new Dictionary <string, string>(); Logger.Instance.Information("calling GetLatestPlatformServiceResourceAsync " + requestUri.ToString() + "\r\n" + localLoggingContext.ToString()); localLoggingContext.PropertyBag[Constants.RemotePlatformServiceUri] = requestUri; if (!string.IsNullOrEmpty(loggingContext?.JobId)) { customerHeaders.Add(Constants.UcapClientRequestId, loggingContext.JobId); } DateTime startTime = DateTime.UtcNow; var result = default(TResource); try { var httpResponse = await RestfulClient.GetAsync(requestUri, customerHeaders).ConfigureAwait(false); localLoggingContext.FillTracingInfoFromHeaders(httpResponse.Headers, false); if (httpResponse.IsSuccessStatusCode || (httpResponse.StatusCode == HttpStatusCode.NotFound && !string.IsNullOrWhiteSpace(loggingContext.PlatformResponseCorrelationId) && !string.IsNullOrWhiteSpace(loggingContext.PlatformResponseServerFqdn))) { if (httpResponse.StatusCode != HttpStatusCode.NoContent && httpResponse.StatusCode != HttpStatusCode.NotFound && httpResponse.Content != null) { var platformResourceStream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); result = MediaTypeFormattersHelper.ReadContentWithType(typeof(TResource), httpResponse.Content.Headers.ContentType, platformResourceStream) as TResource; } } else { var serverSideEx = await RemotePlatformServiceException.ConvertToRemotePlatformServiceExceptionAsync(httpResponse, loggingContext).ConfigureAwait(false); if (serverSideEx != null) { throw serverSideEx; } } } catch (Exception ex) { Logger.Instance.Error(ex, "calling GetLatestPlatformServiceResourceAsync " + requestUri.ToString() + " ERROR\r\n" + localLoggingContext.ToString()); throw; } return(result); }
/// <summary> /// The action when receive callback. /// </summary> /// <returns>The task.</returns> private void OnReceivedCallback(object sender, EventsChannelArgs events) { SerializableHttpRequestMessage httpMessage = events.CallbackHttpRequest; if (httpMessage == null) { return; } if (Logger.Instance.HttpRequestResponseNeedsToBeLogged) { //Technically we should not wait a task here. However, this is in event channel call back thread, and we should not use async in event channel handler LogHelper.LogProtocolHttpRequestAsync(httpMessage, httpMessage.LoggingContext != null ? httpMessage.LoggingContext.TrackingId.ToString() : string.Empty, true).Wait(1000); } EventsEntity eventsEntity = null; using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(httpMessage.Content))) { MediaTypeHeaderValue typeHeader = null; MediaTypeHeaderValue.TryParse(httpMessage.ContentType, out typeHeader); if (typeHeader != null) { try { eventsEntity = MediaTypeFormattersHelper.ReadContentWithType(typeof(EventsEntity), typeHeader, stream) as EventsEntity; } catch (Exception ex) { Logger.Instance.Error(ex, "exception happened in deserialzie http message to EventsEntity"); return; } } else { Logger.Instance.Error("Invalid type header in callback message, skip this message! type header value: " + httpMessage.ContentType); return; } } if (eventsEntity != null && eventsEntity.Senders != null && eventsEntity.Senders.Count > 0) //Ignore heartbeat events or call back reachability check events { this.ProcessEvents(new EventsChannelContext(eventsEntity, httpMessage.LoggingContext)); } }
public static EventsEntity GetEventsEntityForEventsInFile(string filename) { InitializeTokenMapper(); EventsEntity eventsEntity = null; string jsonContent = File.ReadAllText("Data\\" + filename); if (!TestHelper.IsInternalApp) { jsonContent = jsonContent.Replace(Constants.DefaultResourceNamespace, Constants.PublicServiceResourceNamespace); } MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonContent)); MediaTypeHeaderValue typeHeader = null; MediaTypeHeaderValue.TryParse("application/json", out typeHeader); eventsEntity = MediaTypeFormattersHelper.ReadContentWithType(typeof(EventsEntity), typeHeader, stream) as EventsEntity; return(eventsEntity); }
public async Task <AdhocMeetingResource> GetAdhocMeetingResourceAsync(LoggingContext loggingContext, AdhocMeetingInput input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } Logger.Instance.Information("calling Application.RefreshAndInitializeAsync"); await this.RefreshAsync(loggingContext).ConfigureAwait(false); AdhocMeetingResource adhocMeetingResource = null; string href = PlatformResource?.OnlineMeetings?.SelfUri; if (href == null) { throw new CapabilityNotAvailableException("Link to create adhoc meeting is not available."); } Logger.Instance.Information("Start to fetching adhocMeetingResource"); var url = UriHelper.CreateAbsoluteUri(this.BaseUri, href); var httpResponse = await this.PostRelatedPlatformResourceAsync(url, input, new ResourceJsonMediaTypeFormatter(), loggingContext).ConfigureAwait(false); try { var skypeResourceStream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); adhocMeetingResource = MediaTypeFormattersHelper.ReadContentWithType(typeof(AdhocMeetingResource), httpResponse.Content.Headers.ContentType, skypeResourceStream) as AdhocMeetingResource; } catch (Exception ex) { Logger.Instance.Error("Failed to diserialize anon token "); throw new RemotePlatformServiceException("Not get valid AdhocMeetingResource from server, deserialize failure.", ex); } return(adhocMeetingResource); }
// All obsolete methods will be removed when releasing 1.0.0 // We are keeping methods for prerelease as we don't want to break our partners every week :) public async Task <AnonymousApplicationTokenResource> GetAnonApplicationTokenAsync(LoggingContext loggingContext, AnonymousApplicationTokenInput input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } Logger.Instance.Information("Calling Application.RefreshAsync"); await this.RefreshAsync(loggingContext).ConfigureAwait(false); Logger.Instance.Information("Start to fetching anonToken"); string href = PlatformResource?.AnonymousApplicationTokens?.Href; if (href == null) { throw new CapabilityNotAvailableException("Link to get anonymous tokens is not available."); } Uri url = UriHelper.CreateAbsoluteUri(this.BaseUri, href); HttpResponseMessage httpResponse = await this.PostRelatedPlatformResourceAsync(url, input, new ResourceJsonMediaTypeFormatter(), loggingContext).ConfigureAwait(false); try { //Does it neccessary to create a helper class from Common layer to do deserialize? Stream platformResourceStream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); return(MediaTypeFormattersHelper.ReadContentWithType(typeof(AnonymousApplicationTokenResource), httpResponse.Content.Headers.ContentType, platformResourceStream) as AnonymousApplicationTokenResource); } catch (Exception ex) { Logger.Instance.Error("Failed to diserialize anon token "); throw new RemotePlatformServiceException("Not get valid anon token resource from server, deserialize failure.", ex); } }