
io.github.stewseo.client.transport.EndpointBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yelp-fusion-client Show documentation
Show all versions of yelp-fusion-client Show documentation
java client to build api objects, handle http transport, and parse/deserialize/serialize json to/from json
package io.github.stewseo.client.transport;
import io.github.stewseo.client.json.JsonpDeserializer;
import io.github.stewseo.client.transport.endpoints.SimpleEndpoint;
import io.github.stewseo.client._types.ErrorResponse;
import org.apache.http.client.utils.URLEncodedUtils;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
public class EndpointBase implements Endpoint {
private static final Function, Map> EMPTY_MAP = x -> Collections.emptyMap();
/**
* Returns a function that always returns an empty String to String map. Useful to avoid creating lots of
* duplicate lambdas in endpoints that don't have headers or parameters.
*/
@SuppressWarnings("unchecked")
public static Function> emptyMap() {
return (Function>) EMPTY_MAP;
}
protected final String id;
protected final Function method;
protected final Function requestUrl;
protected final Function> queryParameters;
protected final Function> headers;
protected final boolean hasRequestBody;
public EndpointBase(
String id,
Function method,
Function requestUrl,
Function> queryParameters,
Function> headers,
boolean hasRequestBody
) {
this.id = id;
this.method = method;
this.requestUrl = requestUrl;
this.queryParameters = queryParameters;
this.headers = headers;
this.hasRequestBody = hasRequestBody;
}
@Override
public String id() {
return this.id;
}
@Override
public String method(RequestT request) {
return this.method.apply(request);
}
@Override
public String requestUrl(RequestT request) {
return this.requestUrl.apply(request);
}
@Override
public Map queryParameters(RequestT request) {
return this.queryParameters.apply(request);
}
@Override
public Map headers(RequestT request) {
return this.headers.apply(request);
}
@Override
public boolean hasRequestBody() {
return this.hasRequestBody;
}
// ES-specific
@Override
public boolean isError(int statusCode) {
return statusCode >= 400;
}
@Override
public JsonpDeserializer errorDeserializer(int statusCode) {
return ErrorResponse._DESERIALIZER;
}
public SimpleEndpoint withResponseDeserializer(
JsonpDeserializer newResponseParser
) {
return new SimpleEndpoint<>(
id,
method,
requestUrl,
queryParameters,
headers,
hasRequestBody,
newResponseParser
);
}
public static RuntimeException noPathTemplateFound(String what) {
return new RuntimeException("Could not find a request " + what + " with this set of properties. " +
"Please check the API documentation, or raise an issue if this should be a valid request.");
}
public static void pathEncode(String src, StringBuilder dest) {
// TODO: avoid dependency on HttpClient here (and use something more efficient)
dest.append(URLEncodedUtils.formatSegments(src).substring(1));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy