/**
 * Copyright (c) Anton Johansson <antoon.johansson@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.antonjohansson.geolocation.provider.ipapi;

import static java.util.stream.Collectors.toList;

import java.util.Collection;
import java.util.List;

import com.antonjohansson.geolocation.config.Configuration;
import com.antonjohansson.geolocation.framework.Provider;
import com.antonjohansson.geolocation.framework.domain.LookupResult;
import com.antonjohansson.geolocation.http.WebClientFactory;
import com.antonjohansson.geolocation.provider.ipapi.model.LookupRequest;
import com.antonjohansson.geolocation.provider.ipapi.model.LookupResponse;

/**
 * {@link Provider} implementation that utilizes the <a>http://ip-api.com</a> API.
 */
public class IpApiProvider implements Provider
{
    private static final int MAX_BATCH_SIZE = 100;
    private String endpoint = "http://ip-api.com";
    private String token = "";

    public void setEndpoint(String endpoint)
    {
        this.endpoint = endpoint;
    }

    public void setToken(String token)
    {
        this.token = token;
    }

    @Override
    public void validate(Configuration configuration)
    {
        if (configuration.getBatchSize() > MAX_BATCH_SIZE)
        {
            throw new RuntimeException("IP API does not allow more than 100 per batch");
        }
    }

    @Override
    public List<LookupResult> lookup(Collection<String> addresses)
    {
        List<LookupRequest> requests = addresses.stream()
                .map(LookupRequest::new)
                .collect(toList());

        Collection<? extends LookupResponse> responses = WebClientFactory.endpoint(endpoint)
                .json()
                .build()
                .path("/batch")
                .postAndGetCollection(requests, LookupResponse.class);

        return responses.stream().map(this::toResult).collect(toList());
    }

    private LookupResult toResult(LookupResponse response)
    {
        LookupResult result = new LookupResult();
        result.setIp(response.getAddress());
        result.setLongitude(response.getLongitude());
        result.setLatitude(response.getLatitude());
        return result;
    }
}

@@ -740,24 +740,85 @@
 ors.toList;%0D
+%0Aimport static org.apache.commons.lang3.StringUtils.isBlank;%0D
 %0A%0D%0Aimport ja
@@ -860,24 +860,73 @@
 il.List;%0D%0A%0D%0A
+import org.apache.cxf.jaxrs.client.WebClient;%0D%0A%0D%0A
 import com.a
@@ -1586,21 +1586,19 @@
  String 
-to
 ke
-n
+y
  = %22%22;%0D%0A
@@ -1719,21 +1719,19 @@
  set
-Token
+Key
 (String 
 toke
@@ -1726,21 +1726,19 @@
 (String 
-to
 ke
-n
+y
 )%0D%0A    %7B
@@ -1756,21 +1756,17 @@
 his.
-to
 ke
-n
+y
  = 
-to
 ke
-n
+y
 ;%0D%0A 
@@ -2273,54 +2273,24 @@
     
-Collection%3C? extends LookupResponse%3E responses
+WebClient client
  = W
@@ -2407,16 +2407,19 @@
 /batch%22)
+;%0D%0A
 %0D%0A      
@@ -2420,24 +2420,162 @@
         
-        
+if (!isBlank(key))%0D%0A        %7B%0D%0A            client.query(%22key%22, key);%0D%0A        %7D%0D%0A%0D%0A        Collection%3C? extends LookupResponse%3E responses = client
 .postAnd
@@ -2618,26 +2618,24 @@
 se.class);%0D%0A
-%0D%0A
         retu

Add commercial support for IP API