Skip to content

Commit 2dd0095

Browse files
Merge pull request #330 from ParsePlatform/richardross.configuration.serverurl
Add configurable serverURL to Parse.Configuration.
2 parents 7745c88 + 01b0638 commit 2dd0095

17 files changed

+191
-4
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.io.IOException;
2323
import java.io.RandomAccessFile;
2424
import java.io.UnsupportedEncodingException;
25+
import java.net.MalformedURLException;
26+
import java.net.URL;
2527
import java.util.ArrayList;
2628
import java.util.Collection;
2729
import java.util.Collections;
@@ -48,6 +50,7 @@ public static final class Builder {
4850
private Context context;
4951
private String applicationId;
5052
private String clientKey;
53+
private String server = "https://api.parse.com/1";
5154
private boolean localDataStoreEnabled;
5255
private List<ParseNetworkInterceptor> interceptors;
5356

@@ -128,6 +131,20 @@ public Builder clientKey(String clientKey) {
128131
return this;
129132
}
130133

134+
/**
135+
* Set the server URL to be used by Parse.
136+
*
137+
* This method is only required if you intend to use a different API server than the one at
138+
* api.parse.com.
139+
*
140+
* @param server The server URL to set.
141+
* @return The same builder, for easy chaining.
142+
*/
143+
public Builder server(String server) {
144+
this.server = server;
145+
return this;
146+
}
147+
131148
/**
132149
* Add a {@link ParseNetworkInterceptor}.
133150
*
@@ -182,13 +199,15 @@ public Configuration build() {
182199
/* package for tests */ final Context context;
183200
/* package for tests */ final String applicationId;
184201
/* package for tests */ final String clientKey;
202+
/* package for tests */ final String server;
185203
/* package for tests */ final boolean localDataStoreEnabled;
186204
/* package for tests */ final List<ParseNetworkInterceptor> interceptors;
187205

188206
private Configuration(Builder builder) {
189207
this.context = builder.context;
190208
this.applicationId = builder.applicationId;
191209
this.clientKey = builder.clientKey;
210+
this.server = builder.server;
192211
this.localDataStoreEnabled = builder.localDataStoreEnabled;
193212
this.interceptors = builder.interceptors != null ?
194213
Collections.unmodifiableList(new ArrayList<>(builder.interceptors)) :
@@ -356,6 +375,13 @@ public static void initialize(Configuration configuration) {
356375
isLocalDatastoreEnabled = configuration.localDataStoreEnabled;
357376

358377
ParsePlugins.Android.initialize(configuration.context, configuration.applicationId, configuration.clientKey);
378+
379+
try {
380+
ParseRESTCommand.server = new URL(configuration.server);
381+
} catch (MalformedURLException ex) {
382+
throw new RuntimeException(ex);
383+
}
384+
359385
Context applicationContext = configuration.context.getApplicationContext();
360386

361387
ParseHttpClient.setKeepAlive(true);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
* existing data to retrieve.
5050
*/
5151
public class ParseObject {
52-
/* package */ static String server = "https://api.parse.com";
5352
private static final String AUTO_CLASS_NAME = "_Automatic";
5453
/* package */ static final String VERSION_NAME = "1.12.1-SNAPSHOT";
5554

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.io.IOException;
2121
import java.io.InputStream;
22+
import java.net.MalformedURLException;
23+
import java.net.URL;
2224
import java.util.ArrayList;
2325
import java.util.Collections;
2426
import java.util.Iterator;
@@ -44,6 +46,9 @@
4446
private static final String HEADER_MASTER_KEY = "X-Parse-Master-Key";
4547
private static final String PARAMETER_METHOD_OVERRIDE = "_method";
4648

49+
// Set via Parse.initialize(Configuration)
50+
/* package */ static URL server = null;
51+
4752
private static LocalIdManager getLocalIdManager() {
4853
return ParseCorePlugins.getInstance().getLocalIdManager();
4954
}
@@ -189,7 +194,15 @@ public static ParseRESTCommand fromJSONObject(JSONObject jsonObject) {
189194
private static String createUrl(String httpPath) {
190195
// We send all parameters for GET/HEAD/DELETE requests in a post body,
191196
// so no need to worry about query parameters here.
192-
return String.format("%s/1/%s", ParseObject.server, httpPath);
197+
if (httpPath == null) {
198+
return server.toString();
199+
}
200+
201+
try {
202+
return new URL(server, httpPath).toString();
203+
} catch (MalformedURLException ex) {
204+
throw new RuntimeException(ex);
205+
}
193206
}
194207

195208
protected void addAdditionalHeaders(ParseHttpRequest.Builder requestBuilder) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.IOException;
1919
import java.io.InputStream;
20+
import java.net.MalformedURLException;
21+
import java.net.URL;
2022
import java.util.ArrayList;
2123
import java.util.List;
2224

@@ -63,7 +65,7 @@ public static List<Task<JSONObject>> executeBatch(
6365
for (ParseRESTObjectCommand command : commands) {
6466
JSONObject requestParameters = new JSONObject();
6567
requestParameters.put("method", command.method.toString());
66-
requestParameters.put("path", String.format("/1/%s", command.httpPath));
68+
requestParameters.put("path", new URL(server, command.httpPath).getPath());
6769
JSONObject body = command.jsonParameters;
6870
if (body != null) {
6971
requestParameters.put("body", body);
@@ -73,6 +75,8 @@ public static List<Task<JSONObject>> executeBatch(
7375
parameters.put("requests", requests);
7476
} catch (JSONException e) {
7577
throw new RuntimeException(e);
78+
} catch (MalformedURLException e) {
79+
throw new RuntimeException(e);
7680
}
7781

7882
ParseRESTCommand command = new ParseRESTObjectBatchCommand(

Parse/src/test/java/com/parse/NetworkObjectControllerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313

1414
import org.json.JSONArray;
1515
import org.json.JSONObject;
16+
import org.junit.After;
17+
import org.junit.Before;
1618
import org.junit.Test;
1719
import org.junit.runner.RunWith;
1820
import org.robolectric.RobolectricGradleTestRunner;
1921
import org.robolectric.annotation.Config;
2022

2123
import java.io.ByteArrayInputStream;
24+
import java.net.MalformedURLException;
25+
import java.net.URL;
2226
import java.util.ArrayList;
2327
import java.util.List;
2428

@@ -36,6 +40,16 @@
3640
@Config(constants = BuildConfig.class, sdk = 21)
3741
public class NetworkObjectControllerTest {
3842

43+
@Before
44+
public void setUp() throws MalformedURLException {
45+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
46+
}
47+
48+
@After
49+
public void tearDown() {
50+
ParseRESTCommand.server = null;
51+
}
52+
3953
//region testFetchAsync
4054

4155
@Test

Parse/src/test/java/com/parse/NetworkQueryControllerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
import org.json.JSONArray;
1212
import org.json.JSONException;
1313
import org.json.JSONObject;
14+
import org.junit.After;
15+
import org.junit.Before;
1416
import org.junit.Test;
1517

18+
import java.net.MalformedURLException;
19+
import java.net.URL;
1620
import java.util.List;
1721

1822
import bolts.Task;
@@ -23,6 +27,16 @@
2327

2428
public class NetworkQueryControllerTest {
2529

30+
@Before
31+
public void setUp() throws MalformedURLException {
32+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
33+
}
34+
35+
@After
36+
public void tearDown() {
37+
ParseRESTCommand.server = null;
38+
}
39+
2640
//region testConvertFindResponse
2741

2842
@Test

Parse/src/test/java/com/parse/NetworkSessionControllerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
import org.json.JSONException;
1212
import org.json.JSONObject;
13+
import org.junit.After;
14+
import org.junit.Before;
1315
import org.junit.Test;
1416
import org.junit.runner.RunWith;
1517
import org.robolectric.RobolectricGradleTestRunner;
1618
import org.robolectric.annotation.Config;
1719

20+
import java.net.MalformedURLException;
21+
import java.net.URL;
1822
import java.util.Map;
1923

2024
import static org.junit.Assert.assertEquals;
@@ -26,6 +30,16 @@
2630
@Config(constants = BuildConfig.class, sdk = 21)
2731
public class NetworkSessionControllerTest {
2832

33+
@Before
34+
public void setUp() throws MalformedURLException {
35+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
36+
}
37+
38+
@After
39+
public void tearDown() {
40+
ParseRESTCommand.server = null;
41+
}
42+
2943
//region testGetSessionAsync
3044

3145
@Test

Parse/src/test/java/com/parse/NetworkUserControllerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
import org.json.JSONException;
1212
import org.json.JSONObject;
13+
import org.junit.After;
14+
import org.junit.Before;
1315
import org.junit.Test;
1416
import org.junit.runner.RunWith;
1517
import org.robolectric.RobolectricGradleTestRunner;
1618
import org.robolectric.annotation.Config;
1719

20+
import java.net.MalformedURLException;
21+
import java.net.URL;
1822
import java.util.HashMap;
1923
import java.util.Map;
2024

@@ -27,6 +31,16 @@
2731
@Config(constants = BuildConfig.class, sdk = 21)
2832
public class NetworkUserControllerTest {
2933

34+
@Before
35+
public void setUp() throws MalformedURLException {
36+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
ParseRESTCommand.server = null;
42+
}
43+
3044
//region testSignUpAsync
3145

3246
@Test

Parse/src/test/java/com/parse/ParseAnalyticsControllerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
package com.parse;
1010

1111
import org.json.JSONObject;
12+
import org.junit.After;
13+
import org.junit.Before;
1214
import org.junit.Test;
1315
import org.junit.runner.RunWith;
1416
import org.mockito.ArgumentCaptor;
1517
import org.robolectric.RobolectricGradleTestRunner;
1618
import org.robolectric.annotation.Config;
1719

20+
import java.net.MalformedURLException;
21+
import java.net.URL;
1822
import java.util.HashMap;
1923
import java.util.Map;
2024

@@ -35,6 +39,16 @@
3539
@Config(constants = BuildConfig.class, sdk = 21)
3640
public class ParseAnalyticsControllerTest {
3741

42+
@Before
43+
public void setUp() throws MalformedURLException {
44+
ParseRESTCommand.server = new URL("https://api.parse.com/1");
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
ParseRESTCommand.server = null;
50+
}
51+
3852
//region testConstructor
3953

4054
@Test

Parse/src/test/java/com/parse/ParseClientConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ public void testBuilder() {
3030
Parse.Configuration.Builder builder = new Parse.Configuration.Builder(null);
3131
builder.applicationId("foo");
3232
builder.clientKey("bar");
33+
builder.server("some.server");
3334
builder.enableLocalDataStore();
3435
Parse.Configuration configuration = builder.build();
3536

3637
assertNull(configuration.context);
3738
assertEquals(configuration.applicationId, "foo");
3839
assertEquals(configuration.clientKey, "bar");
40+
assertEquals(configuration.server, "some.server");
3941
assertEquals(configuration.localDataStoreEnabled, true);
4042
}
4143

0 commit comments

Comments
 (0)