diff --git a/parse/src/main/java/com/parse/Parse.java b/parse/src/main/java/com/parse/Parse.java index e2f082c04..a404685df 100644 --- a/parse/src/main/java/com/parse/Parse.java +++ b/parse/src/main/java/com/parse/Parse.java @@ -12,6 +12,8 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import android.util.Log; import java.io.File; @@ -191,10 +193,52 @@ public Void then(Task task) { } } + //region Server URL + + /** + * Sets the server URL. This can be used to update the server URL after this client has been + * initialized, without having to {@link Parse#destroy()} this client. + * @param server The server URL to set. + */ + public static void setServer(@NonNull String server) { + try { + ParseRESTCommand.server = new URL(validateServerUrl(server)); + } catch (MalformedURLException ex) { + throw new RuntimeException(ex); + } + } + + /** + * Returns the current server URL. + */ + public static @Nullable String getServer() { + URL server = ParseRESTCommand.server; + return server == null ? null : server.toString(); + } + + /** + * Validates the server URL. + * @param server The server URL to validate. + * @return The validated server URL. + */ + private static @Nullable String validateServerUrl(@Nullable String server) { + + // Add an extra trailing slash so that Parse REST commands include + // the path as part of the server URL (i.e. http://api.myhost.com/parse) + if (server != null && !server.endsWith("/")) { + server = server + "/"; + } + + return server; + } + + //endregion + /** * Destroys this client and erases its local data store. - * Calling this after {@code Parse.initialize} allows you to re-initialize this client - * with a new configuration. + * Calling this after {@link Parse#initialize} allows you to re-initialize this client with a + * new configuration. Calling this while server requests are in progress can cause undefined + * behavior. */ public static void destroy() { ParseObject.unregisterParseSubclasses(); @@ -577,14 +621,7 @@ public Builder clientKey(String clientKey) { * @return The same builder, for easy chaining. */ public Builder server(String server) { - - // Add an extra trailing slash so that Parse REST commands include - // the path as part of the server URL (i.e. http://api.myhost.com/parse) - if (server != null && !server.endsWith("/")) { - server = server + "/"; - } - - this.server = server; + this.server = validateServerUrl(server); return this; }