feat(GODT-2181): Implement GetDomains

This commit is contained in:
James Houlahan
2022-12-13 01:20:31 +01:00
committed by James
parent a847d9b892
commit 779a2ee672
4 changed files with 43 additions and 0 deletions

15
manager_domains.go Normal file
View File

@@ -0,0 +1,15 @@
package proton
import "context"
func (m *Manager) GetDomains(ctx context.Context) ([]string, error) {
var res struct {
Domains []string
}
if _, err := m.r(ctx).SetResult(&res).Get("/core/v4/domains/available"); err != nil {
return nil, err
}
return res.Domains, nil
}

15
server/domains.go Normal file
View File

@@ -0,0 +1,15 @@
package server
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Server) handleGetDomainsAvailable() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Domains": []string{s.domain},
})
}
}

View File

@@ -30,6 +30,11 @@ func initRouter(s *Server) {
auth.POST("/refresh", s.handlePostAuthRefresh())
}
// Domains routes don't need authentication.
if domains := core.Group("/domains"); domains != nil {
domains.GET("/available", s.handleGetDomainsAvailable())
}
// Reporting a bug is also possible without authentication.
if reports := core.Group("/reports"); reports != nil {
reports.POST("/bug", s.handlePostReportBug())

View File

@@ -1662,6 +1662,14 @@ func TestServer_AddressOrder(t *testing.T) {
})
}
func TestServer_Domains(t *testing.T) {
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) {
domains, err := m.GetDomains(ctx)
require.NoError(t, err)
require.Equal(t, []string{s.GetDomain()}, domains)
})
}
func withServer(t *testing.T, fn func(ctx context.Context, s *Server, m *proton.Manager), opts ...Option) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()