Skip to content

Commit

Permalink
Add some template APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
corbadovych committed Nov 17, 2023
1 parent 6985fab commit 4859307
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/sdk/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package template

import (
"context"

"github.com/corbado/corbado-go/pkg/sdk/assert"
"github.com/corbado/corbado-go/pkg/sdk/entity/api"
"github.com/corbado/corbado-go/pkg/sdk/servererror"
)

type Template interface {
CreateEmailTemplate(ctx context.Context, req api.EmailTemplateCreateReq, editors ...api.RequestEditorFn) (*api.EmailTemplateCreateRsp, error)
CreateSMSTemplate(ctx context.Context, req api.SmsTemplateCreateReq, editors ...api.RequestEditorFn) (*api.SmsTemplateCreateRsp, error)
}

type Impl struct {
client *api.ClientWithResponses
}

var _ Template = &Impl{}

// New returns new templates client
func New(client *api.ClientWithResponses) (*Impl, error) {
if err := assert.NotNil(client); err != nil {
return nil, err
}

return &Impl{
client: client,
}, nil
}

// CreateEmailTemplate creates a new email template
func (i *Impl) CreateEmailTemplate(ctx context.Context, req api.EmailTemplateCreateReq, editors ...api.RequestEditorFn) (*api.EmailTemplateCreateRsp, error) {
res, err := i.client.EmailTemplateCreateWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// CreateSMSTemplate creates a new SMS template
func (i *Impl) CreateSMSTemplate(ctx context.Context, req api.SmsTemplateCreateReq, editors ...api.RequestEditorFn) (*api.SmsTemplateCreateRsp, error) {
res, err := i.client.SmsTemplateCreateWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}
14 changes: 14 additions & 0 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/corbado/corbado-go/pkg/sdk/project"
"github.com/corbado/corbado-go/pkg/sdk/servererror"
"github.com/corbado/corbado-go/pkg/sdk/session"
"github.com/corbado/corbado-go/pkg/sdk/template"
"github.com/corbado/corbado-go/pkg/sdk/user"
"github.com/corbado/corbado-go/pkg/sdk/validation"
"github.com/pkg/errors"
Expand All @@ -22,6 +23,7 @@ type SDK interface {
EmailLinks() emaillink.EmailLink
Projects() project.Project
Sessions() session.Session
Templates() template.Template
Users() user.User
Validations() validation.Validation
}
Expand All @@ -34,6 +36,7 @@ type Impl struct {
emailLinks emaillink.EmailLink
projects project.Project
sessions session.Session
templates template.Template
validation validation.Validation
users user.User
}
Expand Down Expand Up @@ -72,6 +75,11 @@ func NewSDK(config *Config) (*Impl, error) {
return nil, err
}

templates, err := template.New(client)
if err != nil {
return nil, err
}

users, err := user.New(client)
if err != nil {
return nil, err
Expand All @@ -93,6 +101,7 @@ func NewSDK(config *Config) (*Impl, error) {
emailLinks: emailLinks,
projects: projects,
sessions: sessions,
templates: templates,
users: users,
validation: validation,
HTTPClient: httpClient,
Expand Down Expand Up @@ -124,6 +133,11 @@ func (i *Impl) Sessions() session.Session {
return i.sessions
}

// Templates returns templates client
func (i *Impl) Templates() template.Template {
return i.templates
}

// Users returns users client
func (i *Impl) Users() user.User {
return i.users
Expand Down

0 comments on commit 4859307

Please sign in to comment.