Skip to content

Commit 3c4e668

Browse files
committed
Fixed response for raids. Create endpoint should return an array
1 parent 891a2b6 commit 3c4e668

File tree

1 file changed

+135
-133
lines changed
  • internal/mock_api/endpoints/raids

1 file changed

+135
-133
lines changed
+135-133
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,135 @@
1-
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
// SPDX-License-Identifier: Apache-2.0
3-
package raids
4-
5-
import (
6-
"encoding/json"
7-
"math/rand"
8-
"net/http"
9-
"time"
10-
11-
"github.com/twitchdev/twitch-cli/internal/database"
12-
"github.com/twitchdev/twitch-cli/internal/mock_api/authentication"
13-
"github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors"
14-
"github.com/twitchdev/twitch-cli/internal/models"
15-
"github.com/twitchdev/twitch-cli/internal/util"
16-
)
17-
18-
var raidsMethodsSupported = map[string]bool{
19-
http.MethodGet: false,
20-
http.MethodPost: true,
21-
http.MethodDelete: true,
22-
http.MethodPatch: false,
23-
http.MethodPut: false,
24-
}
25-
26-
var raidsScopesByMethod = map[string][]string{
27-
http.MethodGet: {},
28-
http.MethodPost: {"channel:manage:raids"},
29-
http.MethodDelete: {"channel:manage:raids"},
30-
http.MethodPatch: {},
31-
http.MethodPut: {},
32-
}
33-
34-
type GetVIPsResponseBody struct {
35-
CreatedAt string `json:"created_at"`
36-
IsMature bool `json:"is_mature"`
37-
}
38-
39-
type Raids struct{}
40-
41-
func (e Raids) Path() string { return "/raids" }
42-
43-
func (e Raids) GetRequiredScopes(method string) []string {
44-
return raidsScopesByMethod[method]
45-
}
46-
47-
func (e Raids) ValidMethod(method string) bool {
48-
return raidsMethodsSupported[method]
49-
}
50-
51-
func (e Raids) ServeHTTP(w http.ResponseWriter, r *http.Request) {
52-
db = r.Context().Value("db").(database.CLIDatabase)
53-
54-
switch r.Method {
55-
case http.MethodPost:
56-
postRaids(w, r)
57-
break
58-
case http.MethodDelete:
59-
deleteRaids(w, r)
60-
break
61-
default:
62-
w.WriteHeader(http.StatusMethodNotAllowed)
63-
}
64-
}
65-
66-
func postRaids(w http.ResponseWriter, r *http.Request) {
67-
userCtx := r.Context().Value("auth").(authentication.UserAuthentication)
68-
if !userCtx.MatchesSpecifiedIDParam(r, "from_broadcaster_id") {
69-
mock_errors.WriteUnauthorized(w, "from_broadcaster_id does not match token")
70-
return
71-
}
72-
73-
fromBroadcasterID := r.URL.Query().Get("from_broadcaster_id")
74-
if fromBroadcasterID == "" {
75-
mock_errors.WriteBadRequest(w, "Missing required parameter from_broadcaster_id")
76-
return
77-
}
78-
79-
toBroadcasterID := r.URL.Query().Get("to_broadcaster_id")
80-
if toBroadcasterID == "" {
81-
mock_errors.WriteBadRequest(w, "Missing required parameter to_broadcaster_id")
82-
return
83-
}
84-
85-
if fromBroadcasterID == toBroadcasterID {
86-
mock_errors.WriteBadRequest(w, "The IDs on from_broadcaster_id and to_broadcaster_id cannot be the same ID")
87-
return
88-
}
89-
90-
// Check if user exists
91-
user, err := db.NewQuery(r, 100).GetUser(database.User{ID: toBroadcasterID})
92-
if err != nil {
93-
mock_errors.WriteServerError(w, "error pulling to_broadcaster_id from user database: "+err.Error())
94-
return
95-
}
96-
if user.ID == "" {
97-
mock_errors.WriteBadRequest(w, "User specified in to_broadcaster_id doesn't exist")
98-
return
99-
}
100-
101-
rand.Seed(util.GetTimestamp().UnixNano())
102-
isMature := rand.Float32() < 0.5
103-
104-
bytes, _ := json.Marshal(models.APIResponse{
105-
Data: GetVIPsResponseBody{
106-
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
107-
IsMature: isMature,
108-
},
109-
})
110-
w.Write(bytes)
111-
112-
// There's no real channel handling in the mock API, so we'll just ingest this and say it happened.
113-
// Right now this means no 409 Conflict handling
114-
}
115-
116-
func deleteRaids(w http.ResponseWriter, r *http.Request) {
117-
userCtx := r.Context().Value("auth").(authentication.UserAuthentication)
118-
if !userCtx.MatchesBroadcasterIDParam(r) {
119-
mock_errors.WriteUnauthorized(w, "broadcaster_id does not match token")
120-
return
121-
}
122-
123-
broadcasterID := r.URL.Query().Get("broadcaster_id")
124-
if broadcasterID == "" {
125-
mock_errors.WriteBadRequest(w, "Missing required parameter broadcaster_id")
126-
return
127-
}
128-
129-
// There's no real channel handling in the mock API, so we'll just ingest this and say it happened.
130-
// Right now this means no 404 Not Found handling
131-
132-
w.WriteHeader(http.StatusNoContent)
133-
}
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package raids
4+
5+
import (
6+
"encoding/json"
7+
"math/rand"
8+
"net/http"
9+
"time"
10+
11+
"github.com/twitchdev/twitch-cli/internal/database"
12+
"github.com/twitchdev/twitch-cli/internal/mock_api/authentication"
13+
"github.com/twitchdev/twitch-cli/internal/mock_api/mock_errors"
14+
"github.com/twitchdev/twitch-cli/internal/models"
15+
"github.com/twitchdev/twitch-cli/internal/util"
16+
)
17+
18+
var raidsMethodsSupported = map[string]bool{
19+
http.MethodGet: false,
20+
http.MethodPost: true,
21+
http.MethodDelete: true,
22+
http.MethodPatch: false,
23+
http.MethodPut: false,
24+
}
25+
26+
var raidsScopesByMethod = map[string][]string{
27+
http.MethodGet: {},
28+
http.MethodPost: {"channel:manage:raids"},
29+
http.MethodDelete: {"channel:manage:raids"},
30+
http.MethodPatch: {},
31+
http.MethodPut: {},
32+
}
33+
34+
type GetVIPsResponseBody struct {
35+
CreatedAt string `json:"created_at"`
36+
IsMature bool `json:"is_mature"`
37+
}
38+
39+
type Raids struct{}
40+
41+
func (e Raids) Path() string { return "/raids" }
42+
43+
func (e Raids) GetRequiredScopes(method string) []string {
44+
return raidsScopesByMethod[method]
45+
}
46+
47+
func (e Raids) ValidMethod(method string) bool {
48+
return raidsMethodsSupported[method]
49+
}
50+
51+
func (e Raids) ServeHTTP(w http.ResponseWriter, r *http.Request) {
52+
db = r.Context().Value("db").(database.CLIDatabase)
53+
54+
switch r.Method {
55+
case http.MethodPost:
56+
postRaids(w, r)
57+
break
58+
case http.MethodDelete:
59+
deleteRaids(w, r)
60+
break
61+
default:
62+
w.WriteHeader(http.StatusMethodNotAllowed)
63+
}
64+
}
65+
66+
func postRaids(w http.ResponseWriter, r *http.Request) {
67+
userCtx := r.Context().Value("auth").(authentication.UserAuthentication)
68+
if !userCtx.MatchesSpecifiedIDParam(r, "from_broadcaster_id") {
69+
mock_errors.WriteUnauthorized(w, "from_broadcaster_id does not match token")
70+
return
71+
}
72+
73+
fromBroadcasterID := r.URL.Query().Get("from_broadcaster_id")
74+
if fromBroadcasterID == "" {
75+
mock_errors.WriteBadRequest(w, "Missing required parameter from_broadcaster_id")
76+
return
77+
}
78+
79+
toBroadcasterID := r.URL.Query().Get("to_broadcaster_id")
80+
if toBroadcasterID == "" {
81+
mock_errors.WriteBadRequest(w, "Missing required parameter to_broadcaster_id")
82+
return
83+
}
84+
85+
if fromBroadcasterID == toBroadcasterID {
86+
mock_errors.WriteBadRequest(w, "The IDs on from_broadcaster_id and to_broadcaster_id cannot be the same ID")
87+
return
88+
}
89+
90+
// Check if user exists
91+
user, err := db.NewQuery(r, 100).GetUser(database.User{ID: toBroadcasterID})
92+
if err != nil {
93+
mock_errors.WriteServerError(w, "error pulling to_broadcaster_id from user database: "+err.Error())
94+
return
95+
}
96+
if user.ID == "" {
97+
mock_errors.WriteBadRequest(w, "User specified in to_broadcaster_id doesn't exist")
98+
return
99+
}
100+
101+
rand.Seed(util.GetTimestamp().UnixNano())
102+
isMature := rand.Float32() < 0.5
103+
104+
bytes, _ := json.Marshal(models.APIResponse{
105+
Data: []GetVIPsResponseBody{
106+
{
107+
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
108+
IsMature: isMature,
109+
},
110+
},
111+
})
112+
w.Write(bytes)
113+
114+
// There's no real channel handling in the mock API, so we'll just ingest this and say it happened.
115+
// Right now this means no 409 Conflict handling
116+
}
117+
118+
func deleteRaids(w http.ResponseWriter, r *http.Request) {
119+
userCtx := r.Context().Value("auth").(authentication.UserAuthentication)
120+
if !userCtx.MatchesBroadcasterIDParam(r) {
121+
mock_errors.WriteUnauthorized(w, "broadcaster_id does not match token")
122+
return
123+
}
124+
125+
broadcasterID := r.URL.Query().Get("broadcaster_id")
126+
if broadcasterID == "" {
127+
mock_errors.WriteBadRequest(w, "Missing required parameter broadcaster_id")
128+
return
129+
}
130+
131+
// There's no real channel handling in the mock API, so we'll just ingest this and say it happened.
132+
// Right now this means no 404 Not Found handling
133+
134+
w.WriteHeader(http.StatusNoContent)
135+
}

0 commit comments

Comments
 (0)