Add client helper methods, errors and auth types for future foji client generator - #57
Merged
Conversation
Motiation: I am adding a client generator to gofoji/foji. To maintain consistency with the foji handler generator, this client should handle HTTP details and provide a simple go interface. This necessitates an error type for non-2xx HTTP responses, which must be in a library foji imports. Change: Add UnexpectedResponseError. This error allows wrappers of http.Client to signal non-successful HTTP responses as an error. It preserves the initial request URL (resp.Req.URL only shows the last URL in a series of redirects) for clear error messages. It preserves the body both for error messages and also to allow inspection for error handling.
Motivation: I am creating a foji client generation template. To retain the same basic structure as the handler generation, users will supply a auth class and helpers to convert the auth class to the required token/username/password etc. To match the handler generation, this will use common types and helper funcs in iken. Change: 1. Add ClientAuthenticateFunc, the basic type for a function that adds authorization to an outbound request. Unlikle AuthenticateFunc, this also allows the function to update the http.Client. This allows using x/oauth2, which requires you to use a client they generate. This client does additional oauth2 refreshs when required, so cannot be replicated as a mere http.Request change. 2. Add *ClientAuth functions for Bearer auth, header auth, query auth, basic auth, cookie auth and "http client wrapping" auth. These do the actual work of setting the request/client for a specific request. 3. Add Client*AuthenticatorFunc for tokens, basic auth, cookie auth and http.Client wrapping. These will be what the application is expected to supply to the foji generated client, and represent how to go from the user object to the credentials the *ClientAuth functions need.
Mirroring SecurityGroup and SecurityGroups, this commit adds ClientSecurityGroup and ClientSecurityGroups to provide the logic for a foji client to combine authenticators to represent the security requirements for an openapi operation.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #57 +/- ##
==========================================
+ Coverage 97.02% 97.76% +0.73%
==========================================
Files 30 32 +2
Lines 1311 1653 +342
==========================================
+ Hits 1272 1616 +344
+ Misses 31 24 -7
- Partials 8 13 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
davidn
marked this pull request as ready for review
July 10, 2026 20:24
bir
reviewed
Jul 10, 2026
bir
reviewed
Jul 10, 2026
bir
reviewed
Jul 10, 2026
bir
reviewed
Jul 10, 2026
bir
reviewed
Jul 10, 2026
bir
reviewed
Jul 10, 2026
leclark
reviewed
Jul 10, 2026
We only want the http.Client used in a request to be modified by authorizers that are actually getting used in the request. ClientSecurityGroup does avoid returning a modified http.Client if it fails, but for defensive programming bir#57 (comment) requests that ClientSecurityGroups doesn't rely on this behaviour. We therefore make two changes: 1. ClientSecurityGroups stores the http.Client returned by a ClientSecurityGroup separately from the inbound client, so it is discarded when the next ClientSecurityGroup is tried. 2. If ClientSecurityGroups returns because of an error, it returns the unmodified original http.Client.
bir
approved these changes
Jul 15, 2026
leclark
approved these changes
Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation: I am adding a client generator to
gofoji/foji. To maintain consistency with the Foji handler generator, this client should handle HTTP details and provide a simple go interface. Akin to handler generation, users will supply a auth class and helpers to convert the auth class to the required token/username/password etc. To match the handler generation, this willuse common types and helper funcs in
iken. Simplicity also necessitates an error type for non-2xx HTTP responses, which must be in a library Foji clients import so that it is uniform across clients.Changes:
Add
UnexpectedResponseError. This error allows wrappers ofhttp.Clientto signal non-successful HTTP responses as an error. It preserves the initial request URL (resp.Req.URL only shows the last URL in a series of redirects) for clear error messages. It preserves the body both for error messages and also to allow inspection for error handling.Add
ClientAuthenticateFunc, the basic type for a function that adds authorization to an outbound request. UnlikeAuthenticateFunc, this also allows the function to update thehttp.Client. This allows usingx/oauth2, which requires you to use a client they generate. This client does additional oauth2 refreshes when required, so cannot be replicated as a merehttp.Requestchange.Add
*ClientAuthfunctions for Bearer auth, header auth, query auth, basic auth, cookie auth and "http client wrapping" auth. These do the actual work of setting the request/client for a specific request.Add
Client*AuthenticatorFuncfor tokens, basic auth, cookie auth andhttp.Clientwrapping. These will be what the application is expected to supply to the foji generated client, and represent how to go from the user object to the credentials the *ClientAuth functions need.Add
ClientSecurityGroupandClientSecurityGroupsto provide the logic for the client to send the authentication for the first security group that has all its authenticators succeed.