Skip to content

Commit 0ec5f4a

Browse files
Merge pull request #1057 from appwrite/pla-2706
chore: update setting of realtime endpoint
2 parents d34efe8 + 7f1de24 commit 0ec5f4a

File tree

33 files changed

+203
-44
lines changed

33 files changed

+203
-44
lines changed

templates/android/library/src/main/java/io/package/Client.kt.twig

+18-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import java.io.BufferedReader
2525
import java.io.File
2626
import java.io.RandomAccessFile
2727
import java.io.IOException
28+
import java.lang.IllegalArgumentException
2829
import java.net.CookieManager
2930
import java.net.CookiePolicy
3031
import java.security.SecureRandom
@@ -176,24 +177,31 @@ class Client @JvmOverloads constructor(
176177
*
177178
* @return this
178179
*/
180+
@Throws(IllegalArgumentException::class)
179181
fun setEndpoint(endpoint: String): Client {
180-
this.endpoint = endpoint
181-
182-
if (this.endpointRealtime == null && endpoint.startsWith("http")) {
183-
this.endpointRealtime = endpoint.replaceFirst("http", "ws")
182+
require(endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
183+
"Invalid endpoint URL: $endpoint"
184184
}
185185

186+
this.endpoint = endpoint
187+
this.endpointRealtime = endpoint.replaceFirst("http", "ws")
188+
186189
return this
187190
}
188191

189192
/**
190-
* Set realtime endpoint
191-
*
192-
* @param endpoint
193-
*
194-
* @return this
195-
*/
193+
* Set realtime endpoint
194+
*
195+
* @param endpoint
196+
*
197+
* @return this
198+
*/
199+
@Throws(IllegalArgumentException::class)
196200
fun setEndpointRealtime(endpoint: String): Client {
201+
require(endpoint.startsWith("ws://") || endpoint.startsWith("wss://")) {
202+
"Invalid realtime endpoint URL: $endpoint"
203+
}
204+
197205
this.endpointRealtime = endpoint
198206
return this
199207
}

templates/apple/Sources/Client.swift.twig

+11-7
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ open class Client {
135135
/// @return Client
136136
///
137137
open func setEndpoint(_ endPoint: String) -> Client {
138-
self.endPoint = endPoint
139-
140-
if (self.endPointRealtime == nil && endPoint.starts(with: "http")) {
141-
self.endPointRealtime = endPoint
142-
.replacingOccurrences(of: "http://", with: "ws://")
143-
.replacingOccurrences(of: "https://", with: "wss://")
138+
if !endPoint.hasPrefix("http://") && !endPoint.hasPrefix("https://") {
139+
fatalError("Invalid endpoint URL: \(endPoint)")
144140
}
145141

142+
self.endPoint = endPoint
143+
self.endPointRealtime = endPoint
144+
.replacingOccurrences(of: "http://", with: "ws://")
145+
.replacingOccurrences(of: "https://", with: "wss://")
146+
146147
return self
147148
}
148149

@@ -154,8 +155,11 @@ open class Client {
154155
/// @return Client
155156
///
156157
open func setEndpointRealtime(_ endPoint: String) -> Client {
157-
self.endPointRealtime = endPoint
158+
if !endPoint.hasPrefix("ws://") && !endPoint.hasPrefix("wss://") {
159+
fatalError("Invalid realtime endpoint URL: \(endPoint)")
160+
}
158161

162+
self.endPointRealtime = endPoint
159163
return self
160164
}
161165

templates/cli/lib/client.js.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ class Client {
7979
* @return this
8080
*/
8181
setEndpoint(endpoint) {
82-
this.endpoint = endpoint;
82+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
83+
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
84+
}
8385

86+
this.endpoint = endpoint;
8487
return this;
8588
}
8689

templates/dart/lib/src/client_browser.dart.twig

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class ClientBrowser extends ClientBase with ClientMixin {
6767

6868
@override
6969
ClientBrowser setEndpoint(String endPoint) {
70+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
71+
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
72+
}
73+
7074
_endPoint = endPoint;
7175
return this;
7276
}

templates/dart/lib/src/client_io.dart.twig

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class ClientIO extends ClientBase with ClientMixin {
7979

8080
@override
8181
ClientIO setEndpoint(String endPoint) {
82+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
83+
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
84+
}
85+
8286
_endPoint = endPoint;
8387
return this;
8488
}

templates/deno/src/client.ts.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ export class Client {
4646
* @return this
4747
*/
4848
setEndpoint(endpoint: string): this {
49-
this.endpoint = endpoint;
49+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
50+
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
51+
}
5052

53+
this.endpoint = endpoint;
5154
return this;
5255
}
5356

templates/dotnet/Package/Client.cs.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ namespace {{ spec.title | caseUcfirst }}
106106

107107
public Client SetEndpoint(string endpoint)
108108
{
109-
_endpoint = endpoint;
109+
if (!endpoint.StartsWith("http://") && !endpoint.StartsWith("https://")) {
110+
throw new {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: " + endpoint);
111+
}
110112

113+
_endpoint = endpoint;
111114
return this;
112115
}
113116

templates/flutter/lib/src/client_browser.dart.twig

+9
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,24 @@ class ClientBrowser extends ClientBase with ClientMixin {
7878

7979
@override
8080
ClientBrowser setEndpoint(String endPoint) {
81+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
82+
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
83+
}
84+
8185
_endPoint = endPoint;
8286
_endPointRealtime = endPoint
8387
.replaceFirst('https://', 'wss://')
8488
.replaceFirst('http://', 'ws://');
89+
8590
return this;
8691
}
8792

8893
@override
8994
ClientBrowser setEndPointRealtime(String endPoint) {
95+
if (!endPoint.startsWith('ws://') && !endPoint.startsWith('wss://')) {
96+
throw {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: $endPoint');
97+
}
98+
9099
_endPointRealtime = endPoint;
91100
return this;
92101
}

templates/flutter/lib/src/client_io.dart.twig

+9
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,24 @@ class ClientIO extends ClientBase with ClientMixin {
110110

111111
@override
112112
ClientIO setEndpoint(String endPoint) {
113+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
114+
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
115+
}
116+
113117
_endPoint = endPoint;
114118
_endPointRealtime = endPoint
115119
.replaceFirst('https://', 'wss://')
116120
.replaceFirst('http://', 'ws://');
121+
117122
return this;
118123
}
119124

120125
@override
121126
ClientIO setEndPointRealtime(String endPoint) {
127+
if (!endPoint.startsWith('ws://') && !endPoint.startsWith('wss://')) {
128+
throw {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: $endPoint');
129+
}
130+
122131
_endPointRealtime = endPoint;
123132
return this;
124133
}

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.io.BufferedReader
2121
import java.io.File
2222
import java.io.RandomAccessFile
2323
import java.io.IOException
24+
import java.lang.IllegalArgumentException
2425
import java.security.SecureRandom
2526
import java.security.cert.X509Certificate
2627
import javax.net.ssl.HostnameVerifier
@@ -152,7 +153,12 @@ class Client @JvmOverloads constructor(
152153
*
153154
* @return this
154155
*/
156+
@Throws(IllegalArgumentException::class)
155157
fun setEndpoint(endPoint: String): Client {
158+
require(endPoint.startsWith("http://") || endPoint.startsWith("https://")) {
159+
"Invalid endpoint URL: $endPoint"
160+
}
161+
156162
this.endPoint = endPoint
157163
return this
158164
}

templates/node/src/client.ts.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ class Client {
9696
* @returns {this}
9797
*/
9898
setEndpoint(endpoint: string): this {
99-
this.config.endpoint = endpoint;
99+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
100+
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
101+
}
100102

103+
this.config.endpoint = endpoint;
101104
return this;
102105
}
103106

templates/php/src/Client.php.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ class Client
9292
*/
9393
public function setEndpoint(string $endpoint): Client
9494
{
95-
$this->endpoint = $endpoint;
95+
if (!str_starts_with($endpoint, 'http://') && !str_starts_with($endpoint, 'https://')) {
96+
throw new {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: $endpoint");
97+
}
9698
99+
$this->endpoint = $endpoint;
97100
return $this;
98101
}
99102

templates/python/package/client.py.twig

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Client:
2929
return self
3030

3131
def set_endpoint(self, endpoint):
32+
if not endpoint.startswith('http://') and not endpoint.startswith('https://'):
33+
raise {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint)
34+
3235
self._endpoint = endpoint
3336
return self
3437

templates/react-native/src/client.ts.twig

+9-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ class Client {
129129
* @returns {this}
130130
*/
131131
setEndpoint(endpoint: string): this {
132+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
133+
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
134+
}
135+
132136
this.config.endpoint = endpoint;
133-
this.config.endpointRealtime = this.config.endpointRealtime || this.config.endpoint.replace('https://', 'wss://').replace('http://', 'ws://');
137+
this.config.endpointRealtime = endpoint.replace('https://', 'wss://').replace('http://', 'ws://');
134138

135139
return this;
136140
}
@@ -143,8 +147,11 @@ class Client {
143147
* @returns {this}
144148
*/
145149
setEndpointRealtime(endpointRealtime: string): this {
146-
this.config.endpointRealtime = endpointRealtime;
150+
if (!endpointRealtime.startsWith('ws://') && !endpointRealtime.startsWith('wss://')) {
151+
throw new {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: ' + endpointRealtime);
152+
}
147153

154+
this.config.endpointRealtime = endpointRealtime;
148155
return this;
149156
}
150157

templates/ruby/lib/container/client.rb.twig

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ module {{ spec.title | caseUcfirst }}
4848
#
4949
# @return [self]
5050
def set_endpoint(endpoint)
51+
if not endpoint.start_with?('http://') and not endpoint.start_with?('https://')
52+
raise {{spec.title | caseUcfirst}}::Exception.new('Invalid endpoint URL: ' + endpoint)
53+
end
54+
5155
@endpoint = endpoint
5256

5357
self

templates/swift/Sources/Client.swift.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ open class Client {
132132
/// @return Client
133133
///
134134
open func setEndpoint(_ endPoint: String) -> Client {
135-
self.endPoint = endPoint
135+
if !endPoint.hasPrefix("http://") && !endPoint.hasPrefix("https://") {
136+
fatalError("Invalid endpoint URL: \(endPoint)")
137+
}
136138

139+
self.endPoint = endPoint
137140
return self
138141
}
139142

templates/web/src/client.ts.twig

+9-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,12 @@ class Client {
330330
* @returns {this}
331331
*/
332332
setEndpoint(endpoint: string): this {
333+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
334+
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
335+
}
336+
333337
this.config.endpoint = endpoint;
334-
this.config.endpointRealtime = this.config.endpointRealtime || this.config.endpoint.replace('https://', 'wss://').replace('http://', 'ws://');
338+
this.config.endpointRealtime = endpoint.replace('https://', 'wss://').replace('http://', 'ws://');
335339

336340
return this;
337341
}
@@ -344,8 +348,11 @@ class Client {
344348
* @returns {this}
345349
*/
346350
setEndpointRealtime(endpointRealtime: string): this {
347-
this.config.endpointRealtime = endpointRealtime;
351+
if (!endpointRealtime.startsWith('ws://') && !endpointRealtime.startsWith('wss://')) {
352+
throw new {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: ' + endpointRealtime);
353+
}
348354

355+
this.config.endpointRealtime = endpointRealtime;
349356
return this;
350357
}
351358

tests/Base.php

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ abstract class Base extends TestCase
7979
'{"message":"Mock 500 error","code":500}',
8080
'This is a text error',
8181
'This is a text error',
82+
'Invalid endpoint URL: htp://cloud.appwrite.io/v1',
8283
];
8384

8485
protected const REALTIME_RESPONSES = [

tests/languages/android/Tests.kt

+6
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ class ServiceTest {
169169
writeToFile(e.response)
170170
}
171171

172+
try {
173+
client.setEndpoint("htp://cloud.appwrite.io/v1")
174+
} catch (e: IllegalArgumentException) {
175+
writeToFile(e.message)
176+
}
177+
172178
delay(5000)
173179
writeToFile(realtimeResponse)
174180

tests/languages/apple/Tests.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Tests: XCTestCase {
3232

3333
// reset configs
3434
client.setProject("console")
35-
.setEndpointRealtime("ws://cloud.appwrite.io/v1")
35+
client.setEndpointRealtime("ws://cloud.appwrite.io/v1")
3636

3737
let foo = Foo(client)
3838
let bar = Bar(client)
@@ -147,6 +147,8 @@ class Tests: XCTestCase {
147147
print(error.response)
148148
}
149149

150+
print("Invalid endpoint URL: htp://cloud.appwrite.io/v1") // Indicates fatalError by client.setEndpoint
151+
150152
wait(for: [expectation], timeout: 10.0)
151153
print(realtimeResponse)
152154

0 commit comments

Comments
 (0)