Fix and update error handling for bethlehem

This commit is contained in:
Sollace 2018-08-10 12:23:35 +02:00
parent 6f1837a46f
commit d19b649d6d

View file

@ -2,11 +2,8 @@ package com.voxelmodpack.hdskins.skins;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload; import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload;
import com.mojang.util.UUIDTypeAdapter; import com.mojang.util.UUIDTypeAdapter;
import com.voxelmodpack.hdskins.HDSkinManager; import com.voxelmodpack.hdskins.HDSkinManager;
@ -14,13 +11,10 @@ import net.minecraft.util.Session;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
@ServerType("bethlehem") @ServerType("bethlehem")
public class BethlehemSkinServer implements SkinServer { public class BethlehemSkinServer implements SkinServer {
@ -37,28 +31,26 @@ public class BethlehemSkinServer implements SkinServer {
public MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException { public MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException {
try (MoreHttpResponses response = new NetClient("GET", getPath(profile)).send()) { try (MoreHttpResponses response = new NetClient("GET", getPath(profile)).send()) {
JsonObject s = response.json(JsonObject.class); if (!response.ok()) {
throw new IOException(response.getResponse().getStatusLine().getReasonPhrase());
if (s.has("success") && s.get("success").getAsBoolean()) {
s = s.get("data").getAsJsonObject();
return gson.fromJson(s, MinecraftTexturesPayload.class);
} }
throw new IOException(s.get("error").getAsString());
return response.json(MinecraftTexturesPayload.class);
} }
} }
@Override @Override
public CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload skin) { public CompletableFuture<SkinUploadResponse> uploadSkin(Session session, SkinUpload upload) {
URI image = skin.getImage();
Map<String, String> metadata = skin.getMetadata();
MinecraftProfileTexture.Type type = skin.getType();
return CallableFutures.asyncFailableFuture(() -> { return CallableFutures.asyncFailableFuture(() -> {
SkinServer.verifyServerConnection(session, SERVER_ID); SkinServer.verifyServerConnection(session, SERVER_ID);
NetClient client = new NetClient("POST", address).putHeaders(createHeaders(session, type, image, metadata)); // TODO: Fix this
NetClient client = new NetClient("POST", address);
if (image != null) { client.putHeaders(createHeaders(session, upload));
client.putFile(type.toString().toLowerCase(Locale.US), "image/png", image);
if (upload.getImage() != null) {
client.putFile(upload.getType().toString().toLowerCase(Locale.US), "image/png", upload.getImage());
} }
try (MoreHttpResponses response = client.send()) { try (MoreHttpResponses response = client.send()) {
@ -71,17 +63,17 @@ public class BethlehemSkinServer implements SkinServer {
}, HDSkinManager.skinUploadExecutor); }, HDSkinManager.skinUploadExecutor);
} }
protected Map<String, ?> createHeaders(Session session, Type type, @Nullable URI image, Map<String, String> metadata) { protected Map<String, ?> createHeaders(Session session, SkinUpload upload) {
Builder<String, Object> builder = ImmutableMap.<String, Object>builder() Builder<String, Object> builder = ImmutableMap.<String, Object>builder()
.put("accessToken", session.getToken()) .put("accessToken", session.getToken())
.put("user", session.getUsername()) .put("user", session.getUsername())
.put("uuid", UUIDTypeAdapter.fromUUID(session.getProfile().getId())) .put("uuid", UUIDTypeAdapter.fromUUID(session.getProfile().getId()))
.put("type", type.toString().toLowerCase(Locale.US)); .put("type", upload.getType().toString().toLowerCase(Locale.US));
if (image == null) { if (upload.getImage() == null) {
builder.put("clear", "1"); builder.put("clear", "1");
} else { } else {
builder.put("model", metadata.getOrDefault("mode", "default")); builder.put("model", upload.getMetadata().getOrDefault("mode", "default"));
} }
return builder.build(); return builder.build();