All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moesif.sdk.okhttp3client.models.OkHttp3ResponseMapper Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.moesif.sdk.okhttp3client.models;

import com.moesif.api.models.EventResponseBuilder;
import com.moesif.api.models.EventResponseModel;
import com.moesif.helpers.CollectionUtils;
import okhttp3.Connection;
import okhttp3.Response;

import java.util.Date;


public class OkHttp3ResponseMapper extends EventResponseModel {

    public static EventResponseModel createOkHttp3Response(
            Response response,
            Date responseDate,
            Connection connection
    ) {
        String ipAddress = getIpAddr(connection);
        return createOkHttp3Response(response, responseDate, ipAddress);
    }

    public static EventResponseModel createOkHttp3Response(
                Response response,
                Date responseDate,
                String ipAddress
    ) {
        return new EventResponseBuilder()
                .time(responseDate)
                .status(response.code())
                .headers(CollectionUtils.flattenMultiMap(
                        response.headers().toMultimap())
                )
                .ipAddress(ipAddress)
                .build();
    }


    /**
     * Obtains socket from Okhttp3 Connection object and runs GetHostByAddress
     *
     * @param c Connection
     * @return null or ipAddress
     */
    public static String getIpAddr(Connection c) {
        String ipAddress = null;
        if (c != null) {
            ipAddress = c
                    .route()
                    .socketAddress()
                    .getAddress()
                    .getHostAddress();
        }
        return ipAddress;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy