Skip to content

Commit 0929626

Browse files
committed
Merge pull request #89 from ParsePlatform/wangmengyan.move_Method_enum_to_ParseHttpRequest
Move Method enum to ParseHttpRequest
2 parents 056d552 + 74e1df7 commit 0929626

30 files changed

+244
-192
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ public static Task<EventuallyPin> pinEventuallyCommand(ParseObject object,
8989
int type = TYPE_COMMAND;
9090
JSONObject json = null;
9191
if (command.httpPath.startsWith("classes")) {
92-
if (command.method == ParseRequest.Method.POST ||
93-
command.method == ParseRequest.Method.PUT) {
92+
if (command.method == ParseHttpRequest.Method.POST ||
93+
command.method == ParseHttpRequest.Method.PUT) {
9494
type = TYPE_SAVE;
95-
} else if (command.method == ParseRequest.Method.DELETE) {
95+
} else if (command.method == ParseHttpRequest.Method.DELETE) {
9696
type = TYPE_DELETE;
9797
}
9898
} else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
/** package */ class ParseAWSRequest extends ParseRequest<byte[]> {
2222

23-
public ParseAWSRequest(Method method, String url) {
23+
public ParseAWSRequest(ParseHttpRequest.Method method, String url) {
2424
super(method, url);
2525
}
2626

@@ -31,12 +31,12 @@ protected Task<byte[]> onResponseAsync(ParseHttpResponse response,
3131
if (statusCode >= 200 && statusCode < 300 || statusCode == 304) {
3232
// OK
3333
} else {
34-
String action = method == Method.GET ? "Download from" : "Upload to";
34+
String action = method == ParseHttpRequest.Method.GET ? "Download from" : "Upload to";
3535
return Task.forError(new ParseException(ParseException.CONNECTION_FAILED, String.format(
3636
"%s S3 failed. %s", action, response.getReasonPhrase())));
3737
}
3838

39-
if (method != Method.GET) {
39+
if (method != ParseHttpRequest.Method.GET) {
4040
return null;
4141
}
4242

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public ParseApacheHttpClient(int socketOperationTimeout, SSLSessionCache sslSess
173173
}
174174

175175
HttpUriRequest apacheRequest;
176-
ParseRequest.Method method = parseRequest.getMethod();
176+
ParseHttpRequest.Method method = parseRequest.getMethod();
177177
String url = parseRequest.getUrl();
178178
switch (method) {
179179
case GET:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public Task<File> then(Task<Boolean> task) throws Exception {
143143
}
144144

145145
// network
146-
final ParseAWSRequest request = new ParseAWSRequest(ParseRequest.Method.GET, state.url());
146+
final ParseAWSRequest request = new ParseAWSRequest(ParseHttpRequest.Method.GET, state.url());
147147

148148
// TODO(grantland): Stream response directly to file t5042019
149149
return request.executeAsync(

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,59 @@
1010

1111
import java.util.Collections;
1212
import java.util.HashMap;
13-
import java.util.Iterator;
1413
import java.util.Map;
1514

1615
/** package */ class ParseHttpRequest {
16+
17+
public enum Method {
18+
GET, POST, PUT, DELETE;
19+
20+
public static Method fromString(String string) {
21+
Method method;
22+
switch (string) {
23+
case "GET":
24+
method = GET;
25+
break;
26+
case "POST":
27+
method = POST;
28+
break;
29+
case "PUT":
30+
method = PUT;
31+
break;
32+
case "DELETE":
33+
method = DELETE;
34+
break;
35+
default:
36+
throw new IllegalArgumentException("Invalid http method: <" + string + ">");
37+
}
38+
return method;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
String string;
44+
switch (this) {
45+
case GET:
46+
string = "GET";
47+
break;
48+
case POST:
49+
string = "POST";
50+
break;
51+
case PUT:
52+
string = "PUT";
53+
break;
54+
case DELETE:
55+
string = "DELETE";
56+
break;
57+
default:
58+
throw new IllegalArgumentException("Invalid http method: <" + this+ ">");
59+
}
60+
return string;
61+
}
62+
}
63+
1764
private final String url;
18-
private final ParseRequest.Method method;
65+
private final ParseHttpRequest.Method method;
1966
private final Map<String, String> headers;
2067
private final ParseHttpBody body;
2168

@@ -30,7 +77,7 @@ public String getUrl() {
3077
return url;
3178
}
3279

33-
public ParseRequest.Method getMethod() {
80+
public ParseHttpRequest.Method getMethod() {
3481
return method;
3582
}
3683

@@ -48,7 +95,7 @@ public ParseHttpBody getBody() {
4895

4996
public static class Builder {
5097
protected String url;
51-
protected ParseRequest.Method method;
98+
protected ParseHttpRequest.Method method;
5299
protected Map<String, String> headers;
53100
protected ParseHttpBody body;
54101

@@ -68,7 +115,7 @@ public Builder setUrl(String url) {
68115
return this;
69116
}
70117

71-
public Builder setMethod(ParseRequest.Method method) {
118+
public Builder setMethod(ParseHttpRequest.Method method) {
72119
this.method = method;
73120
return this;
74121
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public ParseOkHttpClient(int socketOperationTimeout, SSLSessionCache sslSessionC
108108
@Override
109109
/* package */ Request getRequest(ParseHttpRequest parseRequest) throws IOException {
110110
Request.Builder okHttpRequestBuilder = new Request.Builder();
111-
ParseRequest.Method method = parseRequest.getMethod();
111+
ParseHttpRequest.Method method = parseRequest.getMethod();
112112
// Set method
113113
switch (method) {
114114
case GET:
@@ -161,16 +161,16 @@ private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) {
161161
// Set method
162162
switch (okHttpRequest.method()) {
163163
case OKHTTP_GET:
164-
parseRequestBuilder.setMethod(ParseRequest.Method.GET);
164+
parseRequestBuilder.setMethod(ParseHttpRequest.Method.GET);
165165
break;
166166
case OKHTTP_DELETE:
167-
parseRequestBuilder.setMethod(ParseRequest.Method.DELETE);
167+
parseRequestBuilder.setMethod(ParseHttpRequest.Method.DELETE);
168168
break;
169169
case OKHTTP_POST:
170-
parseRequestBuilder.setMethod(ParseRequest.Method.POST);
170+
parseRequestBuilder.setMethod(ParseHttpRequest.Method.POST);
171171
break;
172172
case OKHTTP_PUT:
173-
parseRequestBuilder.setMethod(ParseRequest.Method.PUT);
173+
parseRequestBuilder.setMethod(ParseHttpRequest.Method.PUT);
174174
break;
175175
default:
176176
// This should never happen

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
// Tracks the AppOpened event
2525
/* package for test */ static final String EVENT_APP_OPENED = "AppOpened";
2626

27-
public ParseRESTAnalyticsCommand(String httpPath, Method httpMethod, Map<String, ?> parameters,
27+
public ParseRESTAnalyticsCommand(
28+
String httpPath,
29+
ParseHttpRequest.Method httpMethod,
30+
Map<String, ?> parameters,
2831
String sessionToken) {
2932
super(httpPath, httpMethod, parameters, sessionToken);
3033
}
@@ -57,6 +60,7 @@ public static ParseRESTAnalyticsCommand trackEventCommand(String eventName, JSON
5760
commandParameters.putAll(parameters);
5861
}
5962
commandParameters.put("at", NoObjectsEncoder.get().encode(new Date()));
60-
return new ParseRESTAnalyticsCommand(httpPath, Method.POST, commandParameters, sessionToken);
63+
return new ParseRESTAnalyticsCommand(
64+
httpPath, ParseHttpRequest.Method.POST, commandParameters, sessionToken);
6165
}
6266
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212

1313
/** package */ class ParseRESTCloudCommand extends ParseRESTCommand {
1414

15-
private ParseRESTCloudCommand(String httpPath, Method httpMethod, Map<String, ?> parameters,
15+
private ParseRESTCloudCommand(
16+
String httpPath,
17+
ParseHttpRequest.Method httpMethod,
18+
Map<String, ?> parameters,
1619
String sessionToken) {
1720
super(httpPath, httpMethod, parameters, sessionToken);
1821
}
1922

2023
public static ParseRESTCloudCommand callFunctionCommand(String functionName,
2124
Map<String, ?> parameters, String sessionToken) {
2225
final String httpPath = String.format("functions/%s", functionName);
23-
return new ParseRESTCloudCommand(httpPath, Method.POST, parameters, sessionToken);
26+
return new ParseRESTCloudCommand(
27+
httpPath, ParseHttpRequest.Method.POST, parameters, sessionToken);
2428
}
2529
}

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static LocalIdManager getLocalIdManager() {
4949
private String installationId;
5050
public String masterKey;
5151

52-
private Method method = Method.GET;
52+
private ParseHttpRequest.Method method = ParseHttpRequest.Method.GET;
5353
private String httpPath;
5454
private JSONObject jsonParameters;
5555

@@ -73,7 +73,7 @@ public T masterKey(String masterKey) {
7373
return self();
7474
}
7575

76-
public T method(Method method) {
76+
public T method(ParseHttpRequest.Method method) {
7777
this.method = method;
7878
return self();
7979
}
@@ -121,7 +121,10 @@ public ParseRESTCommand build() {
121121
private String operationSetUUID;
122122
private String localId;
123123

124-
public ParseRESTCommand(String httpPath, Method httpMethod, Map<String, ?> parameters,
124+
public ParseRESTCommand(
125+
String httpPath,
126+
ParseHttpRequest.Method httpMethod,
127+
Map<String, ?> parameters,
125128
String sessionToken) {
126129
this(
127130
httpPath,
@@ -130,12 +133,18 @@ public ParseRESTCommand(String httpPath, Method httpMethod, Map<String, ?> param
130133
sessionToken);
131134
}
132135

133-
protected ParseRESTCommand(String httpPath, Method httpMethod, JSONObject jsonParameters,
136+
protected ParseRESTCommand(
137+
String httpPath,
138+
ParseHttpRequest.Method httpMethod,
139+
JSONObject jsonParameters,
134140
String sessionToken) {
135141
this(httpPath, httpMethod, jsonParameters, null, sessionToken);
136142
}
137143

138-
private ParseRESTCommand(String httpPath, Method httpMethod, JSONObject jsonParameters,
144+
private ParseRESTCommand(
145+
String httpPath,
146+
ParseHttpRequest.Method httpMethod,
147+
JSONObject jsonParameters,
139148
String localId, String sessionToken) {
140149
super(httpMethod, createUrl(httpPath));
141150

@@ -159,7 +168,8 @@ private ParseRESTCommand(String httpPath, Method httpMethod, JSONObject jsonPara
159168

160169
public static ParseRESTCommand fromJSONObject(JSONObject jsonObject) {
161170
String httpPath = jsonObject.optString("httpPath");
162-
Method httpMethod = Method.fromString(jsonObject.optString("httpMethod"));
171+
ParseHttpRequest.Method httpMethod =
172+
ParseHttpRequest.Method.fromString(jsonObject.optString("httpMethod"));
163173
String sessionToken = jsonObject.optString("sessionToken", null);
164174
String localId = jsonObject.optString("localId", null);
165175
JSONObject jsonParameters = jsonObject.optJSONObject("parameters");
@@ -192,17 +202,17 @@ protected void addAdditionalHeaders(ParseHttpRequest.Builder requestBuilder) {
192202

193203
@Override
194204
protected ParseHttpRequest newRequest(
195-
Method method,
205+
ParseHttpRequest.Method method,
196206
String url,
197207
ProgressCallback uploadProgressCallback) {
198208
ParseHttpRequest request;
199209
if (jsonParameters != null &&
200-
method != Method.POST &&
201-
method != Method.PUT) {
210+
method != ParseHttpRequest.Method.POST &&
211+
method != ParseHttpRequest.Method.PUT) {
202212
// The request URI may be too long to include parameters in the URI.
203213
// To avoid this problem we send the parameters in a POST request json-encoded body
204214
// and add a http method override parameter in newBody.
205-
request = super.newRequest(Method.POST, url, uploadProgressCallback);
215+
request = super.newRequest(ParseHttpRequest.Method.POST, url, uploadProgressCallback);
206216
} else {
207217
request = super.newRequest(method, url, uploadProgressCallback);
208218
}
@@ -221,8 +231,8 @@ protected ParseHttpBody newBody(ProgressCallback uploadProgressCallback) {
221231

222232
try {
223233
JSONObject parameters = jsonParameters;
224-
if (method == Method.GET ||
225-
method == Method.DELETE) {
234+
if (method == ParseHttpRequest.Method.GET ||
235+
method == ParseHttpRequest.Method.DELETE) {
226236
// The request URI may be too long to include parameters in the URI.
227237
// To avoid this problem we send the parameters in a POST request json-encoded body
228238
// and add a http method override parameter.
@@ -421,8 +431,8 @@ private void maybeChangeServerOperation() throws JSONException {
421431
httpPath += String.format("/%s", objectId);
422432
url = createUrl(httpPath);
423433

424-
if (httpPath.startsWith("classes") && method == Method.POST) {
425-
method = Method.PUT;
434+
if (httpPath.startsWith("classes") && method == ParseHttpRequest.Method.POST) {
435+
method = ParseHttpRequest.Method.PUT;
426436
}
427437
}
428438
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@
1313

1414
/** package */ class ParseRESTConfigCommand extends ParseRESTCommand {
1515

16-
public ParseRESTConfigCommand(String httpPath, Method httpMethod, Map<String, ?> parameters,
16+
public ParseRESTConfigCommand(
17+
String httpPath,
18+
ParseHttpRequest.Method httpMethod,
19+
Map<String, ?> parameters,
1720
String sessionToken) {
1821
super(httpPath, httpMethod, parameters, sessionToken);
1922
}
2023

2124
public static ParseRESTConfigCommand fetchConfigCommand(String sessionToken) {
22-
return new ParseRESTConfigCommand("config", Method.GET, null, sessionToken);
25+
return new ParseRESTConfigCommand("config", ParseHttpRequest.Method.GET, null, sessionToken);
2326
}
2427

25-
public static ParseRESTConfigCommand updateConfigCommand(final Map<String, ?> configParameters,
26-
String sessionToken) {
28+
public static ParseRESTConfigCommand updateConfigCommand(
29+
final Map<String, ?> configParameters, String sessionToken) {
2730
Map<String, Map<String, ?>> commandParameters = null;
2831
if (configParameters != null) {
2932
commandParameters = new HashMap<>();
3033
commandParameters.put("params", configParameters);
3134
}
32-
return new ParseRESTConfigCommand("config", Method.PUT, commandParameters, sessionToken);
35+
return new ParseRESTConfigCommand(
36+
"config", ParseHttpRequest.Method.PUT, commandParameters, sessionToken);
3337
}
3438
}

0 commit comments

Comments
 (0)