Skip to content

Commit b9460ab

Browse files
committed
refactor: use str starts with instead
1 parent a2c55f0 commit b9460ab

File tree

14 files changed

+30
-61
lines changed

14 files changed

+30
-61
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import java.io.RandomAccessFile
2727
import java.io.IOException
2828
import java.net.CookieManager
2929
import java.net.CookiePolicy
30-
import java.net.URL
3130
import java.security.SecureRandom
3231
import java.security.cert.X509Certificate
3332
import javax.net.ssl.SSLContext
@@ -179,7 +178,7 @@ class Client @JvmOverloads constructor(
179178
*/
180179
@Throws({{ spec.title | caseUcfirst }}Exception::class)
181180
fun setEndpoint(endpoint: String): Client {
182-
if (runCatching { URL(endpoint).protocol !in listOf("http", "https") }.getOrDefault(false)) {
181+
if (!(endpoint.startsWith("http://") || endpoint.startsWith("https://"))) {
183182
throw {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: $endpoint")
184183
}
185184

@@ -198,7 +197,7 @@ class Client @JvmOverloads constructor(
198197
*/
199198
@Throws({{ spec.title | caseUcfirst }}Exception::class)
200199
fun setEndpointRealtime(endpoint: String): Client {
201-
if (runCatching { URL(endpoint).protocol !in listOf("ws", "wss") }.getOrDefault(false)) {
200+
if (!(endpoint.startsWith("ws://") || endpoint.startsWith("wss://"))) {
202201
throw {{spec.title | caseUcfirst}}Exception("Invalid realtime endpoint URL: $endpoint")
203202
}
204203

templates/apple/Sources/Client.swift.twig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ open class Client {
136136
/// @throws Exception
137137
///
138138
open func setEndpoint(_ endPoint: String) throws -> Client {
139-
guard let url = URL(string: endPoint),
140-
(url.scheme == "http" || url.scheme == "https")
141-
else {
139+
guard endPoint.hasPrefix("http://") || endPoint.hasPrefix("https://") else {
142140
throw {{spec.title | caseUcfirst}}Error(
143141
message: "Invalid endpoint URL: \(endPoint)",
144142
code: 0,
@@ -164,9 +162,7 @@ open class Client {
164162
/// @throws Exception
165163
///
166164
open func setEndpointRealtime(_ endPoint: String) throws -> Client {
167-
guard let url = URL(string: endPoint),
168-
(url.scheme == "ws" || url.scheme == "wss")
169-
else {
165+
guard endPoint.hasPrefix("ws://") || endPoint.hasPrefix("wss://") else {
170166
throw {{spec.title | caseUcfirst}}Error(
171167
message: "Invalid realtime endpoint URL: \(endPoint)",
172168
code: 0,

templates/cli/lib/client.js.twig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ class Client {
7979
* @return this
8080
*/
8181
setEndpoint(endpoint) {
82-
try {
83-
const url = new URL(endpoint);
84-
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
85-
throw new Error();
86-
}
87-
} catch {
82+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
8883
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
8984
}
9085

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class ClientBrowser extends ClientBase with ClientMixin {
6767

6868
@override
6969
ClientBrowser setEndpoint(String endPoint) {
70-
Uri? uri = Uri.tryParse(endPoint);
71-
if (uri == null || (uri.scheme != 'http' && uri.scheme != 'https')) {
70+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
7271
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
7372
}
7473

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ class ClientIO extends ClientBase with ClientMixin {
7979

8080
@override
8181
ClientIO setEndpoint(String endPoint) {
82-
Uri? uri = Uri.tryParse(endPoint);
83-
if (uri == null || (uri.scheme != 'http' && uri.scheme != 'https')) {
82+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
8483
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
8584
}
8685

templates/deno/src/client.ts.twig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ export class Client {
4646
* @return this
4747
*/
4848
setEndpoint(endpoint: string): this {
49-
try {
50-
const url = new URL(endpoint);
51-
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
52-
throw new Error();
53-
}
54-
} catch {
49+
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
5550
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
5651
}
5752

templates/dotnet/Package/Client.cs.twig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ namespace {{ spec.title | caseUcfirst }}
106106

107107
public Client SetEndpoint(string endpoint)
108108
{
109-
if (!Uri.TryParse(endpoint, out Uri uriResult) ||
110-
(uriResult.Scheme != Uri.UriSchemeHttp && uriResult.Scheme != Uri.UriSchemeHttps))
111-
{
112-
throw new {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: " + endpoint, 0, "", "");
109+
if (!endpoint.StartsWith("http://") && !endpoint.StartsWith("https://")) {
110+
throw new {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: " + endpoint);
113111
}
114112

115113
_endpoint = endpoint;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ class ClientBrowser extends ClientBase with ClientMixin {
7878

7979
@override
8080
ClientBrowser setEndpoint(String endPoint) {
81-
Uri? uri = Uri.tryParse(endPoint);
82-
if (uri == null || (uri.scheme != 'http' && uri.scheme != 'https')) {
81+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
8382
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
8483
}
8584

@@ -93,8 +92,7 @@ class ClientBrowser extends ClientBase with ClientMixin {
9392

9493
@override
9594
ClientBrowser setEndPointRealtime(String endPoint) {
96-
Uri? uri = Uri.tryParse(endPoint);
97-
if (uri == null || (uri.scheme != 'ws' && uri.scheme != 'wss')) {
95+
if (!endPoint.startsWith('ws://') && !endPoint.startsWith('wss://')) {
9896
throw {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: $endPoint');
9997
}
10098

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ class ClientIO extends ClientBase with ClientMixin {
110110

111111
@override
112112
ClientIO setEndpoint(String endPoint) {
113-
Uri? uri = Uri.tryParse(endPoint);
114-
if (uri == null || (uri.scheme != 'http' && uri.scheme != 'https')) {
113+
if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) {
115114
throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint');
116115
}
117116

@@ -125,8 +124,7 @@ class ClientIO extends ClientBase with ClientMixin {
125124

126125
@override
127126
ClientIO setEndPointRealtime(String endPoint) {
128-
Uri? uri = Uri.tryParse(endPoint);
129-
if (uri == null || (uri.scheme != 'ws' && uri.scheme != 'wss')) {
127+
if (!endPoint.startsWith('ws://') && !endPoint.startsWith('wss://')) {
130128
throw {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: $endPoint');
131129
}
132130

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import java.io.RandomAccessFile
2323
import java.io.IOException
2424
import java.security.SecureRandom
2525
import java.security.cert.X509Certificate
26-
import java.net.URL
2726
import javax.net.ssl.HostnameVerifier
2827
import javax.net.ssl.SSLContext
2928
import javax.net.ssl.SSLSocketFactory
@@ -155,8 +154,8 @@ class Client @JvmOverloads constructor(
155154
*/
156155
@Throws({{ spec.title | caseUcfirst }}Exception::class)
157156
fun setEndpoint(endPoint: String): Client {
158-
if (runCatching { URL(endpoint).protocol !in listOf("http", "https") }.getOrDefault(false)) {
159-
throw {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: $endpoint")
157+
if (!(endPoint.startsWith("http://") || endPoint.startsWith("https://"))) {
158+
throw {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: $endPoint")
160159
}
161160

162161
this.endPoint = endPoint

0 commit comments

Comments
 (0)