Search in sources :

Example 1 with Builder

use of com.squareup.moshi.Moshi.Builder in project ApplicationInsights-Java by microsoft.

the class StatusFileTests method parseJsonFile.

Map parseJsonFile(File tempFolder) throws IOException {
    JsonAdapter<Map> adapter = new Builder().build().adapter(Map.class);
    String fileName = StatusFile.constructFileName(StatusFile.getJsonMap());
    String contents = new String(Files.readAllBytes(new File(tempFolder, fileName).toPath()), UTF_8);
    return adapter.fromJson(contents);
}
Also used : Builder(com.squareup.moshi.Moshi.Builder) Map(java.util.Map) File(java.io.File)

Example 2 with Builder

use of com.squareup.moshi.Moshi.Builder in project ApplicationInsights-Java by microsoft.

the class ProfilerFrontendClientV2 method reportUploadFinish.

/**
 * Report to Service Profiler that the profile upload has been completed.
 */
@Override
public Mono<ArtifactAcceptedResponse> reportUploadFinish(UUID profileId, String etag) {
    URL requestUrl = uploadFinishedRequestUrl(profileId, etag);
    return executePostWithRedirect(requestUrl).flatMap(response -> {
        if (response == null) {
            // this shouldn't happen, the mono should complete with a response or a failure
            return Mono.error(new AssertionError("http response mono returned empty"));
        }
        int statusCode = response.getStatusCode();
        if (statusCode != 201 && statusCode != 202) {
            LOGGER.error("Trace upload failed: {}", statusCode);
            consumeResponseBody(response);
            return Mono.error(new AssertionError("http request failed"));
        }
        return response.getBodyAsString();
    }).flatMap(json -> {
        if (json == null) {
            // this shouldn't happen, the mono should complete with a response or a failure
            return Mono.error(new AssertionError("response body mono returned empty"));
        }
        try {
            JsonAdapter<ArtifactAcceptedResponse> builder = new Builder().build().adapter(ArtifactAcceptedResponse.class);
            ArtifactAcceptedResponse data = builder.fromJson(json);
            if (data == null) {
                return Mono.error(new IllegalStateException("Failed to deserialize response"));
            }
            return Mono.just(data);
        } catch (IOException e) {
            return Mono.error(new IllegalStateException("Failed to deserialize response", e));
        }
    });
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) ArtifactAcceptedResponse(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse) JsonAdapter(com.squareup.moshi.JsonAdapter) BlobAccessPass(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.BlobAccessPass) TimestampContract(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.TimestampContract) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Date(java.util.Date) HttpPipeline(com.azure.core.http.HttpPipeline) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) HttpResponseException(com.azure.core.exception.HttpResponseException) HttpRequest(com.azure.core.http.HttpRequest) HttpMethod(com.azure.core.http.HttpMethod) Builder(com.squareup.moshi.Moshi.Builder) ArtifactAcceptedResponse(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse) Builder(com.squareup.moshi.Moshi.Builder) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

Builder (com.squareup.moshi.Moshi.Builder)2 HttpResponseException (com.azure.core.exception.HttpResponseException)1 HttpMethod (com.azure.core.http.HttpMethod)1 HttpPipeline (com.azure.core.http.HttpPipeline)1 HttpRequest (com.azure.core.http.HttpRequest)1 HttpResponse (com.azure.core.http.HttpResponse)1 ArtifactAcceptedResponse (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.ArtifactAcceptedResponse)1 BlobAccessPass (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.BlobAccessPass)1 TimestampContract (com.microsoft.applicationinsights.serviceprofilerapi.client.contract.TimestampContract)1 JsonAdapter (com.squareup.moshi.JsonAdapter)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Date (java.util.Date)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Mono (reactor.core.publisher.Mono)1