Skip to content

Commit

Permalink
feat: impl update domain method
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Feb 15, 2024
1 parent 7a6c0be commit bc15376
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
52 changes: 46 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) (UpdateDomainResponse, error)
Update(domainId string, params *UpdateDomainRequest) (UpdateDomainResponse, error)
}

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

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

type UpdateDomainResponse struct {
Data Domain `json:"data"`
Error interface{} `json:"error"`
}

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 +74,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) (UpdateDomainResponse, error) {
path := "domains/" + domainId

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

domainUpdatedResp := new(UpdateDomainResponse)

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

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

return *domainUpdatedResp, nil
}

// Update is a wrapper around UpdateWithContext
func (s *DomainsSvcImpl) Update(domainId string, params *UpdateDomainRequest) (UpdateDomainResponse, 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
32 changes: 32 additions & 0 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,35 @@ 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 := `
{
"data": {
"id": "d91cd9bd-1176-453e-8fc1-35364d380206",
"object": "domain"
},
"error": null
}`

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.Data.Id == "d91cd9bd-1176-453e-8fc1-35364d380206")
assert.True(t, updated.Data.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 bc15376

Please sign in to comment.