Skip to content

Commit

Permalink
Merge pull request cloudflare#812 from broswen/add-waiting-room-status
Browse files Browse the repository at this point in the history
Add Waiting Room status method
  • Loading branch information
jacobbednarz authored Feb 21, 2022
2 parents c92a2be + e496110 commit a4ac2df
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
32 changes: 32 additions & 0 deletions waiting_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ type WaitingRoom struct {
JsonResponseEnabled bool `json:"json_response_enabled"`
}

// WaitingRoomStatus describes the status of a waiting room.
type WaitingRoomStatus struct {
Status string `json:"status"`
EventID string `json:"event_id"`
EstimatedQueuedUsers int `json:"estimated_queued_users"`
EstimatedTotalActiveUsers int `json:"estimated_total_active_users"`
MaxEstimatedTimeMinutes int `json:"max_estimated_time_minutes"`
}

// WaitingRoomDetailResponse is the API response, containing a single WaitingRoom.
type WaitingRoomDetailResponse struct {
Response
Expand All @@ -41,6 +50,12 @@ type WaitingRoomsResponse struct {
Result []WaitingRoom `json:"result"`
}

// WaitingRoomStatusResponse is the API response, containing the status of a waiting room.
type WaitingRoomStatusResponse struct {
Response
Result WaitingRoomStatus `json:"result"`
}

// CreateWaitingRoom creates a new Waiting Room for a zone.
//
// API reference: https://api.cloudflare.com/#waiting-room-create-waiting-room
Expand Down Expand Up @@ -144,3 +159,20 @@ func (api *API) DeleteWaitingRoom(ctx context.Context, zoneID, waitingRoomID str
}
return nil
}

// WaitingRoomStatus returns the status of one Waiting Room for a zone.
//
// API reference: https://api.cloudflare.com/#waiting-room-get-waiting-room-status
func (api *API) WaitingRoomStatus(ctx context.Context, zoneID, waitingRoomID string) (WaitingRoomStatus, error) {
uri := fmt.Sprintf("/zones/%s/waiting_rooms/%s/status", zoneID, waitingRoomID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return WaitingRoomStatus{}, err
}
var r WaitingRoomStatusResponse
err = json.Unmarshal(res, &r)
if err != nil {
return WaitingRoomStatus{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
43 changes: 43 additions & 0 deletions waiting_room_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ var waitingRoomJSON = fmt.Sprintf(`
}
`, testTimestampWaitingRoom.Format(time.RFC3339Nano), testTimestampWaitingRoom.Format(time.RFC3339Nano))

var waitingRoomStatusJSON = `
{
"status": "queueing",
"event_id": "25756b2dfe6e378a06b033b670413757",
"estimated_queued_users": 10,
"estimated_total_active_users": 9,
"max_estimated_time_minutes": 5
}
`

var waitingRoom = WaitingRoom{
ID: "699d98642c564d2e855e9661899b7252",
CreatedOn: testTimestampWaitingRoom,
Expand All @@ -50,6 +60,14 @@ var waitingRoom = WaitingRoom{
CustomPageHTML: "{{#waitTimeKnown}} {{waitTime}} mins {{/waitTimeKnown}} {{^waitTimeKnown}} Queue all enabled {{/waitTimeKnown}}",
}

var waitingRoomStatus = WaitingRoomStatus{
Status: "queueing",
EventID: "25756b2dfe6e378a06b033b670413757",
EstimatedQueuedUsers: 10,
EstimatedTotalActiveUsers: 9,
MaxEstimatedTimeMinutes: 5,
}

func TestListWaitingRooms(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -279,3 +297,28 @@ func TestDeleteWaitingRoom(t *testing.T) {
err := client.DeleteWaitingRoom(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252")
assert.NoError(t, err)
}

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

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": %s
}
`, waitingRoomStatusJSON)
}

mux.HandleFunc("/zones/"+testZoneID+"/waiting_rooms/699d98642c564d2e855e9661899b7252/status", handler)
want := waitingRoomStatus

actual, err := client.WaitingRoomStatus(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252")
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

0 comments on commit a4ac2df

Please sign in to comment.