-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
Add streaming support for Wire request bodies #4267
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ofit-converters/wire/src/main/java/retrofit2/converter/wire/WireStreamingRequestBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2015 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package retrofit2.converter.wire; | ||
|
||
import static retrofit2.converter.wire.WireRequestBodyConverter.MEDIA_TYPE; | ||
|
||
import com.squareup.wire.Message; | ||
import com.squareup.wire.ProtoAdapter; | ||
import java.io.IOException; | ||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okio.BufferedSink; | ||
|
||
final class WireStreamingRequestBody<T extends Message<T, ?>> extends RequestBody { | ||
private final ProtoAdapter<T> adapter; | ||
private final T value; | ||
|
||
WireStreamingRequestBody(ProtoAdapter<T> adapter, T value) { | ||
this.adapter = adapter; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return MEDIA_TYPE; | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
adapter.encode(sink, value); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
retrofit-converters/wire/src/test/java/retrofit2/converter/wire/CrashingPhone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Code generated by Wire protocol buffer compiler, do not edit. | ||
// Source file: phone.proto at 6:1 | ||
package retrofit2.converter.wire; | ||
|
||
import com.squareup.wire.FieldEncoding; | ||
import com.squareup.wire.Message; | ||
import com.squareup.wire.ProtoAdapter; | ||
import com.squareup.wire.ProtoReader; | ||
import com.squareup.wire.ProtoWriter; | ||
import com.squareup.wire.WireField; | ||
import com.squareup.wire.internal.Internal; | ||
import java.io.EOFException; | ||
import java.io.IOException; | ||
import okio.ByteString; | ||
|
||
public final class CrashingPhone extends Message<CrashingPhone, CrashingPhone.Builder> { | ||
public static final ProtoAdapter<CrashingPhone> ADAPTER = new ProtoAdapter_CrashingPhone(); | ||
|
||
private static final long serialVersionUID = 0L; | ||
|
||
public static final String DEFAULT_NUMBER = ""; | ||
|
||
@WireField(tag = 1, adapter = "com.squareup.wire.ProtoAdapter#STRING") | ||
public final String number; | ||
|
||
public CrashingPhone(String number) { | ||
this(number, ByteString.EMPTY); | ||
} | ||
|
||
public CrashingPhone(String number, ByteString unknownFields) { | ||
super(ADAPTER, unknownFields); | ||
this.number = number; | ||
} | ||
|
||
@Override | ||
public Builder newBuilder() { | ||
Builder builder = new Builder(); | ||
builder.number = number; | ||
builder.addUnknownFields(unknownFields()); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) return true; | ||
if (!(other instanceof CrashingPhone)) return false; | ||
CrashingPhone o = (CrashingPhone) other; | ||
return Internal.equals(unknownFields(), o.unknownFields()) && Internal.equals(number, o.number); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = super.hashCode; | ||
if (result == 0) { | ||
result = unknownFields().hashCode(); | ||
result = result * 37 + (number != null ? number.hashCode() : 0); | ||
super.hashCode = result; | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder(); | ||
if (number != null) builder.append(", number=").append(number); | ||
return builder.replace(0, 2, "Phone{").append('}').toString(); | ||
} | ||
|
||
public static final class Builder extends Message.Builder<CrashingPhone, Builder> { | ||
public String number; | ||
|
||
public Builder() {} | ||
|
||
public Builder number(String number) { | ||
this.number = number; | ||
return this; | ||
} | ||
|
||
@Override | ||
public CrashingPhone build() { | ||
return new CrashingPhone(number, buildUnknownFields()); | ||
} | ||
} | ||
|
||
private static final class ProtoAdapter_CrashingPhone extends ProtoAdapter<CrashingPhone> { | ||
ProtoAdapter_CrashingPhone() { | ||
super(FieldEncoding.LENGTH_DELIMITED, CrashingPhone.class); | ||
} | ||
|
||
@Override | ||
public int encodedSize(CrashingPhone value) { | ||
return (value.number != null ? ProtoAdapter.STRING.encodedSizeWithTag(1, value.number) : 0) | ||
+ value.unknownFields().size(); | ||
} | ||
|
||
@Override | ||
public void encode(ProtoWriter writer, CrashingPhone value) throws IOException { | ||
throw new EOFException("oops!"); | ||
} | ||
|
||
@Override | ||
public CrashingPhone decode(ProtoReader reader) throws IOException { | ||
Builder builder = new Builder(); | ||
long token = reader.beginMessage(); | ||
for (int tag; (tag = reader.nextTag()) != -1; ) { | ||
switch (tag) { | ||
case 1: | ||
builder.number(ProtoAdapter.STRING.decode(reader)); | ||
break; | ||
default: | ||
{ | ||
FieldEncoding fieldEncoding = reader.peekFieldEncoding(); | ||
Object value = fieldEncoding.rawProtoAdapter().decode(reader); | ||
builder.addUnknownField(tag, fieldEncoding, value); | ||
} | ||
} | ||
} | ||
reader.endMessage(token); | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public CrashingPhone redact(CrashingPhone value) { | ||
Builder builder = value.newBuilder(); | ||
builder.clearUnknownFields(); | ||
return builder.build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Neat!