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

Refactor: Agent Accessor Chain #39

Merged
merged 3 commits into from
Jun 16, 2022

Conversation

the-avid-engineer
Copy link
Member

Arriving from the potential need to have multiple types of agents operating in a single application, I've created this simple implementation of the IAgentAccessor interface which will resolve an agent from a chain of IAgentAccessor implementations.

Example of how to use.

The top-level service provider is expected to only have a single implementation of IAgentAccessor (if you happen to supply more than one, only the last one will be used). Therefore, the AgentAccessorChain implementation has an options object AgentAccessorChainOptions which has its own, private service collection, and this implementation will use all registered implementations of IAgentAccessor.

As an example, you could use this new implementation to combine the HttpContextAgentAccessor, which requires a HTTP Context (via IHttpContextAccessor) to return an IAgent, with a custom implementation that does not require an HTTP Context to return an IAgent. This custom implementation is entirely up to you. (More implementations may or may not come in future package releases. At this time, there is only HttpContextAgentAccessor and UnknownAgentAccessor the latter of which is only appropriate for testing purposes)

serviceCollection.Configure<AgentAccessorChainOptions>(chainOptions =>
{
    // Http Context Agent Accessor
    
    chainOptions.ServiceCollection.Configure<HttpContextAgentSignatureOptions>(signatureOptions =>
    {
        signatureOptions.RedactedHeaders = new[] { "Authorization" };
    });

    chainOptions.ServiceCollection.AddHttpContextAgentAccessor();

    // My Custom Agent Accessor

    chainOptions.ServiceCollection.AddAgentAccessor<MyCustomAgentAccessor>();
});

serviceCollection.AddAgentAccessor<AgentAccessorChain>();

need to use async local to avoid collisions
an implementation of the IAgentAccessor that expects to handle multiple implementations of IAgentAccessor in a private service collection.
@the-avid-engineer the-avid-engineer merged commit f4f55b8 into main Jun 16, 2022
@the-avid-engineer the-avid-engineer deleted the refactor/agent-accessor-chain branch June 16, 2022 23:03
@the-avid-engineer the-avid-engineer self-assigned this Jun 25, 2022
@the-avid-engineer the-avid-engineer added diff:add Code has been added diff:refactor:break Code has been refactored in a way that will cause compiler errors and does not fix a bug labels Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diff:add Code has been added diff:refactor:break Code has been refactored in a way that will cause compiler errors and does not fix a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants