@Test(expected = RequestException.class) public void testInvalidResponse() throws RequestException { testRequest.responseBody = "what"; testRequest.responseStatus = 200; client.getToken("some channel"); }
@NonNull public Response<AuthToken> getToken(@NonNull final String channelId) throws RequestException { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(AUTH_PATH) .build(); String bearerToken; try { bearerToken = createBearerToken(channelId); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw new RequestException("Unable to create bearer token.", e); } final long requestTime = clock.currentTimeMillis(); return requestFactory.createRequest() .setOperation("GET", url) .setAirshipJsonAcceptsHeader() .setHeader("X-UA-App-Key", runtimeConfig.getConfigOptions().appKey) .setHeader("X-UA-Channel", channelId) .setHeader("Authorization", "Bearer " + bearerToken) .execute(new ResponseParser<AuthToken>() { @Override public AuthToken parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return AuthApiClient.parseResponse(responseBody, channelId, requestTime); } return null; } }); }
AuthApiClient { @NonNull public Response<AuthToken> getToken(@NonNull final String channelId) throws RequestException { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(AUTH_PATH) .build(); String bearerToken; try { bearerToken = createBearerToken(channelId); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw new RequestException("Unable to create bearer token.", e); } final long requestTime = clock.currentTimeMillis(); return requestFactory.createRequest() .setOperation("GET", url) .setAirshipJsonAcceptsHeader() .setHeader("X-UA-App-Key", runtimeConfig.getConfigOptions().appKey) .setHeader("X-UA-Channel", channelId) .setHeader("Authorization", "Bearer " + bearerToken) .execute(new ResponseParser<AuthToken>() { @Override public AuthToken parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return AuthApiClient.parseResponse(responseBody, channelId, requestTime); } return null; } }); } }
AuthApiClient { @NonNull public Response<AuthToken> getToken(@NonNull final String channelId) throws RequestException { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(AUTH_PATH) .build(); String bearerToken; try { bearerToken = createBearerToken(channelId); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw new RequestException("Unable to create bearer token.", e); } final long requestTime = clock.currentTimeMillis(); return requestFactory.createRequest() .setOperation("GET", url) .setAirshipJsonAcceptsHeader() .setHeader("X-UA-App-Key", runtimeConfig.getConfigOptions().appKey) .setHeader("X-UA-Channel", channelId) .setHeader("Authorization", "Bearer " + bearerToken) .execute(new ResponseParser<AuthToken>() { @Override public AuthToken parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return AuthApiClient.parseResponse(responseBody, channelId, requestTime); } return null; } }); } AuthApiClient(AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  AuthApiClient(AirshipRuntimeConfig runtimeConfig, Clock clock, RequestFactory requestFactory); }
AuthApiClient { @NonNull public Response<AuthToken> getToken(@NonNull final String channelId) throws RequestException { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(AUTH_PATH) .build(); String bearerToken; try { bearerToken = createBearerToken(channelId); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw new RequestException("Unable to create bearer token.", e); } final long requestTime = clock.currentTimeMillis(); return requestFactory.createRequest() .setOperation("GET", url) .setAirshipJsonAcceptsHeader() .setHeader("X-UA-App-Key", runtimeConfig.getConfigOptions().appKey) .setHeader("X-UA-Channel", channelId) .setHeader("Authorization", "Bearer " + bearerToken) .execute(new ResponseParser<AuthToken>() { @Override public AuthToken parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return AuthApiClient.parseResponse(responseBody, channelId, requestTime); } return null; } }); } AuthApiClient(AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  AuthApiClient(AirshipRuntimeConfig runtimeConfig, Clock clock, RequestFactory requestFactory); @NonNull Response<AuthToken> getToken(@NonNull final String channelId); }
AuthApiClient { @NonNull public Response<AuthToken> getToken(@NonNull final String channelId) throws RequestException { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(AUTH_PATH) .build(); String bearerToken; try { bearerToken = createBearerToken(channelId); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw new RequestException("Unable to create bearer token.", e); } final long requestTime = clock.currentTimeMillis(); return requestFactory.createRequest() .setOperation("GET", url) .setAirshipJsonAcceptsHeader() .setHeader("X-UA-App-Key", runtimeConfig.getConfigOptions().appKey) .setHeader("X-UA-Channel", channelId) .setHeader("Authorization", "Bearer " + bearerToken) .execute(new ResponseParser<AuthToken>() { @Override public AuthToken parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return AuthApiClient.parseResponse(responseBody, channelId, requestTime); } return null; } }); } AuthApiClient(AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  AuthApiClient(AirshipRuntimeConfig runtimeConfig, Clock clock, RequestFactory requestFactory); @NonNull Response<AuthToken> getToken(@NonNull final String channelId);  }