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

Clients for API operations #219

Merged
merged 2 commits into from
Feb 7, 2024
Merged

Clients for API operations #219

merged 2 commits into from
Feb 7, 2024

Conversation

gkats
Copy link
Member

@gkats gkats commented Feb 6, 2024

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

clerk.SetKey("sk_live_XXXX")
domain.Create(ctx, &domain.CreateParams{
  Name: clerk.String("clerk.com"),
})

With a client

config := domain.ClientConfig{}
config.Key = "sk_live_XXXX"
c := domain.NewClient(config)
c.Create(ctx, &domain.CreateParams{
  Name: clerk.String("clerk.com"),
})

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

  1. Add an exported method in <package>/client.go
  2. Run go generate ./...

In order to add a whole new API, we have to

  1. Create a new package with a <package>/client.go file.
  2. Add the go:generate directive in the file.
  3. Add boilerplate code for the Client type and its constructor.
  4. Add methods on *Client for each API operation.
  5. Run go generate ./...

In reality, the only additional steps is adding the generate directive and running the go generate command.

gkats added 2 commits February 6, 2024 15:08
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.
@gkats gkats requested a review from a team as a code owner February 6, 2024 13:55
@@ -9,8 +9,25 @@ import (
"github.com/clerk/clerk-sdk-go/v2"
)

//go:generate go run ../cmd/gen/main.go
Copy link
Member Author

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.

@gkats gkats merged commit e15c401 into v2 Feb 7, 2024
4 checks passed
@gkats gkats deleted the core-1642-with-client branch February 7, 2024 10:33
Backend: clerk.NewBackend(&config.BackendConfig),
}
}

Copy link
Contributor

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)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Sure!

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.

3 participants