com.ibasco.agql.core.AbstractWebApiRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of agql-lib-core Show documentation
Show all versions of agql-lib-core Show documentation
The core library. Required by all agql modules.
/*
* Copyright (c) 2022 Asynchronous Game Query Library
*
* 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.ibasco.agql.core;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.asynchttpclient.Request;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract AbstractWebApiRequest class.
*
* @author Rafael Luis Ibasco
*/
abstract public class AbstractWebApiRequest extends AbstractWebRequest {
/** Constant log
*/
public static final Logger log = LoggerFactory.getLogger(AbstractWebApiRequest.class);
private final Map baseUrlParams;
private final StringSubstitutor substitutor;
private int apiVersion;
private String baseUrlFormat;
/**
* Constructor for AbstractWebApiRequest.
*
* @param apiVersion
* a int
*/
public AbstractWebApiRequest(int apiVersion) {
this.apiVersion = apiVersion;
this.baseUrlParams = new HashMap<>();
this.substitutor = new StringSubstitutor(this.baseUrlParams);
}
/**
* urlParam.
*
* @param criteria
* a {@link com.ibasco.agql.core.AbstractCriteriaBuilder} object
*/
@SuppressWarnings("unchecked")
protected void urlParam(AbstractCriteriaBuilder criteria) {
Set> criteraSet = criteria.getCriteriaSet();
criteraSet.forEach(c -> urlParam(c.getKey(), c.getValue()));
}
/**
* apiVersion.
*
* @return a int
*/
public int apiVersion() {
return apiVersion;
}
/**
* apiVersion.
*
* @param apiVersion
* a int
*/
public void apiVersion(int apiVersion) {
this.apiVersion = apiVersion;
}
/**
* hasProperty.
*
* @param property
* a {@link java.lang.String} object
*
* @return a boolean
*/
protected boolean hasProperty(String property) {
return property(property) != null;
}
/**
* property.
*
* @param property
* a {@link java.lang.String} object
*
* @return a {@link java.lang.String} object
*/
protected String property(String property) {
return baseUrlParams.get(property);
}
/**
* property.
*
* @param property
* a {@link java.lang.String} object
* @param value
* a {@link java.lang.Object} object
*/
protected void property(String property, Object value) {
if (!StringUtils.isEmpty(property) && value != null)
baseUrlParams.put(property, String.valueOf(value));
}
/**
* baseUrlFormat.
*
* @return a {@link java.lang.String} object
*/
public String baseUrlFormat() {
return baseUrlFormat;
}
/**
* baseUrlFormat.
*
* @param baseUrlFormat
* a {@link java.lang.String} object
*/
public void baseUrlFormat(String baseUrlFormat) {
this.baseUrlFormat = baseUrlFormat;
}
/** {@inheritDoc} */
@Override
public Request getMessage() {
baseUrl(resolveBaseUrl());
return super.getMessage();
}
/**
* resolveBaseUrl.
*
* @return a {@link java.lang.String} object
*/
public String resolveBaseUrl() {
return resolveProperties(this.baseUrlFormat);
}
/**
* resolveProperties.
*
* @param textWithVariables
* a {@link java.lang.String} object
*
* @return a {@link java.lang.String} object
*/
protected String resolveProperties(String textWithVariables) {
return substitutor.replace(textWithVariables);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy