Skip to content

Commit

Permalink
feat: support for rewrites api
Browse files Browse the repository at this point in the history
  • Loading branch information
amalucelli committed Jun 28, 2022
1 parent b3a913b commit 8a96f46
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nextdns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Client struct {
Security SecurityService
SecurityTlds SecurityTldsService

// Services for the Rewrites.
Rewrites RewritesService

// Debug mode for the HTTP requests.
Debug bool
}
Expand Down Expand Up @@ -153,6 +156,9 @@ func New(opts ...ClientOption) (*Client, error) {
c.Security = NewSecurityService(c)
c.SecurityTlds = NewSecurityTldsService(c)

// Initialize the services for the Rewrites.
c.Rewrites = NewRewritesService(c)

return c, nil
}

Expand Down
2 changes: 2 additions & 0 deletions nextdns/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type CreateProfileRequest struct {
Denylist []*Denylist `json:"denylist,omitempty"`
Allowlist []*Allowlist `json:"allowlist,omitempty"`
Settings *Settings `json:"settings,omitempty"`
Rewrites []*Rewrites `json:"rewrites,omitempty"`
}

// UpdateProfileRequest encapsulates the request for setting custom profile settings.
Expand Down Expand Up @@ -59,6 +60,7 @@ type Profile struct {
Denylist []*Denylist `json:"denylist,omitempty"`
Allowlist []*Allowlist `json:"allowlist,omitempty"`
Settings *Settings `json:"settings,omitempty"`
Rewrites []*Rewrites `json:"rewrites,omitempty"`
}

// newProfileRequest represents the response from a new profile request.
Expand Down
67 changes: 67 additions & 0 deletions nextdns/rewrites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package nextdns

import (
"context"
"fmt"
"net/http"

"github.com/pkg/errors"
)

// rewritesAPIPath is the HTTP path for the rewrites API.
const rewritesAPIPath = "rewrites"

// Rewrites represents the rewrite list of a profile.
type Rewrites struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Content string `json:"content,omitempty"`
}

// GetRewritesRequest encapsulates the request for getting an rewrites.
type GetRewritesRequest struct {
ProfileID string
}

// RewritesService is an interface for communicating with the NextDNS rewrites API endpoint.
type RewritesService interface {
Get(context.Context, *GetRewritesRequest) ([]*Rewrites, error)
}

// rewritesResponse represents the rewrites response.
type rewritesResponse struct {
Rewrites []*Rewrites `json:"data"`
}

// privacyService represents the NextDNS rewrites service.
type rewritesService struct {
client *Client
}

var _ RewritesService = &rewritesService{}

// NewRewritesService returns a new NextDNS rewrites service.
// nolint: revive
func NewRewritesService(client *Client) *rewritesService {
return &rewritesService{
client: client,
}
}

// Get returns the rewrites of a profile.
func (s *rewritesService) Get(ctx context.Context, request *GetRewritesRequest) ([]*Rewrites, error) {
path := fmt.Sprintf("%s/%s", profileAPIPath(request.ProfileID), rewritesAPIPath)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, errors.Wrap(err, "error creating request to get the rewrite list")
}

response := rewritesResponse{}
err = s.client.do(ctx, req, &response)
if err != nil {
return nil, errors.Wrap(err, "error making a request to get the rewrite list")
}

return response.Rewrites, nil
}

0 comments on commit 8a96f46

Please sign in to comment.