-
-
Notifications
You must be signed in to change notification settings - Fork 735
Clean ParseHttpRequset and ParseHttpResposne #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,14 @@ | |
import java.util.Map; | ||
|
||
/** | ||
* The base class of a http response we receive from parse server. It can be implemented by | ||
* different http library such as Apache http, Android URLConnection, Square OKHttp and so on. | ||
* The http response we receive from parse server. Instances of this class are not immutable. The | ||
* response body may be consumed only once. The other fields are immutable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how is this class mutable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
*/ | ||
/** package */ class ParseHttpResponse { | ||
|
||
/** | ||
* Base builder for {@code ParseHttpResponse}. | ||
*/ | ||
/* package */ static abstract class Init<T extends Init<T>> { | ||
private int statusCode; | ||
private InputStream content; | ||
|
@@ -30,6 +33,7 @@ | |
/* package */ abstract T self(); | ||
|
||
public Init() { | ||
this.totalSize = -1; | ||
this.headers = new HashMap<>(); | ||
} | ||
|
||
|
@@ -64,7 +68,7 @@ public T addHeaders(Map<String, String> headers) { | |
} | ||
|
||
public T addHeader(String key, String value) { | ||
this.headers.put(key, value); | ||
headers.put(key, value); | ||
return self(); | ||
} | ||
|
||
|
@@ -74,18 +78,28 @@ public T setContentType(String contentType) { | |
} | ||
} | ||
|
||
/** | ||
* Builder of {@code ParseHttpResponse}. | ||
*/ | ||
public static class Builder extends Init<Builder> { | ||
|
||
@Override | ||
/* package */ Builder self() { | ||
return this; | ||
} | ||
|
||
/* package */ Builder() { | ||
public Builder() { | ||
super(); | ||
} | ||
|
||
/* package */ Builder(ParseHttpResponse response) { | ||
/** | ||
* Makes a new {@code ParseHttpResponse} {@code Builder} based on the input | ||
* {@code ParseHttpResponse}. | ||
* | ||
* @param response | ||
* The {@code ParseHttpResponse} where the {@code Builder}'s values come from. | ||
*/ | ||
public Builder(ParseHttpResponse response) { | ||
super(); | ||
this.setStatusCode(response.getStatusCode()); | ||
this.setContent(response.getContent()); | ||
|
@@ -100,12 +114,12 @@ public ParseHttpResponse build() { | |
} | ||
} | ||
|
||
/* package */ final int statusCode; | ||
/* package */ final InputStream content; | ||
/* package */ final long totalSize; | ||
/* package */ final String reasonPhrase; | ||
/* package */ final Map<String, String> headers; | ||
/* package */ final String contentType; | ||
private final int statusCode; | ||
private final InputStream content; | ||
private final long totalSize; | ||
private final String reasonPhrase; | ||
private final Map<String, String> headers; | ||
private final String contentType; | ||
|
||
/* package */ ParseHttpResponse(Init<?> builder) { | ||
this.statusCode = builder.statusCode; | ||
|
@@ -116,18 +130,26 @@ public ParseHttpResponse build() { | |
this.contentType = builder.contentType; | ||
} | ||
|
||
public Builder newBuilder() { | ||
return new Builder(this); | ||
} | ||
|
||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
/** | ||
* Returns the content of the {@code ParseHttpResponse}'s body. The {@link InputStream} can only | ||
* be read once and can't be reset. | ||
* | ||
* @return The {@link InputStream} of the {@code ParseHttpResponse}'s body. | ||
*/ | ||
public InputStream getContent() { | ||
return content; | ||
} | ||
|
||
/** | ||
* Returns the size of the {@code ParseHttpResponse}'s body. -1 if the size of the | ||
* {@code ParseHttpResponse}'s body is unknown. | ||
* | ||
* @return The size of the {@code ParseHttpResponse}'s body. | ||
*/ | ||
public long getTotalSize() { | ||
return totalSize; | ||
} | ||
|
@@ -141,7 +163,7 @@ public String getContentType() { | |
} | ||
|
||
public String getHeader(String name) { | ||
return headers == null ? null : headers.get(name); | ||
return headers.get(name); | ||
} | ||
|
||
public Map<String, String> getAllHeaders() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this class mutable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The body of the
ParseHttpRequest
is mutable, they can be change after we build theParseHttpRequest
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