-
Notifications
You must be signed in to change notification settings - Fork 21
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
Clients for API operations #219
Conversation
Added a quick and dirty generator that takes a <package>/client.go file and turns each of the <package>.Client methods into functions. This allows us to automatically generate package-level functions for each API operation. We write the Client methods and then call go generate to handle creating API operations on the package. As an example, let's say we have a domain package and a domain.Client, which supports a Create method. Usage would then be domain.NewClient().Create() When we run go generate for all packages, we'll also get a domain.Create() function which calls the Client method under the hood.
Up until now, our SDK offered only one way to perform API operations. That is package level functions like actortoken.Create(). These package-level functions would use a global Backend to send requests to the Clerk API. This design works well for most cases, but is a bit inflexible. The existence of a global Backend means that there's no way to configure individual API operations (override secret key, url, HTTP client). We now provide an alternative API, which uses clients with a dedicated Backend for each individual API.
@@ -9,8 +9,25 @@ import ( | |||
"github.com/clerk/clerk-sdk-go/v2" | |||
) | |||
|
|||
//go:generate go run ../cmd/gen/main.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be better written as
//go:generate gen
but I couldn't figure out how to build the package and add it to the PATH. I should probably give it more time at some point.
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Could we perhaps avoid or simplify the code generation with something like the following?
var DefaultClient = &Client{Backend: clerk.GetBackend()}
func Create(ctx context.Context, params *CreateParams) (*clerk.ActorToken, error) {
return DefaultClient.Create(ctx, params)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Sure!
Up until now, our SDK offered only one way to perform API operations. That is package level functions like actortoken.Create().
These package-level functions would use a global Backend to send requests to the Clerk API.
This design works well for most cases, but is a bit inflexible. The existence of a global Backend means that there's no way to configure individual API operations (override secret key, url, HTTP client).
We now provide an alternative API, which uses clients with a dedicated Backend for each individual API.
After this commit, there will be two ways to perform API operations using the SDK.
With globals
With a client
Writing code to support both APIs can be cumbersome. This commit includes a generator that processes client files and creates functions for each exported Client method.
The generator is quick and dirty and relies heavily on conventions all over the place. It's a first version designed to help with getting v2 out quickly and certainly needs improvements.
In order to add a new API operation, we now have to
<package>/client.go
go generate ./...
In order to add a whole new API, we have to
<package>/client.go
file.go:generate
directive in the file.Client
type and its constructor.*Client
for each API operation.go generate ./...
In reality, the only additional steps is adding the generate directive and running the
go generate
command.