Search in sources :

Example 6 with DoctorEndpointResponse

use of com.facebook.buck.doctor.config.DoctorEndpointResponse in project buck by facebook.

the class DoctorReportHelper method uploadRequest.

public DoctorEndpointResponse uploadRequest(DoctorEndpointRequest request) {
    if (!doctorConfig.getEndpointUrl().isPresent()) {
        String errorMsg = String.format("Doctor endpoint URL is not set. Please set [%s] %s on your .buckconfig", DoctorConfig.DOCTOR_SECTION, DoctorConfig.URL_FIELD);
        return createErrorDoctorEndpointResponse(errorMsg);
    }
    byte[] requestJson;
    try {
        requestJson = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse("Failed to encode request to JSON. " + "Reason: " + e.getMessage());
    }
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).readTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).writeTimeout(doctorConfig.getHttpTimeoutMs(), TimeUnit.MILLISECONDS).build();
    Response httpResponse;
    try {
        RequestBody requestBody;
        ImmutableMap<String, String> extraArgs = doctorConfig.getExtraRequestArgs();
        if (extraArgs.isEmpty()) {
            requestBody = RequestBody.create(JSON, requestJson);
        } else {
            FormBody.Builder formBody = new FormBody.Builder();
            formBody.add("data", new String(requestJson));
            for (Map.Entry<String, String> entry : extraArgs.entrySet()) {
                formBody.add(entry.getKey(), entry.getValue());
            }
            requestBody = formBody.build();
        }
        Request httpRequest = new Request.Builder().url(doctorConfig.getEndpointUrl().get()).post(requestBody).build();
        httpResponse = httpClient.newCall(httpRequest).execute();
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse("Failed to perform the request to " + doctorConfig.getEndpointUrl().get() + ". Reason: " + e.getMessage());
    }
    try {
        if (httpResponse.isSuccessful()) {
            String body = new String(httpResponse.body().bytes(), Charsets.UTF_8);
            return objectMapper.readValue(body, DoctorEndpointResponse.class);
        }
        return createErrorDoctorEndpointResponse("Request was not successful.");
    } catch (JsonProcessingException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    } catch (IOException e) {
        return createErrorDoctorEndpointResponse(String.format(DECODE_FAIL_TEMPLATE, e.getMessage()));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) FormBody(okhttp3.FormBody) DoctorEndpointRequest(com.facebook.buck.doctor.config.DoctorEndpointRequest) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) DoctorEndpointResponse(com.facebook.buck.doctor.config.DoctorEndpointResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RequestBody(okhttp3.RequestBody)

Aggregations

DoctorEndpointResponse (com.facebook.buck.doctor.config.DoctorEndpointResponse)6 DoctorEndpointRequest (com.facebook.buck.doctor.config.DoctorEndpointRequest)4 UserInputFixture (com.facebook.buck.rage.UserInputFixture)4 Test (org.junit.Test)4 DoctorConfig (com.facebook.buck.doctor.config.DoctorConfig)3 BuildLogEntry (com.facebook.buck.rage.BuildLogEntry)3 BuildLogHelper (com.facebook.buck.rage.BuildLogHelper)3 TestConsole (com.facebook.buck.testutil.TestConsole)2 DoctorReportHelper (com.facebook.buck.doctor.DoctorReportHelper)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 DefectSubmitResult (com.facebook.buck.rage.DefectSubmitResult)1 UserInput (com.facebook.buck.rage.UserInput)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Map (java.util.Map)1 FormBody (okhttp3.FormBody)1 OkHttpClient (okhttp3.OkHttpClient)1