Skip to content

Commit

Permalink
Use v9 in imports and make stytch.Client an interface (#131)
Browse files Browse the repository at this point in the history
* Use v9 in imports

* Make stytch.Client an interface
  • Loading branch information
logan-stytch authored Jul 17, 2023
1 parent 9fcf0d7 commit 35612cc
Show file tree
Hide file tree
Showing 72 changed files with 361 additions and 292 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It pairs well with the Stytch [Web SDK](https://www.npmjs.com/package/@stytch/st
## Install

```console
$ go get github.com/stytchauth/stytch-go/v8
$ go get github.com/stytchauth/stytch-go/v9
```

## Usage
Expand All @@ -34,8 +34,8 @@ Create an API client:
import (
"context"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2c/stytchapi"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2c/stytchapi"
)

stytchAPIClient, err := stytchapi.NewClient(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/stytchauth/stytch-go/v8
module github.com/stytchauth/stytch-go/v9

go 1.18

Expand Down
61 changes: 43 additions & 18 deletions stytch/b2b/b2bstytchapi/b2bstytchapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ package b2bstytchapi
import (
"context"
"net/http"
"strings"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b"
"github.com/stytchauth/stytch-go/v8/stytch/config"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b"
"github.com/stytchauth/stytch-go/v9/stytch/config"
)

type Logger interface {
Printf(format string, v ...any)
}

type API struct {
client *stytch.Client
logger Logger
projectID string
secret string
baseURI config.BaseURI

client stytch.Client
initializationContext context.Context
logger Logger

Organizations *b2b.OrganizationsClient
Sessions *b2b.SessionsClient
Expand All @@ -39,19 +42,43 @@ func WithLogger(logger Logger) Option {
return func(api *API) { api.logger = logger }
}

// WithHTTPClient overrides the HTTP client used by the API client. The default value is
// &http.Client{}.
// WithClient overrides the stytch.Client used by the API client. This is useful for completely mocking out requests by
// using something like GoMock against the stytch.Client interface to customize the responses you receive from API
// methods.
//
// NOTE: You should not use this in conjunction with WithHTTPClient or WithBaseURI since the latter two assume usage of
// the default stytch.DefaultClient and this method completely overrides it to use anything conforming to the interface.
func WithClient(client stytch.Client) Option {
return func(api *API) { api.client = client }
}

// WithHTTPClient overrides the HTTP client used by the API client. The default value is &http.Client{}.
//
// NOTE: You should not use this in conjunction with the WithClient option since WithClient completely overrides the
// stytch.Client with one that may not be a stytch.DefaultClient.
func WithHTTPClient(client *http.Client) Option {
return func(api *API) { api.client.HTTPClient = client }
return func(api *API) {
if defaultClient, ok := api.client.(*stytch.DefaultClient); ok {
defaultClient.HTTPClient = client
}
}
}

// WithBaseURI overrides the client base URI determined by the environment.
//
// The value derived from stytch.EnvLive or stytch.EnvTest is already correct for production use
// in the Live or Test environment, respectively. This is implemented to make it easier to use
// this client to access internal development versions of the API.
//
// NOTE: You should not use this in conjunction with the WithClient option since WithClient completely overrides the
// stytch.Client with one that may not be a stytch.DefaultClient.
func WithBaseURI(uri string) Option {
return func(api *API) { api.client.Config.BaseURI = config.BaseURI(uri) }
return func(api *API) {
api.baseURI = config.BaseURI(uri)
if defaultClient, ok := api.client.(*stytch.DefaultClient); ok {
defaultClient.Config.BaseURI = config.BaseURI(uri)
}
}
}

// WithInitializationContext overrides the context used during initialization.
Expand All @@ -70,15 +97,13 @@ func WithInitializationContext(ctx context.Context) Option {
// to override this behavior, but the intention is to provide a simpler interface for creating a client since it's
// extremely rare that developers would want to use something other than the detected environment.
func NewClient(projectID string, secret string, opts ...Option) (*API, error) {
var detectedEnv config.Env
if strings.HasPrefix(projectID, "project-live-") {
detectedEnv = config.EnvLive
} else {
detectedEnv = config.EnvTest
}

defaultClient := stytch.New(projectID, secret)
a := &API{
client: stytch.New(detectedEnv, projectID, secret),
projectID: projectID,
secret: secret,
baseURI: defaultClient.Config.BaseURI,

client: defaultClient,
initializationContext: context.Background(),
}
for _, o := range opts {
Expand Down
6 changes: 3 additions & 3 deletions stytch/b2b/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ package b2b
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v9/stytch"
)

type DiscoveryClient struct {
C *stytch.Client
C stytch.Client
IntermediateSessions *DiscoveryIntermediateSessionsClient
Organizations *DiscoveryOrganizationsClient
}

func NewDiscoveryClient(c *stytch.Client) *DiscoveryClient {
func NewDiscoveryClient(c stytch.Client) *DiscoveryClient {
return &DiscoveryClient{
C: c,
IntermediateSessions: NewDiscoveryIntermediateSessionsClient(c),
Expand Down
4 changes: 2 additions & 2 deletions stytch/b2b/discovery/intermediatesessions/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package intermediatesessions
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/sessions"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/sessions"
)

// ExchangeParams: Request type for `IntermediateSessions.Exchange`.
Expand Down
6 changes: 3 additions & 3 deletions stytch/b2b/discovery/organizations/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package organizations
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/discovery"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/sessions"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/discovery"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/sessions"
)

// CreateParams: Request type for `Organizations.Create`.
Expand Down
2 changes: 1 addition & 1 deletion stytch/b2b/discovery/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package discovery
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
)

// DiscoveredOrganization:
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/discovery_intermediatesessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"context"
"encoding/json"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/discovery/intermediatesessions"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/discovery/intermediatesessions"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type DiscoveryIntermediateSessionsClient struct {
C *stytch.Client
C stytch.Client
}

func NewDiscoveryIntermediateSessionsClient(c *stytch.Client) *DiscoveryIntermediateSessionsClient {
func NewDiscoveryIntermediateSessionsClient(c stytch.Client) *DiscoveryIntermediateSessionsClient {
return &DiscoveryIntermediateSessionsClient{
C: c,
}
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/discovery_organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"context"
"encoding/json"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/discovery/organizations"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/discovery/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type DiscoveryOrganizationsClient struct {
C *stytch.Client
C stytch.Client
}

func NewDiscoveryOrganizationsClient(c *stytch.Client) *DiscoveryOrganizationsClient {
func NewDiscoveryOrganizationsClient(c stytch.Client) *DiscoveryOrganizationsClient {
return &DiscoveryOrganizationsClient{
C: c,
}
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/magiclinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
"context"
"encoding/json"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/magiclinks"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/magiclinks"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type MagicLinksClient struct {
C *stytch.Client
C stytch.Client
Email *MagicLinksEmailClient
Discovery *MagicLinksDiscoveryClient
}

func NewMagicLinksClient(c *stytch.Client) *MagicLinksClient {
func NewMagicLinksClient(c stytch.Client) *MagicLinksClient {
return &MagicLinksClient{
C: c,
Email: NewMagicLinksEmailClient(c),
Expand Down
2 changes: 1 addition & 1 deletion stytch/b2b/magiclinks/discovery/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package discovery
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/discovery"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/discovery"
)

// AuthenticateParams: Request type for `Discovery.Authenticate`.
Expand Down
2 changes: 1 addition & 1 deletion stytch/b2b/magiclinks/email/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package email
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
)

// InviteParams: Request type for `Email.Invite`.
Expand Down
4 changes: 2 additions & 2 deletions stytch/b2b/magiclinks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package magiclinks
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/sessions"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/sessions"
)

// AuthenticateParams: Request type for `MagicLinks.Authenticate`.
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/magiclinks_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"context"
"encoding/json"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/magiclinks/discovery"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/magiclinks/discovery"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type MagicLinksDiscoveryClient struct {
C *stytch.Client
C stytch.Client
}

func NewMagicLinksDiscoveryClient(c *stytch.Client) *MagicLinksDiscoveryClient {
func NewMagicLinksDiscoveryClient(c stytch.Client) *MagicLinksDiscoveryClient {
return &MagicLinksDiscoveryClient{
C: c,
}
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/magiclinks_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
"context"
"encoding/json"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/magiclinks/email"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/magiclinks/email"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type MagicLinksEmailClient struct {
C *stytch.Client
C stytch.Client
Discovery *MagicLinksEmailDiscoveryClient
}

func NewMagicLinksEmailClient(c *stytch.Client) *MagicLinksEmailClient {
func NewMagicLinksEmailClient(c stytch.Client) *MagicLinksEmailClient {
return &MagicLinksEmailClient{
C: c,
Discovery: NewMagicLinksEmailDiscoveryClient(c),
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/magiclinks_email_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"context"
"encoding/json"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/magiclinks/email/discovery"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/magiclinks/email/discovery"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type MagicLinksEmailDiscoveryClient struct {
C *stytch.Client
C stytch.Client
}

func NewMagicLinksEmailDiscoveryClient(c *stytch.Client) *MagicLinksEmailDiscoveryClient {
func NewMagicLinksEmailDiscoveryClient(c stytch.Client) *MagicLinksEmailDiscoveryClient {
return &MagicLinksEmailDiscoveryClient{
C: c,
}
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
"encoding/json"
"fmt"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type OrganizationsClient struct {
C *stytch.Client
C stytch.Client
Members *OrganizationsMembersClient
}

func NewOrganizationsClient(c *stytch.Client) *OrganizationsClient {
func NewOrganizationsClient(c stytch.Client) *OrganizationsClient {
return &OrganizationsClient{
C: c,
Members: NewOrganizationsMembersClient(c),
Expand Down
2 changes: 1 addition & 1 deletion stytch/b2b/organizations/members/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package members
// !!!

import (
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations"
)

// CreateParams: Request type for `Members.Create`.
Expand Down
10 changes: 5 additions & 5 deletions stytch/b2b/organizations_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
"encoding/json"
"fmt"

"github.com/stytchauth/stytch-go/v8/stytch"
"github.com/stytchauth/stytch-go/v8/stytch/b2b/organizations/members"
"github.com/stytchauth/stytch-go/v8/stytch/stytcherror"
"github.com/stytchauth/stytch-go/v9/stytch"
"github.com/stytchauth/stytch-go/v9/stytch/b2b/organizations/members"
"github.com/stytchauth/stytch-go/v9/stytch/stytcherror"
)

type OrganizationsMembersClient struct {
C *stytch.Client
C stytch.Client
}

func NewOrganizationsMembersClient(c *stytch.Client) *OrganizationsMembersClient {
func NewOrganizationsMembersClient(c stytch.Client) *OrganizationsMembersClient {
return &OrganizationsMembersClient{
C: c,
}
Expand Down
Loading

0 comments on commit 35612cc

Please sign in to comment.