Skip to content

Commit

Permalink
feat: Impl update domain method (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Mar 6, 2024
1 parent 7a6c0be commit 50abc18
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
47 changes: 41 additions & 6 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type DomainsSvc interface {
Get(domainId string) (Domain, error)
RemoveWithContext(ctx context.Context, domainId string) (bool, error)
Remove(domainId string) (bool, error)
UpdateWithContext(ctx context.Context, domainId string, params *UpdateDomainRequest) (Domain, error)
Update(domainId string, params *UpdateDomainRequest) (Domain, error)
}

type DomainsSvcImpl struct {
Expand All @@ -43,13 +45,18 @@ type ListDomainsResponse struct {
Data []Domain `json:"data"`
}

type UpdateDomainRequest struct {
OpenTracking bool `json:"open_tracking,omitempty"`
ClickTracking bool `json:"click_tracking,omitempty"`
}

type Domain struct {
Id string `json:"id"`
Object string `json:"object"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
Status string `json:"status"`
Region string `json:"region"`
Id string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
Region string `json:"region,omitempty"`
}

type Record struct {
Expand All @@ -62,6 +69,34 @@ type Record struct {
Priority json.Number `json:"priority,omitempty"`
}

// UpdateWithContext updates an existing Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/update-domain
func (s *DomainsSvcImpl) UpdateWithContext(ctx context.Context, domainId string, params *UpdateDomainRequest) (Domain, error) {
path := "domains/" + domainId

// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, params)
if err != nil {
return Domain{}, errors.New("[ERROR]: Failed to create Domains.Update request")
}

domainUpdatedResp := new(Domain)

// Send Request
_, err = s.client.Perform(req, domainUpdatedResp)

if err != nil {
return Domain{}, err
}

return *domainUpdatedResp, nil
}

// Update is a wrapper around UpdateWithContext
func (s *DomainsSvcImpl) Update(domainId string, params *UpdateDomainRequest) (Domain, error) {
return s.UpdateWithContext(context.Background(), domainId, params)
}

// CreateWithContext creates a new Domain entry based on the given params
// https://resend.com/docs/api-reference/domains/create-domain
func (s *DomainsSvcImpl) CreateWithContext(ctx context.Context, params *CreateDomainRequest) (CreateDomainResponse, error) {
Expand Down
29 changes: 29 additions & 0 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,32 @@ func TestGetDomain(t *testing.T) {
assert.Equal(t, domain.CreatedAt, "2023-04-26T20:21:26.347412+00:00")
assert.Equal(t, domain.Region, "us-east-1")
}

func TestUpdateDomain(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/domains/d91cd9bd-1176-453e-8fc1-35364d380206", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPatch)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

ret := `
{
"id": "d91cd9bd-1176-453e-8fc1-35364d380206",
"object": "domain"
}`

fmt.Fprint(w, ret)
})

params := &UpdateDomainRequest{
OpenTracking: true,
}
updated, err := client.Domains.Update("d91cd9bd-1176-453e-8fc1-35364d380206", params)
if err != nil {
t.Errorf("Domains.Update returned error: %v", err)
}
assert.True(t, updated.Id == "d91cd9bd-1176-453e-8fc1-35364d380206")
assert.True(t, updated.Object == "domain")
}
11 changes: 11 additions & 0 deletions examples/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ func domainsExample() {
}
fmt.Printf("Retrieved domain: %v", retrievedDomain)

updateDomainParams := &resend.UpdateDomainRequest{
OpenTracking: true,
ClickTracking: true,
}

updated, err := client.Domains.UpdateWithContext(ctx, domain.Id, updateDomainParams)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", updated)

// List
domains, err := client.Domains.ListWithContext(ctx)
if err != nil {
Expand Down

0 comments on commit 50abc18

Please sign in to comment.