package com.echsylon.blocks.network;

import com.echsylon.blocks.callback.DefaultRequest;

import java.util.List;

/**
 * This class offers means of convenient request construction and enqueueing.
 */
@SuppressWarnings("WeakerAccess")
public class DefaultRequestFactory {

    /**
     * Enqueues a new byte array request. It's up to the caller to apply
     * appropriate post processing on the result.
     *
     * @param networkClient The transport to send the request through.
     * @param url           The target URL.
     * @param method        The request method.
     * @param headers       Any optional key/value headers.
     * @param payload       Any optional payload to send along with the
     *                      request.
     * @return A request object to attach any callback implementations to.
     */
    public static DefaultRequest<byte[]> enqueueDefaultRequest(final NetworkClient networkClient,
                                                               final String url,
                                                               final String method,
                                                               final List<NetworkClient.Header> headers,
                                                               final byte[] payload) {

        // Don't even try if the required tools aren't provided.
        if (networkClient == null)
            throw new IllegalArgumentException("The NetworkClient mustn't be null");

        return new DefaultRequest<>(() -> {
            String contentType = null;
            int last = headers != null ? headers.size() - 1 : -1;
            for (int i = last; i >= 0 && contentType == null; i--)
                if ("Content-Type".equals(headers.get(i).key))
                    contentType = headers.get(i).value;

            return networkClient.execute(url, method, headers, payload, contentType);
        });
    }

    /**
     * Enqueues a new JSON request, attempting to transform the given payload to
     * a JSON string.
     *
     * @param networkClient      The transport to send the request through.
     * @param url                The target URL.
     * @param method             The request method.
     * @param headers            Any optional key/value headers.
     * @param payload            Any optional payload to send along with the
     *                           request. Will be serialized as JSON.
     * @param jsonParser         The JSON parser to use when transforming to and
     *                           from JSON notation.
     * @param expectedResultType The class description of the expected result.
     * @param <T>                The type declaration of the response class.
     * @return A request object to attach any callback implementations to.
     */
    public static <T> DefaultRequest<T> enqueueJsonRequest(final NetworkClient networkClient,
                                                           final String url,
                                                           final String method,
                                                           final List<NetworkClient.Header> headers,
                                                           final Object payload,
                                                           final JsonParser jsonParser,
                                                           final Class<T> expectedResultType) {

        // Don't even try if the required tools aren't provided.
        if (networkClient == null)
            throw new IllegalArgumentException("The NetworkClient mustn't be null");

        if (jsonParser == null)
            throw new IllegalArgumentException("The JsonParser mustn't be null");

        return new DefaultRequest<>(() -> {
            byte[] payloadBytes = payload != null ?
                    jsonParser.toJson(payload).getBytes() :
                    null;

            byte[] responseBytes = networkClient.execute(url, method, headers, payloadBytes, "application/json");
            String responseJson = new String(responseBytes);
            return jsonParser.fromJson(responseJson);
        });
    }

}

@@ -1702,17 +1702,17 @@
 f (%22
-C
+c
 ontent-
-T
+t
 ype%22
@@ -1737,16 +1737,30 @@
 t(i).key
+.toLowerCase()
 ))%0A     
@@ -2062,21 +2062,16 @@
 kClient 
-     
 The tran
@@ -2129,29 +2129,24 @@
 l           
-     
 The target U
@@ -2173,29 +2173,24 @@
 thod        
-     
 The request 
@@ -2221,29 +2221,24 @@
 aders       
-     
 Any optional
@@ -2285,21 +2285,16 @@
 d       
-     
 Any opti
@@ -2320,32 +2320,41 @@
 d along with the
+ request.
 %0A     *         
@@ -2365,30 +2365,16 @@
         
-      request.
  Will be
@@ -2418,29 +2418,24 @@
 onParser    
-  
-   
 The JSON par
@@ -2469,16 +2469,21 @@
 g to and
+ from
 %0A     * 
@@ -2507,18 +2507,8 @@
     
-  
-   from 
 JSON
@@ -2536,95 +2536,11 @@
 ram 
-expectedResultType The class description of the expected result.%0A     * @param %3CT%3E     
+%3CT%3E
     
@@ -2582,22 +2582,16 @@
 response
- class
 .%0A     *
@@ -3100,16 +3100,16 @@
 ayload,%0A
+
         
@@ -3190,102 +3190,8 @@
 rser
-,%0A                                                           final Class%3CT%3E expectedResultType
 ) %7B%0A

Clean up the default request factory