Skip to content

Commit

Permalink
Merge pull request #38 from celestiaorg/mojtaba/refactor-api-endpoint…
Browse files Browse the repository at this point in the history
…s-path

chore: refactor endpoints path builder
  • Loading branch information
mojtaba-esk committed Dec 8, 2023
2 parents e944605 + 31421fb commit 456bae4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
26 changes: 10 additions & 16 deletions api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (
"go.uber.org/zap"
)

const EndpointPrefix = "/api/v1"

func path(endpoint string) string {
return EndpointPrefix + endpoint
}

func NewRESTApiV1(productionMode bool, logger *zap.Logger) *RESTApiV1 {
restAPI := &RESTApiV1{
router: mux.NewRouter(),
Expand All @@ -35,19 +29,19 @@ func NewRESTApiV1(productionMode bool, logger *zap.Logger) *RESTApiV1 {

restAPI.router.HandleFunc("/", restAPI.IndexPage).Methods(http.MethodGet, http.MethodPost, http.MethodOptions, http.MethodPut, http.MethodHead)

restAPI.router.HandleFunc(path("/packetloss/start"), restAPI.PacketlossStart).Methods(http.MethodPost)
restAPI.router.HandleFunc(path("/packetloss/status"), restAPI.PacketlossStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(path("/packetloss/stop"), restAPI.PacketlossStop).Methods(http.MethodPost)
restAPI.router.HandleFunc(PacketlossPath.Start(), restAPI.PacketlossStart).Methods(http.MethodPost)
restAPI.router.HandleFunc(PacketlossPath.Status(), restAPI.PacketlossStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(PacketlossPath.Stop(), restAPI.PacketlossStop).Methods(http.MethodPost)

restAPI.router.HandleFunc(path("/bandwidth/start"), restAPI.BandwidthStart).Methods(http.MethodPost)
restAPI.router.HandleFunc(path("/bandwidth/status"), restAPI.BandwidthStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(path("/bandwidth/stop"), restAPI.BandwidthStop).Methods(http.MethodPost)
restAPI.router.HandleFunc(BandwidthPath.Start(), restAPI.BandwidthStart).Methods(http.MethodPost)
restAPI.router.HandleFunc(BandwidthPath.Status(), restAPI.BandwidthStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(BandwidthPath.Stop(), restAPI.BandwidthStop).Methods(http.MethodPost)

restAPI.router.HandleFunc(path("/latency/start"), restAPI.LatencyStart).Methods(http.MethodPost)
restAPI.router.HandleFunc(path("/latency/status"), restAPI.LatencyStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(path("/latency/stop"), restAPI.LatencyStop).Methods(http.MethodPost)
restAPI.router.HandleFunc(LatencyPath.Start(), restAPI.LatencyStart).Methods(http.MethodPost)
restAPI.router.HandleFunc(LatencyPath.Status(), restAPI.LatencyStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(LatencyPath.Stop(), restAPI.LatencyStop).Methods(http.MethodPost)

restAPI.router.HandleFunc(path("/services/status"), restAPI.NetServicesStatus).Methods(http.MethodGet)
restAPI.router.HandleFunc(ServicesPath.Status(), restAPI.NetServicesStatus).Methods(http.MethodGet)

return restAPI
}
Expand Down
26 changes: 26 additions & 0 deletions api/v1/api_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

const endpointPrefix = "/api/v1"

type serviceEndpointPath struct {
basePath string
}

func (e *serviceEndpointPath) Status() string {
return endpointPrefix + e.basePath + "/status"
}

func (e *serviceEndpointPath) Start() string {
return endpointPrefix + e.basePath + "/start"
}

func (e *serviceEndpointPath) Stop() string {
return endpointPrefix + e.basePath + "/stop"
}

var (
PacketlossPath = &serviceEndpointPath{basePath: "/packetloss"}
BandwidthPath = &serviceEndpointPath{basePath: "/bandwidth"}
LatencyPath = &serviceEndpointPath{basePath: "/latency"}
ServicesPath = &serviceEndpointPath{basePath: "/services"}
)
4 changes: 2 additions & 2 deletions api/v1/bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *APITestSuite) TestBandwidthStartStop() {
jsonBody, err := json.Marshal(s.getDefaultBandwidthStartRequest())
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/api/v1/bandwidth/start", bytes.NewReader(jsonBody))
req, err := http.NewRequest(http.MethodPost, api.BandwidthPath.Start(), bytes.NewReader(jsonBody))
require.NoError(t, err)

rr := httptest.NewRecorder()
Expand Down Expand Up @@ -48,7 +48,7 @@ func (s *APITestSuite) TestBandwidthStatus() {
jsonBody, err := json.Marshal(s.getDefaultBandwidthStartRequest())
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/api/v1/bandwidth/start", bytes.NewReader(jsonBody))
req, err := http.NewRequest(http.MethodPost, api.BandwidthPath.Start(), bytes.NewReader(jsonBody))
require.NoError(t, err)

rr := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions api/v1/latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *APITestSuite) TestLatencyStartStop() {
jsonBody, err := json.Marshal(s.getDefaultLatencyStartRequest())
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/api/v1/latency/start", bytes.NewReader(jsonBody))
req, err := http.NewRequest(http.MethodPost, api.LatencyPath.Start(), bytes.NewReader(jsonBody))
require.NoError(t, err)

rr := httptest.NewRecorder()
Expand Down Expand Up @@ -48,7 +48,7 @@ func (s *APITestSuite) TestLatencyStatus() {
jsonBody, err := json.Marshal(s.getDefaultLatencyStartRequest())
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/api/v1/latency/start", bytes.NewReader(jsonBody))
req, err := http.NewRequest(http.MethodPost, api.LatencyPath.Start(), bytes.NewReader(jsonBody))
require.NoError(t, err)

rr := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions api/v1/packetloss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *APITestSuite) TestPacketlossStartStop() {
jsonBody, err := json.Marshal(s.getDefaultPacketLossStartRequest())
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/api/v1/packetloss/start", bytes.NewReader(jsonBody))
req, err := http.NewRequest(http.MethodPost, api.PacketlossPath.Start(), bytes.NewReader(jsonBody))
require.NoError(t, err)

rr := httptest.NewRecorder()
Expand Down Expand Up @@ -48,7 +48,7 @@ func (s *APITestSuite) TestPacketlossStatus() {
jsonBody, err := json.Marshal(s.getDefaultPacketLossStartRequest())
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, "/api/v1/packetloss/start", bytes.NewReader(jsonBody))
req, err := http.NewRequest(http.MethodPost, api.PacketlossPath.Start(), bytes.NewReader(jsonBody))
require.NoError(t, err)

rr := httptest.NewRecorder()
Expand Down
22 changes: 10 additions & 12 deletions sdk/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,50 @@ import (
"github.com/celestiaorg/bittwister/api/v1"
)

const endpointPrefix = api.EndpointPrefix

type PacketLossStartRequest = api.PacketLossStartRequest
type BandwidthStartRequest = api.BandwidthStartRequest
type LatencyStartRequest = api.LatencyStartRequest
type ServiceStatus = api.ServiceStatus
type MetaMessage = api.MetaMessage

func (c *Client) PacketlossStart(req PacketLossStartRequest) error {
return c.postServiceAction(endpointPrefix+"/packetloss/start", req)
return c.postServiceAction(api.PacketlossPath.Start(), req)
}

func (c *Client) PacketlossStop() error {
return c.postServiceAction(endpointPrefix+"/packetloss/stop", nil)
return c.postServiceAction(api.PacketlossPath.Stop(), nil)
}

func (c *Client) PacketlossStatus() (*MetaMessage, error) {
return c.getServiceStatus(endpointPrefix + "/packetloss/status")
return c.getServiceStatus(api.PacketlossPath.Status())
}

func (c *Client) BandwidthStart(req BandwidthStartRequest) error {
return c.postServiceAction(endpointPrefix+"/bandwidth/start", req)
return c.postServiceAction(api.BandwidthPath.Start(), req)
}

func (c *Client) BandwidthStop() error {
return c.postServiceAction(endpointPrefix+"/bandwidth/stop", nil)
return c.postServiceAction(api.BandwidthPath.Stop(), nil)
}

func (c *Client) BandwidthStatus() (*MetaMessage, error) {
return c.getServiceStatus(endpointPrefix + "/bandwidth/status")
return c.getServiceStatus(api.BandwidthPath.Status())
}

func (c *Client) LatencyStart(req LatencyStartRequest) error {
return c.postServiceAction(endpointPrefix+"/latency/start", req)
return c.postServiceAction(api.LatencyPath.Start(), req)
}

func (c *Client) LatencyStop() error {
return c.postServiceAction(endpointPrefix+"/latency/stop", nil)
return c.postServiceAction(api.LatencyPath.Stop(), nil)
}

func (c *Client) LatencyStatus() (*MetaMessage, error) {
return c.getServiceStatus(endpointPrefix + "/latency/status")
return c.getServiceStatus(api.LatencyPath.Status())
}

func (c *Client) AllServicesStatus() ([]ServiceStatus, error) {
resp, err := c.getResource(endpointPrefix + "/services/status")
resp, err := c.getResource(api.ServicesPath.Status())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 456bae4

Please sign in to comment.