Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FRPAL-5609] Add support for redirects from API endpoints #46

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion Float.Core/Net/RequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,25 @@ public async Task<Response> Send(HttpMethod method, string path, Dictionary<stri
request = await authStrategy.AuthenticateRequest(request).ConfigureAwait(false);
}

var response = await Send(request).ConfigureAwait(false);
Response response;

try
{
response = await Send(request).ConfigureAwait(false);
}
catch (ForbiddenRedirectException ex)
{
// recreate a new request with the redirected url.
request = PrepareRequestMessage(method, ex.RedirectUri, headers, body);

if (authStrategy != null)
{
// Send the first authenticated request
request = await authStrategy.AuthenticateRequest(request).ConfigureAwait(false);
}

response = await Send(request).ConfigureAwait(false);
}

// If we get unauthorized response, try to authenticate a second time.
// This is common in OAuth when an access token is expired.
Expand Down Expand Up @@ -280,9 +298,17 @@ async Task<Response> Send(HttpRequestMessage request)

try
{
var originalUri = request.RequestUri?.AbsolutePath;

var httpResponse = await webClient.SendAsync(request).ConfigureAwait(false);
var responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

// Verify if a forbidden redirect happened.
if (httpResponse.StatusCode == HttpStatusCode.Forbidden && originalUri != request.RequestUri?.AbsolutePath)
arctouch-alejandrovarela marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ForbiddenRedirectException(request.RequestUri);
arctouch-alejandrovarela marked this conversation as resolved.
Show resolved Hide resolved
}

return new Response(httpResponse, responseContent);
}
catch (Exception e)
Expand All @@ -295,5 +321,18 @@ async Task<Response> Send(HttpRequestMessage request)
throw;
}
}

/// <summary>
/// Forbidden redirect exception helper to handle a content redirect.
/// </summary>
internal class ForbiddenRedirectException : Exception
{
public ForbiddenRedirectException(Uri redirectUri)
{
RedirectUri = redirectUri;
}

public Uri RedirectUri { get; }
}
}
}
Loading