Skip to content
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 download streaming support #872

Merged
merged 4 commits into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ install:
- export LD_LIBRARY_PATH=$HOME/.local/lib/:$LD_LIBRARY_PATH
# Install npm
- curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | sh && source ~/.dnx/dnvm/dnvm.sh && dnvm upgrade
- rm -rf ~/.nvm && git clone https://github.com/creationix/nvm.git ~/.nvm && (cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) && source ~/.nvm/nvm.sh && nvm install 4
- npm install -g npm@'>=1.4.3'
- npm install
# Install ruby dependencies
Expand All @@ -49,4 +50,4 @@ after_script:
- cat TestResults/*.log
notifications:
slack:
secure: d0PFVLcyqcMxNtmZ8JaEpIBzXbUbZCgKs8QtBB5qEIXDnxflSR3AhczlILNhTBKtMEBlwVzsz65yP09XcmvB6xpAfJbHqSRzk2frKa5viPcAD8Wr/NYamt9/UiTCsnql8MqzjVy0tLdMscXKRmsUey4YF570zl0b7gAbq7XTqxM=
secure: d0PFVLcyqcMxNtmZ8JaEpIBzXbUbZCgKs8QtBB5qEIXDnxflSR3AhczlILNhTBKtMEBlwVzsz65yP09XcmvB6xpAfJbHqSRzk2frKa5viPcAD8Wr/NYamt9/UiTCsnql8MqzjVy0tLdMscXKRmsUey4YF570zl0b7gAbq7XTqxM=
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ else
{
@: @@@(method.HttpMethod.ToString().ToUpper())("@(method.Url.TrimStart('/'))")
}
if (method.ReturnType.Body.IsPrimaryType(KnownPrimaryType.Stream))
{
@: @@Streaming
}
@: Call<@method.CallType> @(method.Name)(@method.MethodParameterApiDeclaration);
@EmptyLine
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ else
{
@: @@@(method.HttpMethod.ToString().ToUpper())("@(method.Url.TrimStart('/'))")
}
if (method.ReturnType.Body.IsPrimaryType(KnownPrimaryType.Stream))
{
@: @@Streaming
}
@: Call<@method.CallType> @(method.Name)(@method.MethodParameterApiDeclaration);
@EmptyLine
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.Streaming;
import retrofit2.Response;
import retrofit2.Retrofit;

Expand Down Expand Up @@ -54,14 +55,17 @@ public FilesOperationsImpl(Retrofit retrofit, AutoRestSwaggerBATFileService clie
interface FilesService {
@Headers("Content-Type: application/json; charset=utf-8")
@GET("files/stream/nonempty")
@Streaming
Call<ResponseBody> getFile();

@Headers("Content-Type: application/json; charset=utf-8")
@GET("files/stream/verylarge")
@Streaming
Call<ResponseBody> getFileLarge();

@Headers("Content-Type: application/json; charset=utf-8")
@GET("files/stream/empty")
@Streaming
Call<ResponseBody> getEmptyFile();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import retrofit2.http.Part;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Streaming;
import retrofit2.Response;
import retrofit2.Retrofit;

Expand Down Expand Up @@ -61,10 +62,12 @@ public FormdataOperationsImpl(Retrofit retrofit, AutoRestSwaggerBATFormDataServi
interface FormdataService {
@Multipart
@POST("formdata/stream/uploadfile")
@Streaming
Call<ResponseBody> uploadFile(@Part("fileContent") RequestBody fileContent, @Part("fileName") String fileName);

@Headers("Content-Type: application/octet-stream")
@PUT("formdata/stream/uploadfile")
@Streaming
Call<ResponseBody> uploadFileViaBody(@Body RequestBody fileContent);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package fixtures.bodyfile;

import org.junit.Assert;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Ignore;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public class FilesTests {
private static AutoRestSwaggerBATFileService client;

@BeforeClass
public static void setup() {
client = new AutoRestSwaggerBATFileServiceImpl("http://localhost.:3000");
OkHttpClient.Builder builder = new OkHttpClient.Builder().readTimeout(1, TimeUnit.MINUTES);
client = new AutoRestSwaggerBATFileServiceImpl("http://localhost.:3000", builder, new Retrofit.Builder());
}

@Test
Expand All @@ -27,11 +31,10 @@ public void getFile() throws Exception {
}
}

@Ignore("This fails -- needs to be fixed")
@Test
public void getLargeFile() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
try (InputStream result = client.getFilesOperations().getFileLarge().getBody()) {
long streamSize = 3000 * 1024 * 1024;
long streamSize = 3000L * 1024L * 1024L;
long skipped = result.skip(streamSize);
Assert.assertEquals(streamSize, skipped);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public static void main(String[] args) throws Exception {
report.put("HttpRedirect301Put", 1);
report.put("HttpRedirect302Patch", 1);

report.put("FileStreamVeryLarge", 1);

int total = report.size();
int hit = 0;
List<String> missing = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,10 @@ public virtual List<string> ImplImports
.ForEach(p => imports.AddRange(p.ImportFrom()));
// return type
imports.AddRange(this.ReturnType.Body.ImportFrom(ServiceClient.Namespace, Namer));
if (ReturnType.Body.IsPrimaryType(KnownPrimaryType.Stream))
{
imports.Add("retrofit2.http.Streaming");
}
// response type (can be different from return type)
this.Responses.ForEach(r => imports.AddRange(r.Value.Body.ImportFrom(ServiceClient.Namespace, Namer)));
// Header type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ else
{
@: @@@(method.HttpMethod.ToString().ToUpper())("@(method.Url.TrimStart('/'))")
}
if (method.ReturnType.Body.IsPrimaryType(KnownPrimaryType.Stream))
{
@: @@Streaming
}
@: Call<@method.CallType> @(method.Name)(@method.MethodParameterApiDeclaration);
@EmptyLine
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ else
{
@: @@@(method.HttpMethod.ToString().ToUpper())("@(method.Url.TrimStart('/'))")
}
if (method.ReturnType.Body.IsPrimaryType(KnownPrimaryType.Stream))
{
@: @@Streaming
}
@: Call<@method.CallType> @(method.Name)(@method.MethodParameterApiDeclaration);
@EmptyLine
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ protected Object buildBody(int statusCode, ResponseBody responseBody) throws IOE
// Return raw response if InputStream is the target type
else if (type == InputStream.class) {
InputStream stream = responseBody.byteStream();
responseBody.close();
return stream;
}
// Deserialize
Expand Down