io.datarouter.httpclient.client.StandardDatarouterLinkClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2009 HotPads ([email protected])
*
* 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 io.datarouter.httpclient.client;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.http.impl.client.CloseableHttpClient;
import io.datarouter.httpclient.endpoint.link.BaseLink;
import io.datarouter.httpclient.endpoint.link.LinkTool;
import io.datarouter.httpclient.endpoint.link.LinkType;
import jakarta.inject.Singleton;
@Singleton
public class StandardDatarouterLinkClient<
L extends LinkType>
implements DatarouterLinkClient{
private final CloseableHttpClient httpClient;
private final Supplier urlPrefix;
StandardDatarouterLinkClient(
CloseableHttpClient httpClient,
Supplier urlPrefix){
this.httpClient = httpClient;
this.urlPrefix = urlPrefix;
}
public StandardDatarouterLinkClient(StandardDatarouterHttpClient client){
this(
client.httpClient,
client.urlPrefix);
}
@Override
public String toUrl(BaseLink link){
link.setUrlPrefix(urlPrefix.get());
String finalUrl = URI.create(link.urlPrefix + link.pathNode.toSlashedString())
.normalize()
.toString();
Map paramMap = LinkTool.getParamFields(link);
String params = paramMap.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&", "?", ""));
return finalUrl + params;
}
@Override
public void shutdown(){
try{
httpClient.close();
}catch(IOException e){
throw new RuntimeException(e);
}
}
@Override
public void initUrlPrefix(BaseLink link){
link.setUrlPrefix(urlPrefix.get());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy