Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit b4e587a0 authored by Tobias Kaminsky's avatar Tobias Kaminsky Committed by GitHub
Browse files

Merge pull request #543 from desperateCoder/master

add response headers to responses for `Call<T>` Retrofit methods
parents 58ae5acc 67a97cb7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ public class NextcloudAPI {
        return convertStreamToTargetEntity(response.getBody(), type);
    }

    private <T> T convertStreamToTargetEntity(InputStream inputStream, Type targetEntity) throws IOException {
    public <T> T convertStreamToTargetEntity(InputStream inputStream, Type targetEntity) throws IOException {
        final T result;
        try (InputStream os = inputStream;
             Reader targetReader = new InputStreamReader(os)) {
+16 −2
Original line number Diff line number Diff line
@@ -3,11 +3,16 @@ package com.nextcloud.android.sso.helper;
import androidx.annotation.NonNull;

import com.nextcloud.android.sso.aidl.NextcloudRequest;
import com.nextcloud.android.sso.api.AidlNetworkRequest;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
@@ -53,8 +58,17 @@ public final class Retrofit2Helper {
            @Override
            public Response<T> execute() {
                try {
                    T body = nextcloudAPI.performRequestV2(resType, nextcloudRequest);
                    return Response.success(body);
                    com.nextcloud.android.sso.api.Response response = nextcloudAPI.performNetworkRequestV2(nextcloudRequest);

                    T body = nextcloudAPI.convertStreamToTargetEntity(response.getBody(), resType);
                    Map<String, String> headerMap = new HashMap<>();
                    ArrayList<AidlNetworkRequest.PlainHeader> plainHeaders = response.getPlainHeaders();
                    if (plainHeaders != null) {
                        for (AidlNetworkRequest.PlainHeader header : plainHeaders) {
                            headerMap.put(header.getName(), header.getValue());
                        }
                    }
                    return Response.success(body, Headers.of(headerMap));
                } catch (NextcloudHttpRequestFailedException e) {
                    final Throwable cause = e.getCause();
                    return convertExceptionToResponse(e.getStatusCode(), cause == null ? e.getMessage() : cause.getMessage());
+20 −10
Original line number Diff line number Diff line
@@ -113,7 +113,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<List<String>>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -199,7 +200,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<List<String>>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -260,7 +262,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -319,7 +322,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -350,7 +354,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -377,7 +382,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -401,7 +407,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<ResponseBody>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -426,7 +433,8 @@ public class TestRetrofitAPI {

        Type type = new TypeToken<ResponseBody>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -454,7 +462,8 @@ public class TestRetrofitAPI {
        Type type = new TypeToken<ResponseBody>() {
        }.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }
@@ -482,7 +491,8 @@ public class TestRetrofitAPI {
        Type type = new TypeToken<ResponseBody>() {
        }.getType();
        try {
            verify(nextcloudApiMock).performRequestV2(eq(type), eq(request));
            verify(nextcloudApiMock).performNetworkRequestV2(eq(request));
            verify(nextcloudApiMock).convertStreamToTargetEntity(any(), eq(type));
        } catch (Exception e) {
            fail(e.getMessage());
        }