Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
63 lines (52 loc) · 1.48 KB
/
Copy pathutil.go
File metadata and controls
63 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gatewayutil
import (
"errors"
"fmt"
"net/url"
"github.com/discordpkg/gateway"
)
func DeriveShardID(snowflake uint64, totalNumberOfShards uint) gateway.ShardID {
createdUnix := snowflake >> 22
groups := uint64(totalNumberOfShards)
return gateway.ShardID(createdUnix % groups)
}
var supportedAPIVersions = []string{
"8", "9", "10",
}
var supportedAPICodes = []string{
"json",
}
var ErrURLScheme = errors.New("url scheme was not websocket (ws nor wss)")
var ErrUnsupportedAPIVersion = fmt.Errorf("only discord api version %+v is supported", supportedAPIVersions)
var ErrUnsupportedAPICodec = fmt.Errorf("only %+v is supported", supportedAPICodes)
var ErrIncompleteDialURL = errors.New("incomplete url is missing one or many of: 'version', 'encoding', 'scheme'")
func ValidateDialURL(URLString string) (string, error) {
in := func(keyword string, slice []string) bool {
for i := range slice {
if keyword == slice[i] {
return true
}
}
return false
}
u, urlErr := url.Parse(URLString)
if urlErr != nil {
return "", urlErr
}
scheme := u.Scheme
v := u.Query().Get("v")
encoding := u.Query().Get("encoding")
if v == "" || encoding == "" || scheme == "" {
return "", ErrIncompleteDialURL
}
if u.Scheme != "ws" && u.Scheme != "wss" {
return "", ErrURLScheme
}
if v != "" && !in(v, supportedAPIVersions) {
return "", ErrUnsupportedAPIVersion
}
if encoding != "" && !in(encoding, supportedAPICodes) {
return "", ErrUnsupportedAPICodec
}
return u.String(), nil
}