-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvrchat.go
111 lines (97 loc) · 2.17 KB
/
vrchat.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package vrchat
import (
"errors"
"net/http"
"net/http/cookiejar"
"net/url"
"sync"
"time"
"github.com/go-resty/resty/v2"
jsoniter "github.com/json-iterator/go"
"github.com/project-vrcat/vrchat-go/websocket"
)
type (
base struct {
client *resty.Client
jar *cookiejar.Jar
}
VRCGO struct {
base
sync.Mutex
events map[string][]func(interface{})
User User
Pipeline *websocket.Pipeline
}
Options struct {
ClientUserAgent string
Timeout time.Duration
RetryCount int
ProxyURL string
}
Map map[string]interface{}
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var (
Err2FAFailed = errors.New("2FA verification failed")
ErrAuthTokenNotFound = errors.New("AuthToken not found")
)
func NewClient() *VRCGO {
return NewClientWithOptions(Options{
Timeout: time.Second * 5,
RetryCount: 3,
})
}
func NewClientWithOptions(opt Options) *VRCGO {
client := resty.New()
client.JSONMarshal = json.Marshal
client.JSONUnmarshal = json.Unmarshal
if opt.ClientUserAgent == "" {
opt.ClientUserAgent = "vrchat-go/version 0.1.0"
}
if opt.Timeout == 0 {
opt.Timeout = time.Second * 5
}
if opt.ProxyURL != "" {
client.SetProxy(opt.ProxyURL)
}
client.
SetHostURL("https://api.vrchat.cloud/api/1").
SetHeader("User-Agent", opt.ClientUserAgent).
SetTimeout(opt.Timeout).
SetRetryCount(opt.RetryCount)
base := base{
client: client,
}
vrc := &VRCGO{
base: base,
events: map[string][]func(interface{}){},
User: User{base},
}
vrc.Pipeline = websocket.New(opt.ProxyURL, opt.ClientUserAgent, vrc.CallEvent)
client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
switch resp.StatusCode() {
case http.StatusOK:
return nil
default:
return errors.New(errorMessage(resp.String()))
}
})
return vrc
}
func (v *VRCGO) Cookies() ([]*http.Cookie, error) {
return v.jar.Cookies(&url.URL{
Scheme: "https",
Host: "api.vrchat.cloud",
}), nil
}
func errorMessage(body string) (message string) {
var r struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
if err := json.UnmarshalFromString(body, &r); err != nil {
return ""
}
return r.Error.Message
}