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

feat: expose api client hooks for dotnet #389

Merged
merged 1 commit into from
Jan 2, 2025

Conversation

sbward
Copy link
Contributor

@sbward sbward commented Dec 14, 2024

When using Ory SDK from .NET Blazor WASM, it is necessary to call httpRequestMessage.SetBrowserRequestCredentials(BrowserRequestCredentials.Include) on HttpRequestMessage before invoking browser flows via fetch, so that cookies are included in the request and saved to the browser from the response.

Before making this PR I attempted to convince OpenAPI Generator maintainers to expose end-user-facing hooks on the generated ApiClient, but the implementation is taking time to work out. That is why this PR is for Ory SDK and not OpenAPI Generator.

This is a non-breaking change to add two new public properties to the ApiClient class RequestHook and ResponseHook which are optional actions to pre- and post-process RestClient roundtrips. This allows .NET Blazor WASM users to set a hook like so when creating a client. For example when using Kratos through FrontendApi:

var kratosURL = sp.GetService<IConfiguration>()?.GetValue<string>("Kratos:URL") ?? "";
var config = MergeConfigurations(
	Ory.Client.Client.GlobalConfiguration.Instance,
	new Ory.Client.Client.Configuration { BasePath = kratosURL }
);
static void includeCredentialsHook(RestRequest request)
{
	request.OnBeforeRequest = (HttpRequestMessage httpRequest) =>
	{
		httpRequest.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
		return ValueTask.CompletedTask;
	};
}
var syncClient = new ApiClient(kratosURL)
{
	RequestHook = includeCredentialsHook
};
var asyncClient = new ApiClient(kratosURL)
{
	RequestHook = includeCredentialsHook
};
return new FrontendApi(syncClient, asyncClient, config);

I've built and tested this change with my Blazor WASM project and it worked perfectly.

Please let me know if you require additional documentation or tests.

Related Issue or Design Document

Checklist

  • I have read the contributing guidelines and signed the CLA.
  • I have referenced an issue containing the design document if my change introduces a new feature.
  • I have read the security policy.
  • I confirm that this pull request does not address a security vulnerability.
    If this pull request addresses a security vulnerability,
    I confirm that I got approval (please contact [email protected]) from the maintainers to push the changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary documentation within the code base (if appropriate).

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@aeneasr aeneasr merged commit c446ce6 into ory:master Jan 2, 2025
2 checks passed
aeneasr added a commit that referenced this pull request Jan 2, 2025
aeneasr added a commit that referenced this pull request Jan 2, 2025
@aeneasr
Copy link
Member

aeneasr commented Jan 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants