mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-03-01 17:59:39 -05:00
Test application
This commit is contained in:
@@ -2,12 +2,15 @@ package io.github.jwdeveloper.tiktok.handlers;
|
||||
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.*;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.events.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.messages.*;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokErrorEvent;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokUnhandledEvent;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageParsingException;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
@@ -18,36 +21,33 @@ public abstract class WebResponseHandlerBase {
|
||||
private final Map<String, TikTokMessageHandler> handlers;
|
||||
private final TikTokEventHandler tikTokEventHandler;
|
||||
|
||||
public WebResponseHandlerBase(TikTokEventHandler tikTokEventHandler)
|
||||
{
|
||||
public WebResponseHandlerBase(TikTokEventHandler tikTokEventHandler) {
|
||||
handlers = new HashMap<>();
|
||||
this.tikTokEventHandler =tikTokEventHandler;
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
init();
|
||||
}
|
||||
|
||||
public abstract void init();
|
||||
|
||||
public void register(Class<?> input, Class<?> output)
|
||||
{
|
||||
register(input,(e)->
|
||||
public void register(Class<?> input, Class<?> output) {
|
||||
register(input, (e) ->
|
||||
{
|
||||
try
|
||||
{
|
||||
try {
|
||||
var parseMethod = input.getDeclaredMethod("parseFrom", ByteString.class);
|
||||
var deserialized = parseMethod.invoke(null,e.getBinary());
|
||||
var deserialized = parseMethod.invoke(null, e.getBinary());
|
||||
|
||||
var constructors = Arrays.stream(output.getConstructors()).filter(ea -> Arrays.stream(ea.getParameterTypes()).toList().contains(input)).findFirst();
|
||||
|
||||
var tiktokEvent = constructors.get().newInstance(deserialized);
|
||||
return (TikTokEvent)tiktokEvent;
|
||||
}
|
||||
catch (Exception ex)
|
||||
} catch (Exception ex)
|
||||
{
|
||||
throw new TikTokLiveException("Unable to handle parsing from class: "+input.getSimpleName()+" to class "+output.getSimpleName(),ex);
|
||||
throw new TikTokLiveMessageParsingException("Unable to handle parsing from class: " + input.getSimpleName() + " to class " + output.getSimpleName(), ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
public <T> void register(Class clazz, Function<WebcastResponse.Message,TikTokEvent> func)
|
||||
{
|
||||
|
||||
public <T> void register(Class clazz, Function<WebcastResponse.Message, TikTokEvent> func) {
|
||||
var haandler = new TikTokMessageHandler<T>() {
|
||||
@Override
|
||||
public Class<T> getHandleClazz() {
|
||||
@@ -64,21 +64,21 @@ public abstract class WebResponseHandlerBase {
|
||||
}
|
||||
|
||||
public void handle(WebcastResponse webcastResponse) {
|
||||
// System.out.println("==============================================================");
|
||||
// System.out.println("Getting messages: " + webcastResponse.getMessagesList().size());
|
||||
for (var message : webcastResponse.getMessagesList()) {
|
||||
try {
|
||||
handleSingleMessage(message);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Error whilst Handling Message. Stopping Client.{Environment.NewLine}Final Message: {Convert.ToBase64String(message.Binary)}", e);
|
||||
} catch (Exception e)
|
||||
{
|
||||
var decoded = Base64.getEncoder().encodeToString(message.getBinary().toByteArray());
|
||||
|
||||
var exception = new TikTokLiveException("Error whilst Handling Message. Stopping Client. Final Message: \n"+decoded, e);
|
||||
tikTokEventHandler.publish(new TikTokErrorEvent(exception));
|
||||
}
|
||||
}
|
||||
// System.out.println("==============================================================");
|
||||
}
|
||||
|
||||
private void handleSingleMessage(WebcastResponse.Message message) throws Exception {
|
||||
if(!handlers.containsKey(message.getType()))
|
||||
{
|
||||
if (!handlers.containsKey(message.getType())) {
|
||||
tikTokEventHandler.publish(new TikTokUnhandledEvent(message));
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user