-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVRChatClientBuilder.cs
More file actions
181 lines (164 loc) · 6.41 KB
/
Copy pathVRChatClientBuilder.cs
File metadata and controls
181 lines (164 loc) · 6.41 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Net;
namespace VRChat.API.Client
{
/// <summary>
/// The core of VRChat.API.Client. The VRChatClientBuilder class provides a Fluent API for building <see cref="IVRChat"/> clients, and smoothly
/// <b/> authentication against VRChat's API, with a rebuilding pipeline if ever needed.
/// </summary>
public class VRChatClientBuilder
{
private const string _defaultUserAgent = "VRChat.API.Client/1.0 (.NET) netstandard2.0 (https://dot.net) VRChat.API/RestSharp";
private const string _defaultApiKey = "JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26";
private readonly Configuration _configuration;
/// <summary>
/// Initializes a blank <see cref="VRChatClientBuilder"/> <br />
/// </summary>
public VRChatClientBuilder() : this(null) { }
/// <summary>
/// Initializes a <see cref="VRChatClientBuilder"/> from a <see cref="global::VRChat.API.Client.Configuration"/> (if any)
/// <br /> <b style="color: red">Note: <em>This should not be used unless you know what you're doing</em></b>
/// </summary>
/// <param name="incomingConfiguration">The <see cref="Configuration"/> to initialize with as a base</param>
public VRChatClientBuilder(Configuration incomingConfiguration)
{
_configuration = incomingConfiguration ?? new Configuration();
if(_configuration.UserAgent == null)
this.WithUserAgent(_defaultUserAgent);
}
/// <summary>
/// Creates a <see cref="VRChatClientBuilder"/> from a <see cref="Configuration"/>
/// <br /> <b style="color: red">Note: <em>This should not be used unless you know what you're doing</em></b>
/// </summary>
/// <param name="incomingConfiguration">The <see cref="Configuration"/> to initialize with as a base</param>
public static VRChatClientBuilder From(Configuration incomingConfiguration) =>
new VRChatClientBuilder(incomingConfiguration);
/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="auth"></param>
/// <param name="apiKey"></param>
/// <returns></returns>
public VRChatClientBuilder WithCredentials(string username, string password, string auth, string apiKey) => this
.WithUsername(username)
.WithPassword(password)
.WithAuthCookie(auth)
.WithApiKey(apiKey);
/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="apiKey"></param>
/// <returns></returns>
public VRChatClientBuilder WithCredentials(string username, string password, string apiKey) =>
this.WithCredentials(username, password, null, apiKey);
/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public VRChatClientBuilder WithCredentials(string username, string password) =>
this.WithCredentials(username, password, null, null);
/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public VRChatClientBuilder WithUsername(string username)
{
_configuration.Username = username;
return this;
}
/// <summary>
///
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public VRChatClientBuilder WithPassword(string password)
{
_configuration.Password = password;
return this;
}
/// <summary>
///
/// </summary>
/// <param name="apiKey"></param>
/// <returns></returns>
public VRChatClientBuilder WithApiKey(string apiKey)
{
_configuration.AddApiKey("apiKey", apiKey ?? _defaultApiKey);
return this;
}
/// <summary>
///
/// </summary>
/// <param name="auth"></param>
/// <returns></returns>
public VRChatClientBuilder WithAuthCookie(string auth)
{
if (auth != null)
_configuration.AddApiKey("auth", auth);
return this;
}
/// <summary>
///
/// </summary>
/// <param name="userAgent"></param>
/// <returns></returns>
public VRChatClientBuilder WithUserAgent(string userAgent)
{
_configuration.UserAgent = userAgent ?? _defaultUserAgent;
return this;
}
/// <summary>
///
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public VRChatClientBuilder WithTimeout(TimeSpan timeout)
{
_configuration.Timeout = (int)timeout.TotalMilliseconds; // Using Miliseconds over TotalMilliseconds can cause issues when the timespan is empty
return this;
}
/// <summary>
///
/// </summary>
/// <param name="proxy"></param>
/// <returns></returns>
public VRChatClientBuilder WithProxy(WebProxy proxy)
{
_configuration.Proxy = proxy;
return this;
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <param name="bypass"></param>
/// <returns></returns>
public VRChatClientBuilder WithProxy(string url, bool bypass = true) =>
this.WithProxy(new WebProxy(url, bypass));
/// <summary>
///
/// </summary>
/// <param name="useWithoutCredentials"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public IVRChat Build(bool useWithoutCredentials = true)
{
if(!useWithoutCredentials)
{
if(_configuration.Username == null || _configuration.Password == null)
{
if (_configuration.GetApiKeyWithPrefix("auth") == null)
throw new ArgumentException("No credentials have been set up, and useWithoutCredentials is false");
}
}
return VRChat.CreateInternal(_configuration);
}
}
}