package io.qameta.allure.okhttp3;

import io.qameta.allure.attachment.DefaultAttachmentProcessor;
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
import io.qameta.allure.attachment.http.HttpRequestAttachment;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;

/**
 * Allure interceptor logger for Retrofit.
 */
public class AllureOkHttp3 implements Interceptor {

    @SuppressWarnings("NullableProblems")
    @Override
    public Response intercept(final Chain chain) throws IOException {
        final Request request = chain.request();
        final HttpRequestAttachment.Builder builder = create("Request", request.url().toString())
                .withHeaders(toMapConverter(request.headers().toMultimap()));

        final RequestBody requestBody = request.body();
        if (Objects.nonNull(requestBody)) {
            builder.withBody(readRequestBody(requestBody));
        }
        final HttpRequestAttachment requestAttachment = builder.build();
        new DefaultAttachmentProcessor().addAttachment(
                requestAttachment,
                new FreemarkerAttachmentRenderer("http-request.ftl")
        );
        return chain.proceed(request);

    }

    private static Map<String, String> toMapConverter(final Map<String, List<String>> items) {
        final Map<String, String> result = new HashMap<>();
        items.forEach((key, value) -> result.put(key, String.join("; ", value)));
        return result;
    }

    private static String readRequestBody(final RequestBody requestBody) throws IOException {
        final Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);
        return buffer.readString(StandardCharsets.UTF_8);
    }
}

@@ -28,16 +28,123 @@
 http3;%0A%0A
+import io.qameta.allure.attachment.AttachmentData;%0Aimport io.qameta.allure.attachment.AttachmentProcessor;%0A
 import i
@@ -326,16 +326,80 @@
 chment;%0A
+import io.qameta.allure.attachment.http.HttpResponseAttachment;%0A
 import o
@@ -495,16 +495,45 @@
 sponse;%0A
+import okhttp3.ResponseBody;%0A
 import o
@@ -717,94 +717,8 @@
 s;%0A%0A
-import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;%0A%0A
 /**%0A
@@ -821,49 +821,8 @@
  %7B%0A%0A
-    @SuppressWarnings(%22NullableProblems%22)
 %0A   
@@ -920,157 +920,347 @@
 nal 
-Request request = chain.request();%0A        final HttpRequestAttachment.Builder builder = create(%22Request%22, request.url().toString())%0A                
+AttachmentProcessor%3CAttachmentData%3E processor = new DefaultAttachmentProcessor();%0A%0A        final Request request = chain.request();%0A        final String requestUrl = request.url().toString();%0A        final HttpRequestAttachment.Builder requestAttachmentBuilder = HttpRequestAttachment.Builder%0A                .create(%22Request%22, requestUrl)
 .wit
@@ -1430,17 +1430,34 @@
         
-b
+requestAttachmentB
 uilder.w
@@ -1561,17 +1561,34 @@
 hment = 
-b
+requestAttachmentB
 uilder.b
@@ -1607,106 +1607,937 @@
     
-new DefaultAttachmentProcessor().addAttachment(%0A                requestAttachment,%0A               
+processor.addAttachment(requestAttachment, new FreemarkerAttachmentRenderer(%22http-request.ftl%22));%0A%0A        final Response response = chain.proceed(request);%0A        final HttpResponseAttachment.Builder responseAttachmentBuilder = HttpResponseAttachment.Builder%0A                .create(%22Response%22).withHeaders(toMapConverter(response.headers().toMultimap()));%0A%0A        final Response.Builder responseBuilder = response.newBuilder();%0A%0A        final ResponseBody responseBody = response.body();%0A%0A        if (Objects.nonNull(responseBody)) %7B%0A            final byte%5B%5D bytes = responseBody.bytes();%0A            responseAttachmentBuilder.withBody(new String(bytes, StandardCharsets.UTF_8));%0A            responseBuilder.body(ResponseBody.create(responseBody.contentType(), bytes));%0A        %7D%0A%0A        final HttpResponseAttachment responseAttachment = responseAttachmentBuilder.build();%0A        processor.addAttachment(responseAttachment,
  new
@@ -2578,31 +2578,24 @@
 p-re
-quest.ftl%22)%0A        
+sponse.ftl%22)
 );%0A
+%0A
     
@@ -2609,33 +2609,33 @@
 urn 
-chain.proceed(request
+responseBuilder.build(
 );%0A
-%0A
     

add okhttp3 response (via #66)