Skip to content

Commit

Permalink
feat: Ctx everywhere (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgejr568 committed Oct 13, 2023
1 parent a770741 commit 94814b3
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 66 deletions.
39 changes: 29 additions & 10 deletions api_keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resend

import (
"context"
"net/http"
)

Expand All @@ -26,22 +27,25 @@ type ApiKey struct {
}

type ApiKeysSvc interface {
Create(*CreateApiKeyRequest) (CreateApiKeyResponse, error)
CreateWithContext(ctx context.Context, params *CreateApiKeyRequest) (CreateApiKeyResponse, error)
Create(params *CreateApiKeyRequest) (CreateApiKeyResponse, error)
ListWithContext(ctx context.Context) (ListApiKeysResponse, error)
List() (ListApiKeysResponse, error)
RemoveWithContext(ctx context.Context, apiKeyId string) (bool, error)
Remove(apiKeyId string) (bool, error)
}

type ApiKeysSvcImpl struct {
client *Client
}

// Create creates a new API Key based on the given params
// CreateWithContext creates a new API Key based on the given params
// https://resend.com/docs/api-reference/api-keys/create-api-key
func (s *ApiKeysSvcImpl) Create(params *CreateApiKeyRequest) (CreateApiKeyResponse, error) {
func (s *ApiKeysSvcImpl) CreateWithContext(ctx context.Context, params *CreateApiKeyRequest) (CreateApiKeyResponse, error) {
path := "api-keys"

// Prepare request
req, err := s.client.NewRequest(http.MethodPost, path, params)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateApiKeyResponse{}, ErrFailedToCreateApiKeysCreateRequest
}
Expand All @@ -59,13 +63,18 @@ func (s *ApiKeysSvcImpl) Create(params *CreateApiKeyRequest) (CreateApiKeyRespon
return *apiKeysResp, nil
}

// List all API Keys in the project
// Create creates a new API Key based on the given params
func (s *ApiKeysSvcImpl) Create(params *CreateApiKeyRequest) (CreateApiKeyResponse, error) {
return s.CreateWithContext(context.Background(), params)
}

// ListWithContext list all API Keys in the project
// https://resend.com/docs/api-reference/api-keys/list-api-keys
func (s *ApiKeysSvcImpl) List() (ListApiKeysResponse, error) {
func (s *ApiKeysSvcImpl) ListWithContext(ctx context.Context) (ListApiKeysResponse, error) {
path := "api-keys"

// Prepare request
req, err := s.client.NewRequest(http.MethodGet, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListApiKeysResponse{}, ErrFailedToCreateApiKeysListRequest
}
Expand All @@ -83,13 +92,18 @@ func (s *ApiKeysSvcImpl) List() (ListApiKeysResponse, error) {
return *apiKeysResp, nil
}

// Remove deletes a given api key by id
// List all API Keys in the project
func (s *ApiKeysSvcImpl) List() (ListApiKeysResponse, error) {
return s.ListWithContext(context.Background())
}

// RemoveWithContext deletes a given api key by id
// https://resend.com/docs/api-reference/api-keys/delete-api-key
func (s *ApiKeysSvcImpl) Remove(apiKeyId string) (bool, error) {
func (s *ApiKeysSvcImpl) RemoveWithContext(ctx context.Context, apiKeyId string) (bool, error) {
path := "api-keys/" + apiKeyId

// Prepare request
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return false, ErrFailedToCreateApiKeysRemoveRequest
}
Expand All @@ -103,3 +117,8 @@ func (s *ApiKeysSvcImpl) Remove(apiKeyId string) (bool, error) {

return true, nil
}

// Remove deletes a given api key by id
func (s *ApiKeysSvcImpl) Remove(apiKeyId string) (bool, error) {
return s.RemoveWithContext(context.Background(), apiKeyId)
}
68 changes: 52 additions & 16 deletions domains.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package resend

import (
"context"
"encoding/json"
"errors"
"net/http"
)

type DomainsSvc interface {
Create(*CreateDomainRequest) (CreateDomainResponse, error)
CreateWithContext(ctx context.Context, params *CreateDomainRequest) (CreateDomainResponse, error)
Create(params *CreateDomainRequest) (CreateDomainResponse, error)
VerifyWithContext(ctx context.Context, domainId string) (bool, error)
Verify(domainId string) (bool, error)
ListWithContext(ctx context.Context) (ListDomainsResponse, error)
List() (ListDomainsResponse, error)
GetWithContext(ctx context.Context, domainId string) (Domain, error)
Get(domainId string) (Domain, error)
RemoveWithContext(ctx context.Context, domainId string) (bool, error)
Remove(domainId string) (bool, error)
}

Expand Down Expand Up @@ -56,13 +62,13 @@ type Record struct {
Priority json.Number `json:"priority,omitempty"`
}

// Create creates a new Domain entry based on the given params
// CreateWithContext creates a new Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/create-domain
func (s *DomainsSvcImpl) Create(params *CreateDomainRequest) (CreateDomainResponse, error) {
func (s *DomainsSvcImpl) CreateWithContext(ctx context.Context, params *CreateDomainRequest) (CreateDomainResponse, error) {
path := "domains"

// Prepare request
req, err := s.client.NewRequest(http.MethodPost, path, params)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return CreateDomainResponse{}, errors.New("[ERROR]: Failed to create Domains.Create request")
}
Expand All @@ -80,13 +86,19 @@ func (s *DomainsSvcImpl) Create(params *CreateDomainRequest) (CreateDomainRespon
return *domainsResp, nil
}

// Verify verifies a given domain Id
// Create creates a new Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/create-domain
func (s *DomainsSvcImpl) Create(params *CreateDomainRequest) (CreateDomainResponse, error) {
return s.CreateWithContext(context.Background(), params)
}

// VerifyWithContext verifies a given domain Id
// https://resend.com/docs/api-reference/domains/verify-domain
func (s *DomainsSvcImpl) Verify(domainId string) (bool, error) {
func (s *DomainsSvcImpl) VerifyWithContext(ctx context.Context, domainId string) (bool, error) {
path := "domains/" + domainId + "/verify"

// Prepare request
req, err := s.client.NewRequest(http.MethodPost, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return false, errors.New("[ERROR]: Failed to create Domains.Verify request")
}
Expand All @@ -101,13 +113,19 @@ func (s *DomainsSvcImpl) Verify(domainId string) (bool, error) {
return true, nil
}

// List returns the list of all doamins
// Verify verifies a given domain Id
// https://resend.com/docs/api-reference/domains/verify-domain
func (s *DomainsSvcImpl) Verify(domainId string) (bool, error) {
return s.VerifyWithContext(context.Background(), domainId)
}

// ListWithContext returns the list of all domains
// https://resend.com/docs/api-reference/domains/list-domains
func (s *DomainsSvcImpl) List() (ListDomainsResponse, error) {
func (s *DomainsSvcImpl) ListWithContext(ctx context.Context) (ListDomainsResponse, error) {
path := "domains"

// Prepare request
req, err := s.client.NewRequest(http.MethodGet, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return ListDomainsResponse{}, errors.New("[ERROR]: Failed to create Domains.Verify request")
}
Expand All @@ -124,13 +142,19 @@ func (s *DomainsSvcImpl) List() (ListDomainsResponse, error) {
return *domains, nil
}

// Remove removes a given domain entry by id
// List returns the list of all domains
// https://resend.com/docs/api-reference/domains/list-domains
func (s *DomainsSvcImpl) List() (ListDomainsResponse, error) {
return s.ListWithContext(context.Background())
}

// RemoveWithContext removes a given domain entry by id
// https://resend.com/docs/api-reference/domains/delete-domain
func (s *DomainsSvcImpl) Remove(domainId string) (bool, error) {
func (s *DomainsSvcImpl) RemoveWithContext(ctx context.Context, domainId string) (bool, error) {
path := "domains/" + domainId

// Prepare request
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return false, errors.New("[ERROR]: Failed to create Domains.Remove request")
}
Expand All @@ -145,13 +169,19 @@ func (s *DomainsSvcImpl) Remove(domainId string) (bool, error) {
return true, nil
}

// Get retrieves a domain object
// Remove removes a given domain entry by id
// https://resend.com/docs/api-reference/domains/delete-domain
func (s *DomainsSvcImpl) Remove(domainId string) (bool, error) {
return s.RemoveWithContext(context.Background(), domainId)
}

// GetWithContext retrieves a domain object
// https://resend.com/docs/api-reference/domains/get-domain
func (s *DomainsSvcImpl) Get(domainId string) (Domain, error) {
func (s *DomainsSvcImpl) GetWithContext(ctx context.Context, domainId string) (Domain, error) {
path := "domains/" + domainId

// Prepare request
req, err := s.client.NewRequest(http.MethodGet, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return Domain{}, errors.New("[ERROR]: Failed to create Domains.Get request")
}
Expand All @@ -167,3 +197,9 @@ func (s *DomainsSvcImpl) Get(domainId string) (Domain, error) {

return *domain, nil
}

// Get retrieves a domain object
// https://resend.com/docs/api-reference/domains/get-domain
func (s *DomainsSvcImpl) Get(domainId string) (Domain, error) {
return s.GetWithContext(context.Background(), domainId)
}
30 changes: 23 additions & 7 deletions emails.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resend

import (
"context"
"encoding/json"
"net/http"
)
Expand Down Expand Up @@ -81,20 +82,23 @@ func (a *Attachment) MarshalJSON() ([]byte, error) {
}

type EmailsSvc interface {
Send(*SendEmailRequest) (*SendEmailResponse, error)
SendWithContext(ctx context.Context, params *SendEmailRequest) (*SendEmailResponse, error)
Send(params *SendEmailRequest) (*SendEmailResponse, error)
GetWithContext(ctx context.Context, emailID string) (*Email, error)
Get(emailID string) (*Email, error)
}

type EmailsSvcImpl struct {
client *Client
}

// Send sends an email with the given params
func (s *EmailsSvcImpl) Send(params *SendEmailRequest) (*SendEmailResponse, error) {
// SendWithContext sends an email with the given params
// https://resend.com/docs/api-reference/emails/send-email
func (s *EmailsSvcImpl) SendWithContext(ctx context.Context, params *SendEmailRequest) (*SendEmailResponse, error) {
path := "emails"

// Prepare request
req, err := s.client.NewRequest(http.MethodPost, path, params)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, params)
if err != nil {
return nil, ErrFailedToCreateEmailsSendRequest
}
Expand All @@ -112,13 +116,19 @@ func (s *EmailsSvcImpl) Send(params *SendEmailRequest) (*SendEmailResponse, erro
return emailResponse, nil
}

// Get retrieves an email with the given emailID
// Send sends an email with the given params
// https://resend.com/docs/api-reference/emails/send-email
func (s *EmailsSvcImpl) Send(params *SendEmailRequest) (*SendEmailResponse, error) {
return s.SendWithContext(context.Background(), params)
}

// GetWithContext retrieves an email with the given emailID
// https://resend.com/docs/api-reference/emails/retrieve-email
func (s *EmailsSvcImpl) Get(emailID string) (*Email, error) {
func (s *EmailsSvcImpl) GetWithContext(ctx context.Context, emailID string) (*Email, error) {
path := "emails/" + emailID

// Prepare request
req, err := s.client.NewRequest(http.MethodGet, path, nil)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, ErrFailedToCreateEmailsGetRequest
}
Expand All @@ -135,3 +145,9 @@ func (s *EmailsSvcImpl) Get(emailID string) (*Email, error) {

return emailResponse, nil
}

// Get retrieves an email with the given emailID
// https://resend.com/docs/api-reference/emails/retrieve-email
func (s *EmailsSvcImpl) Get(emailID string) (*Email, error) {
return s.GetWithContext(context.Background(), emailID)
}
12 changes: 8 additions & 4 deletions examples/api_keys.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package examples

import (
"context"
"fmt"
"os"

"github.com/resendlabs/resend-go"
)

func apiKeysExample() {

ctx := context.TODO()
apiKey := os.Getenv("RESEND_API_KEY")

client := resend.NewClient(apiKey)
Expand All @@ -18,21 +19,24 @@ func apiKeysExample() {
Name: "nice api key",
}

resp, err := client.ApiKeys.Create(params)
resp, err := client.ApiKeys.CreateWithContext(ctx, params)
if err != nil {
panic(err)
}
fmt.Println("Created API Key id: " + resp.Id)
fmt.Println("Token: " + resp.Token)

// List
apiKeys, err := client.ApiKeys.List()
apiKeys, err := client.ApiKeys.ListWithContext(ctx)
if err != nil {
panic(err)
}
fmt.Printf("You have %d api keys in your project\n", len(apiKeys.Data))

// Delete
client.ApiKeys.Remove(resp.Id)
_, err = client.ApiKeys.RemoveWithContext(ctx, resp.Id)
if err != nil {
panic(err)
}
println("deleted api key id: " + resp.Id)
}
Loading

0 comments on commit 94814b3

Please sign in to comment.