Search in sources :

Example 6 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project natural-voice-mobile-sdk-android by aimmatic.

the class AimMatic method fetchUserProfile.

/**
 * Fetch user profile
 *
 * @param token    a valid token
 * @param callback a callback function to notice when request is success or failure
 */
public static void fetchUserProfile(String token, final Callback<Profile> callback) {
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder().url(String.format("%s/v1/profile", service)).addHeader("Authorization", "Bearer " + token).build();
    okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            callback.onError(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try {
                ProfileResponse profileResponse = new Gson().fromJson(response.body().string(), ProfileResponse.class);
                callback.onSuccess(profileResponse.getProfile());
            } catch (Exception e) {
                if (response.code() != 200) {
                    callback.onError(new Exception("Server reply with status " + response.code()));
                } else {
                    callback.onError(e);
                }
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Gson(com.google.gson.Gson) IOException(java.io.IOException) IOException(java.io.IOException)

Example 7 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project intellij-common by redhat-developer.

the class NetworkUtils method getClient.

public static OkHttpClient getClient() {
    final HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
    final IdeaWideProxySelector ideaWideProxySelector = new IdeaWideProxySelector(httpConfigurable);
    final IdeaWideAuthenticator ideaWideAuthenticator = new IdeaWideAuthenticator(httpConfigurable);
    final Authenticator proxyAuthenticator = getProxyAuthenticator(ideaWideAuthenticator);
    final OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.proxySelector(ideaWideProxySelector).proxyAuthenticator(proxyAuthenticator);
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IdeaWideProxySelector(com.intellij.util.net.IdeaWideProxySelector) HttpConfigurable(com.intellij.util.net.HttpConfigurable) IdeaWideAuthenticator(com.intellij.util.net.IdeaWideAuthenticator) Authenticator(okhttp3.Authenticator) IdeaWideAuthenticator(com.intellij.util.net.IdeaWideAuthenticator)

Example 8 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project fineract by apache.

the class ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl method okHttpConnectionMethod.

@Transactional
@Override
@SuppressWarnings("deprecation")
public String okHttpConnectionMethod(String userName, String password, String subscriptionKey, String subscriptionId, String url, String token, File file, FormDataContentDisposition fileData, Long uniqueId, String nrcId, String process) {
    String reponseMessage = null;
    RequestBody requestBody = null;
    OkHttpClient client = new OkHttpClient();
    if (process.equals("UploadCreditReport")) {
        String fileName = fileData.getFileName();
        requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
        requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, requestBody).addFormDataPart("BODY", "formdata").addFormDataPart("userName", userName).build();
    } else if (process.equals("token")) {
        final MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String jsonBody = "" + "BODY=x-www-form-urlencoded&\r" + "grant_type=password&\r" + "userName=" + userName + "&\r" + "password=" + password + "&\r";
        requestBody = RequestBody.create(jsonBody, mediaType);
    } else if (process.equals("NRC")) {
        final MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String jsonBody = "BODY=x-www-form-urlencoded&nrc=" + nrcId + "&";
        requestBody = RequestBody.create(jsonBody, mediaType);
    }
    HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
    String urlokhttp = urlBuilder.build().toString();
    Request request = null;
    if (token == null) {
        request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").url(urlokhttp).post(requestBody).build();
    }
    if (token != null) {
        if (process.equals("CreditReport")) {
            // GET method for fetching credit report
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").header("Authorization", "Bearer " + token).url(urlokhttp).get().build();
        } else if (process.equals("UploadCreditReport")) {
            // POST for uploading Credit-Report(multipart/form-data)
            // To ThitsaWork
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "multipart/form-data").header("Authorization", "Bearer " + token).url(urlokhttp).post(requestBody).build();
        } else {
            // POST method for application/x-www-form-urlencoded
            request = new Request.Builder().header("mcix-subscription-key", subscriptionKey).header("mcix-subscription-id", subscriptionId).header("Content-Type", "application/x-www-form-urlencoded").header("Authorization", "Bearer " + token).url(urlokhttp).post(requestBody).build();
        }
    }
    Response response;
    Integer responseCode = 0;
    try {
        response = client.newCall(request).execute();
        responseCode = response.code();
        reponseMessage = response.body().string();
    } catch (IOException e) {
        LOG.error("error occured in HTTP request-response method.", e);
    }
    if (responseCode != HttpURLConnection.HTTP_OK) {
        this.httpResponse(responseCode, reponseMessage);
    }
    if (process.equals("UploadCreditReport")) {
        // to show the Response on frontEnd
        JsonObject reportObject = JsonParser.parseString(reponseMessage).getAsJsonObject();
        String ResponseMessageJson = reportObject.get("ResponseMessage").getAsString();
        this.handleAPIIntegrityIssues(ResponseMessageJson);
    }
    return reponseMessage;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) CommandWrapperBuilder(org.apache.fineract.commands.service.CommandWrapperBuilder) DataValidatorBuilder(org.apache.fineract.infrastructure.core.data.DataValidatorBuilder) Request(okhttp3.Request) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project fineract by apache.

the class ProcessorHelper method createWebHookService.

public WebHookService createWebHookService(final String url) {
    final OkHttpClient client = createClient();
    final Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
    retrofitBuilder.baseUrl(url);
    retrofitBuilder.client(client);
    retrofitBuilder.addConverterFactory(GsonConverterFactory.create());
    final Retrofit retrofit = retrofitBuilder.build();
    return retrofit.create(WebHookService.class);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient)

Example 10 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project axonserver-connector-java by AxonIQ.

the class AbstractAxonServerIntegrationTest method initialize.

@BeforeAll
static void initialize() throws IOException {
    client = new OkHttpClient();
    axonServerAddress = new ServerAddress(toxiProxyContainer.getContainerIpAddress(), toxiProxyContainer.getMappedPort(8124));
    axonServerHttpPort = new ServerAddress(axonServerContainer.getContainerIpAddress(), axonServerContainer.getMappedPort(8024));
    ToxiproxyClient client = new ToxiproxyClient(toxiProxyContainer.getContainerIpAddress(), toxiProxyContainer.getMappedPort(8474));
    axonServerProxy = getOrCreateProxy(client, "axonserver", "0.0.0.0:8124", "axonserver:8124");
}
Also used : OkHttpClient(org.testcontainers.shaded.okhttp3.OkHttpClient) ServerAddress(io.axoniq.axonserver.connector.impl.ServerAddress) ToxiproxyClient(eu.rekawek.toxiproxy.ToxiproxyClient) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)1944 Request (okhttp3.Request)1024 Response (okhttp3.Response)880 IOException (java.io.IOException)567 Test (org.junit.Test)365 Call (okhttp3.Call)290 RequestBody (okhttp3.RequestBody)222 Test (org.junit.jupiter.api.Test)145 Retrofit (retrofit2.Retrofit)138 File (java.io.File)132 HttpUrl (okhttp3.HttpUrl)131 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)128 Callback (okhttp3.Callback)117 JSONObject (org.json.JSONObject)110 ArrayList (java.util.ArrayList)106 ResponseBody (okhttp3.ResponseBody)105 Gson (com.google.gson.Gson)98 MediaType (okhttp3.MediaType)98 List (java.util.List)92 Map (java.util.Map)85