Skip to content

Commit a933a3e

Browse files
mtrezzaJawnnypoo
authored andcommitted
Add update server url (#985)
* update code docs for destroy * added setServer and getServer
1 parent bf882c5 commit a933a3e

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

parse/src/main/java/com/parse/Parse.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import android.content.pm.PackageManager;
1313
import android.content.pm.ResolveInfo;
1414
import androidx.annotation.NonNull;
15+
import androidx.annotation.Nullable;
16+
1517
import android.util.Log;
1618

1719
import java.io.File;
@@ -191,10 +193,52 @@ public Void then(Task<Void> task) {
191193
}
192194
}
193195

196+
//region Server URL
197+
198+
/**
199+
* Sets the server URL. This can be used to update the server URL after this client has been
200+
* initialized, without having to {@link Parse#destroy()} this client.
201+
* @param server The server URL to set.
202+
*/
203+
public static void setServer(@NonNull String server) {
204+
try {
205+
ParseRESTCommand.server = new URL(validateServerUrl(server));
206+
} catch (MalformedURLException ex) {
207+
throw new RuntimeException(ex);
208+
}
209+
}
210+
211+
/**
212+
* Returns the current server URL.
213+
*/
214+
public static @Nullable String getServer() {
215+
URL server = ParseRESTCommand.server;
216+
return server == null ? null : server.toString();
217+
}
218+
219+
/**
220+
* Validates the server URL.
221+
* @param server The server URL to validate.
222+
* @return The validated server URL.
223+
*/
224+
private static @Nullable String validateServerUrl(@Nullable String server) {
225+
226+
// Add an extra trailing slash so that Parse REST commands include
227+
// the path as part of the server URL (i.e. http://api.myhost.com/parse)
228+
if (server != null && !server.endsWith("/")) {
229+
server = server + "/";
230+
}
231+
232+
return server;
233+
}
234+
235+
//endregion
236+
194237
/**
195238
* Destroys this client and erases its local data store.
196-
* Calling this after {@code Parse.initialize} allows you to re-initialize this client
197-
* with a new configuration.
239+
* Calling this after {@link Parse#initialize} allows you to re-initialize this client with a
240+
* new configuration. Calling this while server requests are in progress can cause undefined
241+
* behavior.
198242
*/
199243
public static void destroy() {
200244
ParseObject.unregisterParseSubclasses();
@@ -577,14 +621,7 @@ public Builder clientKey(String clientKey) {
577621
* @return The same builder, for easy chaining.
578622
*/
579623
public Builder server(String server) {
580-
581-
// Add an extra trailing slash so that Parse REST commands include
582-
// the path as part of the server URL (i.e. http://api.myhost.com/parse)
583-
if (server != null && !server.endsWith("/")) {
584-
server = server + "/";
585-
}
586-
587-
this.server = server;
624+
this.server = validateServerUrl(server);
588625
return this;
589626
}
590627

0 commit comments

Comments
 (0)