Search in sources :

Example 1 with Header

use of retrofit.client.Header in project enroscar by stanfy.

the class RetrofitClient method readResponse.

private Response readResponse(URLConnection connection) throws IOException {
    int status = HttpURLConnection.HTTP_OK;
    String reason = "";
    if (connection instanceof HttpURLConnection) {
        status = ((HttpURLConnection) connection).getResponseCode();
        reason = ((HttpURLConnection) connection).getResponseMessage();
    }
    List<Header> headers = new ArrayList<>();
    for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
        String name = field.getKey();
        for (String value : field.getValue()) {
            headers.add(new Header(name, value));
        }
    }
    String mimeType = connection.getContentType();
    int length = connection.getContentLength();
    InputStream stream;
    if (status >= 400 && connection instanceof HttpURLConnection) {
        stream = ((HttpURLConnection) connection).getErrorStream();
    } else {
        stream = connection.getInputStream();
    }
    TypedInput responseBody = new TypedInputStream(mimeType, length, stream);
    return new Response(connection.getURL().toString(), status, reason, headers, responseBody);
}
Also used : InputStream(java.io.InputStream) TypedInput(retrofit.mime.TypedInput) ArrayList(java.util.ArrayList) Response(retrofit.client.Response) HttpURLConnection(java.net.HttpURLConnection) Header(retrofit.client.Header) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with Header

use of retrofit.client.Header in project enroscar by stanfy.

the class RetrofitClient method prepareRequest.

private void prepareRequest(final URLConnection connection, final Request request) throws IOException {
    if (connection instanceof HttpURLConnection) {
        // HttpURLConnection artificially restricts request method
        try {
            ((HttpURLConnection) connection).setRequestMethod(request.getMethod());
        } catch (ProtocolException e) {
            try {
                methodField.set(connection, request.getMethod());
            } catch (IllegalAccessException e1) {
                throw RetrofitError.unexpectedError(getUrl(request), e1);
            }
        }
    }
    connection.setDoInput(true);
    for (Header header : request.getHeaders()) {
        connection.addRequestProperty(header.getName(), header.getValue());
    }
    TypedOutput body = request.getBody();
    if (body != null) {
        connection.setDoOutput(true);
        connection.addRequestProperty("Content-Type", body.mimeType());
        long length = body.length();
        if (length != -1) {
            connection.addRequestProperty("Content-Length", String.valueOf(length));
        }
        if (connection instanceof HttpURLConnection) {
            if (length != -1) {
                ((HttpURLConnection) connection).setFixedLengthStreamingMode((int) length);
            } else {
                ((HttpURLConnection) connection).setChunkedStreamingMode(CHUNK_SIZE);
            }
        }
        body.writeTo(connection.getOutputStream());
    }
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) Header(retrofit.client.Header) TypedOutput(retrofit.mime.TypedOutput)

Example 3 with Header

use of retrofit.client.Header in project Varis-Android by dkhmelenko.

the class TestAuthPresenter method testTwoFactorAuth.

@Test
public void testTwoFactorAuth() {
    final String login = "login";
    final String password = "password";
    String auth = EncryptionUtils.generateBasicAuthorization(login, password);
    // rules for throwing a request for 2-factor auth
    Header header = new Header(GithubApiService.TWO_FACTOR_HEADER, "required");
    List<Header> headers = new ArrayList<>();
    headers.add(header);
    Response response = new Response("https://github.com", 401, "twoFactorAuth", headers, null);
    TaskError taskError = new TaskError(401, "twoFactorAuth");
    taskError.setResponse(response);
    TaskException exception = new TaskException(taskError);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenThrow(exception);
    mAuthPresenter.login(login, password);
    verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
    verify(mAuthView).showTwoFactorAuth();
    // rules for handling 2-factor auth continuation
    final String securityCode = "123456";
    final String gitHubToken = "gitHubToken";
    Authorization authorization = new Authorization();
    authorization.setToken(gitHubToken);
    authorization.setId(1L);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), eq(securityCode), any(AuthorizationRequest.class))).thenReturn(authorization);
    final String accessToken = "token";
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(gitHubToken);
    AccessToken token = new AccessToken();
    token.setAccessToken(accessToken);
    when(mTravisRestClient.getApiService().auth(request)).thenReturn(token);
    when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
    mAuthPresenter.twoFactorAuth(securityCode);
    verify(mTaskManager).startAuth(gitHubToken);
    verify(mTaskManager).deleteAuthorization(auth, String.valueOf(authorization.getId()), securityCode);
    verify(mAuthView, times(2)).hideProgress();
    verify(mAuthView).finishView();
}
Also used : Response(retrofit.client.Response) Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) Header(retrofit.client.Header) TaskException(com.khmelenko.lab.varis.task.TaskException) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) ArrayList(java.util.ArrayList) TaskError(com.khmelenko.lab.varis.task.TaskError) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) Test(org.junit.Test)

Aggregations

Header (retrofit.client.Header)3 HttpURLConnection (java.net.HttpURLConnection)2 ArrayList (java.util.ArrayList)2 Response (retrofit.client.Response)2 AccessTokenRequest (com.khmelenko.lab.varis.network.request.AccessTokenRequest)1 AuthorizationRequest (com.khmelenko.lab.varis.network.request.AuthorizationRequest)1 AccessToken (com.khmelenko.lab.varis.network.response.AccessToken)1 Authorization (com.khmelenko.lab.varis.network.response.Authorization)1 TaskError (com.khmelenko.lab.varis.task.TaskError)1 TaskException (com.khmelenko.lab.varis.task.TaskException)1 InputStream (java.io.InputStream)1 ProtocolException (java.net.ProtocolException)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 TypedInput (retrofit.mime.TypedInput)1 TypedOutput (retrofit.mime.TypedOutput)1