private EventHandlerResponse GetRedirectionAction( Response <ODataObject> response, HttpResponseMessage responseMessage, HttpRequestMessage httpRequestMessage) { EventHandlerResponse action; var redirection = response.Value as Redirection; // Removed until API is updated to provide this correctly. // !redirection.Available || if (redirection.Uri == null) { throw new ZoneUnavailableException(responseMessage.RequestMessage.RequestUri, "Destination zone is unavailable"); } if (httpRequestMessage.RequestUri.GetAuthority() != redirection.Uri.GetAuthority()) { action = this.ShareFileClient.OnChangeDomain(httpRequestMessage, redirection); } else { action = EventHandlerResponse.Redirect(redirection); } return(action); }
public async void OnDomainChangeRaised(bool async) { // Arrange var shareFileClient = GetShareFileClient(true); ConfigureDomainChangedResponse(); var changeDomainRaised = false; shareFileClient.AddChangeDomainHandler((message, redirect) => { changeDomainRaised = true; ConfigureItemResponse(); return(EventHandlerResponse.Redirect(redirect)); }); var query = shareFileClient.Items.Get(shareFileClient.Items.GetAlias(GetId())); // Act if (async) { await query.ExecuteAsync(); } else { query.Execute(); } // Assert changeDomainRaised.Should().BeTrue(); }
private Response <T> ParseTypedResponse <T>(HttpResponseMessage httpResponseMessage) where T : class { var watch = new ActionStopwatch("ProcessResponse", ShareFileClient.Logging); var responseStream = httpResponseMessage.Content.ReadAsStreamAsync().WaitForTask(); if (responseStream != null) { if (typeof(T).IsSubclassOf(typeof(ODataObject))) { var result = DeserializeStream <ODataObject>(responseStream); LogResponse(result, httpResponseMessage.RequestMessage.RequestUri, httpResponseMessage.Headers.ToString(), httpResponseMessage.StatusCode); ShareFileClient.Logging.Trace(watch); CheckAsyncOperationScheduled(result); //workaround for metadata not returning on Redirections string redirectUri; if (result is ODataObject && result.TryGetProperty("Uri", out redirectUri)) { result = new Redirection { Uri = new Uri(redirectUri) }; } if (result is Redirection && typeof(T) != typeof(Redirection)) { var redirection = result as Redirection; // Removed until API is updated to provide this correctly. // !redirection.Available || if (redirection.Uri == null) { throw new ZoneUnavailableException(httpResponseMessage.RequestMessage.RequestUri, "Destination zone is unavailable"); } if (httpResponseMessage.RequestMessage.RequestUri.GetAuthority() != redirection.Uri.GetAuthority()) { return(Response.CreateAction <T>(ShareFileClient.OnChangeDomain(httpResponseMessage.RequestMessage, redirection))); } else { return(Response.CreateAction <T>(EventHandlerResponse.Redirect(redirection))); } } else if (result is T) { return(Response.CreateSuccess(result as T)); } else { throw new InvalidApiResponseException(httpResponseMessage.StatusCode, "Unable to parse API return to desired type"); } } else { var result = DeserializeStream <T>(responseStream); LogResponse(result, httpResponseMessage.RequestMessage.RequestUri, httpResponseMessage.Headers.ToString(), httpResponseMessage.StatusCode); ShareFileClient.Logging.Trace(watch); return(Response.CreateSuccess(result)); } } ShareFileClient.Logging.Trace(watch); throw new InvalidApiResponseException(httpResponseMessage.StatusCode, "Unable to read response stream"); }
public async Task <T> ExecuteAsync <T>(IQuery <T> query, CancellationToken?token = null) where T : class { var streamQuery = query as IQuery <Stream>; if (streamQuery != null) { return(await this.ExecuteAsync(streamQuery, token) as T); } EventHandlerResponse action = null; int retryCount = 0; do { var requestRoundtripWatch = new ActionStopwatch("RequestRoundTrip", ShareFileClient.Logging); var apiRequest = ApiRequest.FromQuery(query as QueryBase); if (action != null && action.Redirection != null) { apiRequest.IsComposed = true; apiRequest.Uri = action.Redirection.GetCalculatedUri(); apiRequest.Body = action.Redirection.Body; apiRequest.HttpMethod = action.Redirection.Method ?? "GET"; } var httpRequestMessage = BuildRequest(apiRequest); if (httpRequestMessage.RequestUri.AbsolutePath.Contains("Sessions/Login")) { var authorizationHeader = GetAuthorizationHeaderValue(httpRequestMessage.RequestUri, "Bearer"); if (authorizationHeader != null) { httpRequestMessage.Headers.Authorization = authorizationHeader; httpRequestMessage.Headers.Add("X-SFAPI-Tool", ShareFileClient.Configuration.ToolName); httpRequestMessage.Headers.Add("X-SFAPI-ToolVersion", ShareFileClient.Configuration.ToolVersion); } } var responseMessage = await ExecuteRequestAsync(httpRequestMessage, GetCompletionOptionForQuery(typeof(T)), token); action = null; try { if (typeof(T).IsSubclassOf(typeof(ODataObject))) { var response = await HandleTypedResponse <ODataObject>(responseMessage, apiRequest, retryCount ++).ConfigureAwait(false); if (response.Value != null) { string redirectUri; if (response.Value is ODataObject && response.Value.TryGetProperty("Uri", out redirectUri)) { response.Value = new Redirection { Uri = new Uri(redirectUri) }; } if (response.Value is Redirection && typeof(T) != typeof(Redirection)) { var redirection = response.Value as Redirection; // Removed until API is updated to provide this correctly. // !redirection.Available || if (redirection.Uri == null) { throw new ZoneUnavailableException(responseMessage.RequestMessage.RequestUri, "Destination zone is unavailable"); } if (httpRequestMessage.RequestUri.GetAuthority() != redirection.Uri.GetAuthority()) { action = ShareFileClient.OnChangeDomain(httpRequestMessage, redirection); } else { action = EventHandlerResponse.Redirect(redirection); } } else { return(response.Value as T); } } else { action = response.Action; } } else { var response = await HandleTypedResponse <T>(responseMessage, apiRequest, retryCount ++).ConfigureAwait(false); if (response.Value != null) { return(response.Value); } else { action = response.Action; } } } finally { requestRoundtripWatch.Stop(); } } while (action != null && (action.Action == EventHandlerResponseAction.Retry || action.Action == EventHandlerResponseAction.Redirect)); return(default(T)); }