forked from square/retrofit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add converters module and protobuf converter.
- Loading branch information
1 parent
b54de2e
commit ebf8c26
Showing
9 changed files
with
760 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Retrofit Converters | ||
=================== | ||
|
||
Retrofit ships with a default converter for JSON that uses Gson but the library is content-format | ||
agnostic. The child modules contained herein are additional converters for other popular formats. | ||
|
||
To use, supply an instance of your desired converter when building your `RestAdapter` instance. | ||
|
||
```java | ||
RestAdapter restAdapter = new RestAdapter.Builder() | ||
.setServer("https://api.fake.google.com") | ||
.setConverter(new ProtoConverter()) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.1.2-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>retrofit-converters</artifactId> | ||
<name>Converters</name> | ||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>protobuf</module> | ||
</modules> | ||
</project> |
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,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>retrofit-converters</artifactId> | ||
<version>1.1.2-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>converter-protobuf</artifactId> | ||
<name>Converter: Protocol Buffers</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup.retrofit</groupId> | ||
<artifactId>retrofit</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.protobuf</groupId> | ||
<artifactId>protobuf-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easytesting</groupId> | ||
<artifactId>fest-assert-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
55 changes: 55 additions & 0 deletions
55
retrofit-converters/protobuf/src/main/java/retrofit/converter/ProtoConverter.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,55 @@ | ||
// Copyright 2013 Square, Inc. | ||
package retrofit.converter; | ||
|
||
import com.google.protobuf.AbstractMessageLite; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
import retrofit.mime.TypedByteArray; | ||
import retrofit.mime.TypedInput; | ||
import retrofit.mime.TypedOutput; | ||
|
||
/** A {@link Converter} that reads and writes protocol buffers. */ | ||
public class ProtoConverter implements Converter { | ||
private static final String MIME_TYPE = "application/x-protobuf"; | ||
|
||
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException { | ||
if (!(type instanceof Class<?>)) { | ||
throw new IllegalArgumentException("Expected a raw Class<?> but was " + type); | ||
} | ||
Class<?> c = (Class<?>) type; | ||
if (!AbstractMessageLite.class.isAssignableFrom(c)) { | ||
throw new IllegalArgumentException("Expected a protobuf message but was " + c.getName()); | ||
} | ||
|
||
String mimeType = body.mimeType(); | ||
if (!MIME_TYPE.equals(mimeType)) { | ||
throw new ConversionException("Response content type was not a proto: " + mimeType); | ||
} | ||
|
||
try { | ||
Method parseFrom = c.getMethod("parseFrom", InputStream.class); | ||
return parseFrom.invoke(null, body.in()); | ||
} catch (InvocationTargetException e) { | ||
throw new ConversionException(c.getName() + ".parseFrom() failed", e.getCause()); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalArgumentException("Expected a protobuf message but was " + c.getName()); | ||
} catch (IllegalAccessException e) { | ||
throw new AssertionError(); | ||
} catch (IOException e) { | ||
throw new ConversionException(e); | ||
} | ||
} | ||
|
||
@Override public TypedOutput toBody(Object object) { | ||
if (!(object instanceof AbstractMessageLite)) { | ||
throw new IllegalArgumentException( | ||
"Expected a protobuf message but was " + (object != null ? object.getClass().getName() | ||
: "null")); | ||
} | ||
byte[] bytes = ((AbstractMessageLite) object).toByteArray(); | ||
return new TypedByteArray(MIME_TYPE, bytes); | ||
} | ||
} |
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,4 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
protoc --java_out=java/ protos/phone.proto |
Oops, something went wrong.