
org.johnnei.enjin.spec.impl.EnjinApiImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enjin-api-impl Show documentation
Show all versions of enjin-api-impl Show documentation
The specification of the Enjin API
The newest version!
package org.johnnei.enjin.spec.impl;
import java.util.Arrays;
import java.util.Optional;
import org.johnnei.enjin.EnjinAccessException;
import org.johnnei.enjin.EnjinAuthenticationException;
import org.johnnei.enjin.IEnjinApi;
import org.johnnei.enjin.internal.RequestWrapper;
import org.johnnei.enjin.internal.http.IRequestExecuter;
import org.johnnei.enjin.internal.http.impl.HttpAccessImpl;
import org.johnnei.enjin.internal.http.impl.RequestExecuterImpl;
import org.johnnei.enjin.spec.AuthenticationType;
import org.johnnei.enjin.spec.EnjinMethod;
import org.johnnei.enjin.spec.IUser;
import org.johnnei.enjin.spec.IUserAdmin;
import org.johnnei.enjin.spec.dto.User;
import org.johnnei.enjin.spec.dto.auth.AuthSessionToken;
import org.johnnei.enjin.spec.dto.auth.IAuthToken;
public class EnjinApiImpl implements IEnjinApi {
private final String domain;
private final boolean readOnly;
private final String apiKey;
private IUser userSection;
private IUserAdmin userAdminSection;
public EnjinApiImpl(String domain, boolean readOnly, String apiKey) {
this.domain = domain;
this.readOnly = readOnly;
this.apiKey = apiKey;
IRequestExecuter requestExecuter = new RequestExecuterImpl(new HttpAccessImpl(), domain);
userSection = new UserImpl(this, requestExecuter);
userAdminSection = new UserAdminImpl(this, requestExecuter);
}
public RequestWrapper createRequest(EnjinMethod method, Optional user, T params) {
if (method.isWriteOnly() && readOnly) {
throw new EnjinAccessException(String.format("Method %s requires write access.", method.method()));
}
return new RequestWrapper<>(method.method(), params, getAuthToken(method, user));
}
private IAuthToken getAuthToken(EnjinMethod method, Optional user) {
if (!method.isAuthRequired()) {
return null;
}
if (apiKey != null && canAuthenticateAs(AuthenticationType.API_KEY, method.allowedAuthTypes())) {
throw new EnjinAuthenticationException("API Key authentication is not yet supported.");
}
if (user.isPresent() && canAuthenticateAs(AuthenticationType.SESSION, method.allowedAuthTypes())) {
return new AuthSessionToken.Builder()
.setSessionId(user.get().getSessionId())
.build();
}
throw new EnjinAuthenticationException(String.format(
"Method %s requires authentication. No information for the allowed authentication methods (%s).",
method.method(),
Arrays.toString(method.allowedAuthTypes())));
}
private boolean canAuthenticateAs(AuthenticationType type, AuthenticationType[] allowedTypes) {
for (AuthenticationType allowedType : allowedTypes) {
if (allowedType.equals(type)) {
return true;
}
}
return false;
}
@Override
public IUser getUserSection() {
return userSection;
}
@Override
public IUserAdmin getUserAdminSection() {
return userAdminSection;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("EnjinApiImpl [domain=");
builder.append(domain);
builder.append("]");
return builder.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy