Fixed Live User Data Mapper throwing MalformedJsonException!

This commit is contained in:
kohlerpop1
2024-02-28 21:03:00 -05:00
parent d3004d76c1
commit 15c642297c
6 changed files with 79 additions and 51 deletions

View File

@@ -28,7 +28,6 @@ import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
import io.github.jwdeveloper.tiktok.live.GiftsManager; import io.github.jwdeveloper.tiktok.live.GiftsManager;
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder; import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class TikTokLive { public class TikTokLive {
@@ -102,14 +101,9 @@ public class TikTokLive {
* @return GiftsManager * @return GiftsManager
*/ */
public static GiftsManager gifts() { public static GiftsManager gifts() {
if (giftsManager != null) { if (giftsManager == null) {
return giftsManager; synchronized (GiftsManager.class) {
} giftsManager = new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
synchronized (GiftsManager.class)
{
if (giftsManager == null)
{
return new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
} }
} }
return giftsManager; return giftsManager;

View File

@@ -184,6 +184,9 @@ public class TikTokLiveClient implements LiveClient {
tikTokEventHandler.publish(this, event); tikTokEventHandler.publish(this, event);
} }
@Override
public void publishMessage(String base64) {}
@Override @Override
public GiftsManager getGiftManager() { public GiftsManager getGiftManager() {
return giftsManager; return giftsManager;

View File

@@ -83,7 +83,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
.toJsonResponse(); .toJsonResponse();
if (result.isFailure()) if (result.isFailure())
throw new TikTokLiveRequestException("Unable to fetch gifts information's"+result.toStack()); throw new TikTokLiveRequestException("Unable to fetch gifts information's - "+result);
var json = result.getContent(); var json = result.getContent();
return giftsDataMapper.map(json); return giftsDataMapper.map(json);
@@ -111,10 +111,10 @@ public class TikTokLiveHttpClient implements LiveHttpClient
.toJsonResponse(); .toJsonResponse();
if (result.isFailure()) if (result.isFailure())
throw new TikTokLiveRequestException("Unable to get information's about user"+result.toStack()); throw new TikTokLiveRequestException("Unable to get information's about user - "+result);
var json = result.getContent(); var json = result.getContent();
return liveUserDataMapper.map(json); return liveUserDataMapper.map(json, logger);
} }
@Override @Override
@@ -138,7 +138,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
.toJsonResponse(); .toJsonResponse();
if (result.isFailure()) if (result.isFailure())
throw new TikTokLiveRequestException("Unable to get info about live room"+result.toStack()); throw new TikTokLiveRequestException("Unable to get info about live room - "+result);
var json = result.getContent(); var json = result.getContent();
return liveDataMapper.map(json); return liveDataMapper.map(json);
@@ -153,7 +153,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
var resultHeader = ActionResult.of(credentialsResponse.headers().firstValue("x-set-tt-cookie")); var resultHeader = ActionResult.of(credentialsResponse.headers().firstValue("x-set-tt-cookie"));
if (resultHeader.isFailure()) { if (resultHeader.isFailure()) {
logger.warning("SignServer Headers: "+request.getRoomId()+" - "+credentialsResponse.headers().map()); logger.warning("SignServer Headers: "+request.getRoomId()+" - "+credentialsResponse.headers().map());
throw new TikTokSignServerException("Sign server did not return the x-set-tt-cookie header"+result.toStack()); throw new TikTokSignServerException("Sign server did not return the x-set-tt-cookie header - "+result);
} }
var websocketCookie = resultHeader.getContent(); var websocketCookie = resultHeader.getContent();
var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body()); var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body());
@@ -169,7 +169,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
return new LiveConnectionData.Response(websocketCookie, webSocketUrl, webcastResponse); return new LiveConnectionData.Response(websocketCookie, webSocketUrl, webcastResponse);
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
throw new TikTokSignServerException("Unable to parse websocket credentials response to WebcastResponse"+result.toStack()); throw new TikTokSignServerException("Unable to parse websocket credentials response to WebcastResponse - "+result);
} }
} }
@@ -197,7 +197,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
var result = builder.build().toResponse(); var result = builder.build().toResponse();
if (result.isFailure()) if (result.isFailure())
throw new TikTokSignServerException("Unable to get websocket connection credentials"+result.toStack()); throw new TikTokSignServerException("Unable to get websocket connection credentials - "+result);
return result; return result;
} }

