public DriverHTTP(final String uri, final String username, final String password, Client client) { Preconditions.checkArgument(!Strings.isNullOrEmpty(uri), "URI can not be null or empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "Username can not null or empty"); this.uri = uri; this.username = username; this.password = password; if (this.client == null) { this.client = new OkClient(new OkHttpClient()); } else { this.client = client; } this.restAdapter = new RestAdapter.Builder() .setEndpoint(this.uri) .setErrorHandler(new InfluxDBHTTPErrorHandler()) .setClient(this.client) .build(); this.restService = this.restAdapter.create(InfluxDBHTTPInterface.class); }
@Test public void shouldVerify() throws Exception { SSLSession sslSession = mock(SSLSession.class); X509Certificate certificate = mock(X509Certificate.class); Principal principal = new Principal() { @Override public String getName() { return "CN=lmis.cmam.gov.mz"; } }; when(sslSession.getPeerCertificates()).thenReturn(new Certificate[]{certificate}); when(certificate.getSubjectDN()).thenReturn(principal); LMISRestManagerMock.mockClient = mock(Client.class); LMISRestManagerMock lmisRestManagerMock = new LMISRestManagerMock(RuntimeEnvironment.application); lmisRestManagerMock.superGetSSLClient(); assertTrue(lmisRestManagerMock.okHttpClient.getHostnameVerifier().verify("whateverName", sslSession)); }
public DrupalOAuth2Manager(Context context, String endpoint, String clientId, String clientSecret) { setContext(context); setEndpoint(endpoint); setClientId(clientId); setClientSecret(clientSecret); Client client = new OkClient(getOkHttpClient()); if (mRequestInterceptor == null) { mRequestInterceptor = new SimpleRequestInterceptor(); } mRequestInterceptor.cookie = cookie; RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(endpoint) .setRequestInterceptor(mRequestInterceptor) .setErrorHandler(new ErrorHandler()) .setClient(client) .setConverter(new retrofit.converter.JacksonConverter()) .build(); mService = restAdapter.create(DrupalOAuth2.class); }
public DrupalService getService(String endpoint) { if (endpoint == null) return mService; if (mService == null) { this.endpoint = endpoint; Client client = new OkClient(getOkHttpClient()); if (mRequestInterceptor == null) { mRequestInterceptor = new SimpleRequestInterceptor(); } mRequestInterceptor.cookie = cookie; mRequestInterceptor.accessToken = accessToken; mRequestInterceptor.xcsrfToken = xcsrfToken; mService = new RestAdapter.Builder() .setEndpoint(endpoint) .setRequestInterceptor(mRequestInterceptor) .setErrorHandler(new ErrorHandler()) .setClient(client) .setConverter(new retrofit.converter.JacksonConverter()) .setLogLevel(RestAdapter.LogLevel.FULL) // Do this for development too. .build().create(DrupalService.class); } return mService; }
@Test public void errorWithEmptyBodyDoesNotCrash() { TestClient client = new RestAdapter.Builder() .setEndpoint("http://example.com") .setClient(new Client() { @Override public Response execute(Request request) throws IOException { return new Response("", 400, "invalid request", Collections.<Header>emptyList(), null); } }) .setErrorHandler(errorHandler) .setExecutors(new SynchronousExecutor(), new SynchronousExecutor()) .build() .create(TestClient.class); try { client.getFullMenus(); failBecauseExceptionWasNotThrown(ApiException.class); } catch (ApiException e) { assertThat(e.getMessage()).isNull(); assertThat(e.getCause()).isNotNull(); } }
@Test public void UnauthorizedOnNonAuthRouteThrowsCorrectException() { TestClient client = new RestAdapter.Builder() .setEndpoint("http://example.com") .setClient(new Client() { @Override public Response execute(Request request) throws IOException { Error apiError = new Error("1234", "Invalid social credentials"); TypedInput input = new TypedByteArray("application/x-protobuf", apiError.toByteArray()); return new Response("", 401, "invalid request", Collections.<Header>emptyList(), input); } }) .setErrorHandler(errorHandler) .setExecutors(new SynchronousExecutor(), new SynchronousExecutor()) .setConverter(new WireConverter()) .build() .create(TestClient.class); try { client.getFullMenus(); failBecauseExceptionWasNotThrown(ExpiredSessionException.class); } catch (ExpiredSessionException e) { // No-op } }
@Test public void unauthorizedOnAuthRouteThrowsCorrectException() { AuthService service = new RestAdapter.Builder() .setEndpoint("http://example.com") .setClient(new Client() { @Override public Response execute(Request request) throws IOException { Error apiError = new Error("1234", "Invalid social credentials"); TypedInput input = new TypedByteArray("application/x-protobuf", apiError.toByteArray()); return new Response("", 401, "invalid request", Collections.<Header>emptyList(), input); } }) .setErrorHandler(errorHandler) .setExecutors(new SynchronousExecutor(), new SynchronousExecutor()) .setConverter(new WireConverter()) .build() .create(AuthService.class); try { service.getAuth(new AuthRequest()); failBecauseExceptionWasNotThrown(SocialCredentialsException.class); } catch (SocialCredentialsException e) { // No-op } }
private RestAdapter(Server server, Client.Provider clientProvider, Executor httpExecutor, Executor callbackExecutor, RequestInterceptor requestInterceptor, Converter converter, Profiler profiler, ErrorHandler errorHandler, Log log, LogLevel logLevel) { this.server = server; this.clientProvider = clientProvider; this.httpExecutor = httpExecutor; if (Platform.HAS_RX_JAVA && httpExecutor != null) { this.rxSupport = new RxSupport(httpExecutor); } else { this.rxSupport = null; } this.callbackExecutor = callbackExecutor; this.requestInterceptor = requestInterceptor; this.converter = converter; this.profiler = profiler; this.errorHandler = errorHandler; this.log = log; this.logLevel = logLevel; }
/** * The HTTP client used for requests. */ public Builder setClient(final Client client) { if (client == null) { throw new NullPointerException("Client may not be null."); } return setClient(new Client.Provider() { @Override public Client get() { return client; } }); }
@SuppressWarnings("unchecked") // Mock profiler type erasure. @Before public void setUp() throws Exception { mockClient = mock(Client.class); mockRequestExecutor = spy(new SynchronousExecutor()); mockCallbackExecutor = spy(new SynchronousExecutor()); mockProfiler = mock(Profiler.class); example = new RestAdapter.Builder() // .setClient(mockClient) .setExecutors(mockRequestExecutor, mockCallbackExecutor) .setServer("http://example.com") .setProfiler(mockProfiler) .build() .create(Example.class); }
@Before public void setUp() throws IOException { Client client = mock(Client.class); doThrow(new AssertionError()).when(client).execute(any(Request.class)); httpExecutor = spy(new SynchronousExecutor()); callbackExecutor = spy(new SynchronousExecutor()); RestAdapter restAdapter = new RestAdapter.Builder() // .setClient(client) .setExecutors(httpExecutor, callbackExecutor) .setServer("http://example.com") .setLogLevel(RestAdapter.LogLevel.NONE) .build(); valueChangeListener = mock(ValueChangeListener.class); mockRestAdapter = MockRestAdapter.from(restAdapter); mockRestAdapter.setValueChangeListener(valueChangeListener); // Seed the random with a value so the tests are deterministic. mockRestAdapter.random.setSeed(2847); }
@Test public void testCreate4() { RobinServiceFactory factory = mock(RobinServiceFactory.class, Mockito.CALLS_REAL_METHODS); Credential credential = mock(Credential.class); Client client = mock(Client.class); HttpUrl endpoint = HttpUrl.parse("http://example.com"); RestAdapter.Builder builder = mock(RestAdapter.Builder.class, Mockito.CALLS_REAL_METHODS); doReturn(builder).when(factory).getRestAdapterBuilder(); MockService service = factory.create(MockService.class, credential, client, endpoint); assertThat(service).isNotNull(); verify(builder, times(1)).setRequestInterceptor(any(RequestInterceptor.class)); verify(builder, times(1)).setEndpoint(endpoint.toString()); }
public OAuthHandler(Client client, String tokenIssuingEndpoint, String username, String password, String clientId, String clientSecret) { super(); this.client = client; this.tokenIssuingEndpoint = tokenIssuingEndpoint; this.username = username; this.password = password; this.clientId = clientId; this.clientSecret = clientSecret; }
private static Client getClient() { return new UrlConnectionClient(); // OkHttpClient client = new OkHttpClient(); // client.setConnectTimeout(60, TimeUnit.SECONDS); // client.setWriteTimeout(60, TimeUnit.SECONDS); // client.setReadTimeout(60, TimeUnit.SECONDS); // return new OkClient(client); }
@Provides @Singleton RestAdapter provideRestAdapter(Endpoint endpoint, Client client, ErrorHandler errorHandler) { return new RestAdapter.Builder() // .setClient(client) // .setEndpoint(endpoint) // .setErrorHandler(errorHandler) .build(); }
public MockRestAdapter(final String responseString, final HashMap<String, String> headers) { this.exceptionReference = new AtomicReference<>(); this.requestReference = new AtomicReference<>(); this.resultReference = new AtomicReference<>(); this.mockRestAdapter = new UpholdRestAdapter(); this.restAdapter = new RestAdapter.Builder().setEndpoint(BuildConfig.API_SERVER_URL) .setRequestInterceptor(this.mockRestAdapter.getUpholdRequestInterceptor()) .setClient(new Client() { @Override public Response execute(Request request) throws IOException { requestReference.set(request); return new Response("some/url", 200, "reason", new ArrayList<Header>() {{ if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); add(new retrofit.client.Header(key, value)); } } }}, new TypedByteArray("application/json", responseString == null ? new byte[0] : responseString.getBytes())); } }).build(); this.mockRestAdapter.setAdapter(this.restAdapter); }
@Provides Client provideApiClient() { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS); return new OkClient(okHttpClient); }
@Provides @ApplicationScope AuthService provideAuthRestAdapter(Client client, Converter converter) { MockRestAdapter mockRestAdapter = MockRestAdapter.from( new RestAdapter.Builder().setClient(client).setConverter(converter).setLogLevel(RestAdapter.LogLevel.BASIC).setLog(new RestAdapter.Log() { @Override public void log(String message) { Timber.d(message); } }).setEndpoint("https://alexrwegener.com/").build()); mockRestAdapter.setDelay(100); return mockRestAdapter.create(AuthService.class, new MockAuthService()); }
public static RestAdapter buildBasicRestAdapter(PulsarEndpoint endpoint, Client client, RestErrorHandler errorHandler) { String endpointString = endpoint.get(); System.out.println("setting endpoint as " + endpointString); return new RestAdapter.Builder() .setLogLevel(RestAdapter.LogLevel.FULL) .setClient(client) .setEndpoint(endpointString) .setConverter(new JacksonConverter(JsonUtils.objectMapper)) .setErrorHandler(errorHandler) .build(); }
@Override public Client get() { final OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS); okHttpClient.setConnectTimeout(configuration.getConnectionTimeout(), TimeUnit.MILLISECONDS); return new OkClient(okHttpClient); }
@Provides @Singleton RestAdapter provideRestAdapter(Endpoint endpoint, Client client, Gson gson, SharedPreferences preferences) { return new RestAdapter.Builder() .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.HEADERS : RestAdapter.LogLevel.NONE) .setClient(client) .setConverter(new GsonConverter(gson)) .setRequestInterceptor(new ApiHeaders()) .setEndpoint(endpoint) .build(); }
private RestAdapter(Server server, Client.Provider clientProvider, Executor httpExecutor, Executor callbackExecutor, RequestInterceptor requestInterceptor, Converter converter, Profiler profiler, ErrorHandler errorHandler, Log log, LogLevel logLevel) { this.server = server; this.clientProvider = clientProvider; this.httpExecutor = httpExecutor; this.callbackExecutor = callbackExecutor; this.requestInterceptor = requestInterceptor; this.converter = converter; this.profiler = profiler; this.errorHandler = errorHandler; this.log = log; this.logLevel = logLevel; }
/** * The HTTP client used for requests. */ public Builder setClient(Client.Provider clientProvider) { if (clientProvider == null) { throw new NullPointerException("Client provider may not be null."); } this.clientProvider = clientProvider; return this; }
public static <T> T create(String endpoint, Class<T> service, Client client){ RestAdapter adapter = new RestAdapter.Builder() .setEndpoint(endpoint) .setLogLevel(RestAdapter.LogLevel.BASIC) .setClient(client) .build(); return adapter.create(service); }
@Provides @Singleton RestAdapter provideRestAdapter(Endpoint endpoint, Client client) { return new RestAdapter.Builder() .setClient(client) .setLogLevel(RestAdapter.LogLevel.FULL) .setEndpoint(endpoint) .build(); }
@Provides @Singleton Client provideClient(OkHttpClient client) { client = new OkHttpClient(); client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout client.setConnectionPool(new ConnectionPool(0, 5 * 60 * 1000)); return new OkClient(client); }
@Provides @Singleton BitcoinAverage provideBitcoinAverage(Client client) { RestAdapter restAdapter = new RestAdapter.Builder() .setClient(client) .setLogLevel(RestAdapter.LogLevel.FULL) .setEndpoint(BITCOIN_AVERAGE_API_ENDPOINT) .build(); return restAdapter.create(BitcoinAverage.class); }
@Provides @Singleton CoinbaseMarket provideCoinbaseMarket(Client client) { RestAdapter restAdapter = new RestAdapter.Builder() .setClient(client) .setLogLevel(RestAdapter.LogLevel.NONE) .setEndpoint(COINBASE_API_ENDPOINT) .build(); return restAdapter.create(CoinbaseMarket.class); }
@Provides @Singleton BitstampExchange provideBitstampExchange(Client client) { RestAdapter restAdapter = new RestAdapter.Builder() .setClient(client) .setLogLevel(RestAdapter.LogLevel.NONE) .setEndpoint(BITSTAMP_API_ENDPOINT) .build(); return restAdapter.create(BitstampExchange.class); }
@Provides @Singleton Bluelytics provideBluelytics(Client client) { RestAdapter restAdapter = new RestAdapter.Builder() .setClient(client) .setLogLevel(RestAdapter.LogLevel.NONE) .setEndpoint(BLUELYTICS_API_ENDPOINT) .build(); return restAdapter.create(Bluelytics.class); }
@ApplicationScope @Provides protected GithubApi provideGithubApi(Client client, Gson gson) { RestAdapter restAdapter = new RestAdapter.Builder() .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE) .setClient(client) .setConverter(new GsonConverter(gson)) .setEndpoint("https://api.github.com") .build(); return restAdapter.create(GithubApi.class); }
@ApplicationScope @Provides protected BitbucketApi provideGithubApi(Client client, Gson gson) { RestAdapter restAdapter = new RestAdapter.Builder() .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE) .setClient(client) .setConverter(new GsonConverter(gson)) .setEndpoint("https://api.bitbucket.org/2.0") .build(); return restAdapter.create(BitbucketApi.class); }
public AuthServiceImpl(SphereAuthConfig sphereAuthConfig, Client client) { this.sphereAuthConfig = sphereAuthConfig; RestAdapter restAdapter = new RestAdapter.Builder() .setClient(client) .setEndpoint(sphereAuthConfig.getAuthUrl()).build(); this.authAPIService = restAdapter.create(AuthAPIService.class); }
@Provides @Singleton RestAdapter provideRestAdapter(Client client, Endpoint endpoint, Converter converter) { return new RestAdapter.Builder() .setClient(client) .setEndpoint(endpoint) .setConverter(converter) .setLogLevel(RestAdapter.LogLevel.FULL) .build(); }