View File

@@ -1,6 +1,8 @@
package io.github.jwdeveloper.tiktok.common; package io.github.jwdeveloper.tiktok.common;
import com.google.gson.*;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
@@ -11,6 +13,8 @@ public class ActionResult<T> {
private boolean success = true; private boolean success = true;
private T content; private T content;
private String message; private String message;
@Accessors(chain = true, fluent = true)
private ActionResult<?> previous;
protected ActionResult(T object) { protected ActionResult(T object) {
this.content = object; this.content = object;
@@ -41,8 +45,9 @@ public class ActionResult<T> {
public boolean hasMessage() { public boolean hasMessage() {
return message != null; return message != null;
} }
public String toStack() {
return hasMessage() ? " - "+message : ""; public boolean hasPrevious() {
return previous != null;
} }
public boolean hasContent() { public boolean hasContent() {
@@ -84,4 +89,18 @@ public class ActionResult<T> {
public static <T> ActionResult<T> failure() { public static <T> ActionResult<T> failure() {
return failure(null); return failure(null);
} }
public JsonObject toJson() {
JsonObject map = new JsonObject();
map.addProperty("success", success);
map.add("content", new Gson().toJsonTree(content));
map.addProperty("message", message);
map.add("previous", hasPrevious() ? previous.toJson() : null);
return map;
}
@Override
public String toString() {
return "ActionResult: "+new Gson().newBuilder().setPrettyPrinting().create().toJson(toJson());
}
} }

View File

@@ -1,5 +1,8 @@
package io.github.jwdeveloper.tiktok.common; package io.github.jwdeveloper.tiktok.common;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -7,6 +10,8 @@ public class ActionResultBuilder<T>
{ {
private final T content; private final T content;
private String message; private String message;
@Setter @Accessors(fluent = true, chain = true)
private ActionResult<?> previous;
public ActionResultBuilder(T content) { public ActionResultBuilder(T content) {
this.content = content; this.content = content;
@@ -18,10 +23,10 @@ public class ActionResultBuilder<T>
} }
public ActionResult<T> success() { public ActionResult<T> success() {
return ActionResult.success(content, message); return ActionResult.success(content, message).previous(previous);
} }
public ActionResult<T> failure() { public ActionResult<T> failure() {
return ActionResult.success(content, message); return ActionResult.success(content, message).previous(previous);
} }
} }

View File

@@ -22,13 +22,16 @@
*/ */
package io.github.jwdeveloper.tiktok.http.mappers; package io.github.jwdeveloper.tiktok.http.mappers;
import com.google.gson.JsonParser; import com.google.gson.*;
import io.github.jwdeveloper.tiktok.data.requests.LiveUserData; import io.github.jwdeveloper.tiktok.data.requests.LiveUserData;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException; import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
import java.util.logging.Logger;
public class LiveUserDataMapper public class LiveUserDataMapper
{ {
public LiveUserData.Response map(String json) { public LiveUserData.Response map(String json, Logger logger) {
try {
var jsonObject = JsonParser.parseString(json).getAsJsonObject(); var jsonObject = JsonParser.parseString(json).getAsJsonObject();
var message = jsonObject.get("message").getAsString(); var message = jsonObject.get("message").getAsString();
@@ -62,5 +65,9 @@ public class LiveUserDataMapper
}; };
return new LiveUserData.Response(json, statusEnum, roomId, startTime); return new LiveUserData.Response(json, statusEnum, roomId, startTime);
} catch (JsonSyntaxException e) {
logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage());
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
}
} }
} }