mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
- add comments next to magic numbers
This commit is contained in:
@@ -99,7 +99,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
|
||||
//TODO 250 Magic number
|
||||
if (clientSettings.getPingInterval() < 250)
|
||||
throw new TikTokLiveException("Minimum allowed ping interval is 250 millseconds");
|
||||
throw new TikTokLiveException("Minimum allowed ping interval is 250 milliseconds");
|
||||
|
||||
var httpSettings = clientSettings.getHttpSettings();
|
||||
httpSettings.getParams().put("app_language", clientSettings.getClientLanguage());
|
||||
@@ -176,12 +176,10 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
dependance.registerSingleton(LiveClient.class, TikTokLiveClient.class);
|
||||
|
||||
onCustomDependencies.forEach(action -> action.accept(dependance));
|
||||
|
||||
var container = dependance.build();
|
||||
|
||||
var listenerManager = container.find(ListenersManager.class);
|
||||
listeners.forEach(listenerManager::addListener);
|
||||
|
||||
return container.find(LiveClient.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -136,7 +136,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
||||
var url = TIKTOK_URL_WEB + "api-live/user/room";
|
||||
var result = httpFactory.client(url)
|
||||
.withParam("uniqueId", request.getUserName())
|
||||
.withParam("sourceType", "54")
|
||||
.withParam("sourceType", "54") //MAGIC NUMBER, WHAT 54 means?
|
||||
.build()
|
||||
.toJsonResponse();
|
||||
|
||||
@@ -218,7 +218,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
||||
private ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) {
|
||||
HttpClientBuilder builder = httpFactory.client(TIKTOK_SIGN_API)
|
||||
.withParam("client", "ttlive-java")
|
||||
.withParam("uuc", "1")
|
||||
.withParam("uuc", "1") //MAGIC NUMBER!
|
||||
.withParam("room_id", room_id);
|
||||
|
||||
if (clientSettings.getApiKey() != null)
|
||||
|
||||
@@ -129,7 +129,7 @@ public class TikTokListenersManager implements ListenersManager {
|
||||
var methodContainer = dependanceContainer.createChildContainer()
|
||||
.configure(configuration ->
|
||||
{
|
||||
//Modfying container, so it returns TikTokEvent object instance,
|
||||
//Modifying container, so it returns TikTokEvent object instance,
|
||||
//when TikTokEvent type is encountered in the methods parameters
|
||||
configuration.onInjection(injectionEvent ->
|
||||
{
|
||||
|
||||
@@ -53,7 +53,7 @@ public class TikTokGenericEventMapper {
|
||||
|
||||
private final Map<Class<?>, Method> methodCache;
|
||||
private final Map<TypePair, Constructor<?>> constructorCache;
|
||||
|
||||
private static final String PARSE_FIELD = "parseFrom";
|
||||
public TikTokGenericEventMapper() {
|
||||
this.methodCache = new HashMap<>();
|
||||
this.constructorCache = new HashMap<>();
|
||||
@@ -75,7 +75,7 @@ public class TikTokGenericEventMapper {
|
||||
public Method getParsingMethod(Class<?> input) throws RuntimeException {
|
||||
return methodCache.computeIfAbsent(input, aClass -> {
|
||||
try {
|
||||
return aClass.getDeclaredMethod("parseFrom", byte[].class);
|
||||
return aClass.getDeclaredMethod(PARSE_FIELD, byte[].class);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -34,11 +34,12 @@ public class TikTokLiveMapper implements LiveMapper {
|
||||
private final Map<String, TikTokLiveMapperModel> mappers;
|
||||
private final LiveMapperHelper mapperUtils;
|
||||
private final TikTokLiveMapperModel globalMapperModel;
|
||||
private static final String GLOBAL_MESSAGE = "GLOBAL MESSAGE";
|
||||
|
||||
public TikTokLiveMapper(LiveMapperHelper mapperUtils) {
|
||||
this.mappers = new HashMap<>();
|
||||
this.mapperUtils = mapperUtils;
|
||||
this.globalMapperModel = new TikTokLiveMapperModel("any message");
|
||||
this.globalMapperModel = new TikTokLiveMapperModel(GLOBAL_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -38,7 +38,6 @@ import java.util.*;
|
||||
public class TikTokGiftEventHandler {
|
||||
private final Map<Long, WebcastGiftMessage> giftsMessages;
|
||||
private final TikTokRoomInfo tikTokRoomInfo;
|
||||
|
||||
private final GiftsManager giftsManager;
|
||||
|
||||
public TikTokGiftEventHandler(GiftsManager giftsManager, TikTokRoomInfo tikTokRoomInfo) {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.requests.LiveData;
|
||||
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
|
||||
import io.github.jwdeveloper.tiktok.listener.ListenersManager;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftsManager;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveEventsHandler;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveMessagesHandler;
|
||||
import io.github.jwdeveloper.tiktok.models.ConnectionState;
|
||||
import io.github.jwdeveloper.tiktok.websocket.LiveSocketClient;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class TikTokLiveClientTests {
|
||||
|
||||
private LiveClient sut;
|
||||
LiveMessagesHandler messageHandler;
|
||||
GiftsManager giftsManager;
|
||||
TikTokRoomInfo tikTokLiveMeta;
|
||||
LiveHttpClient tiktokHttpClient;
|
||||
LiveSocketClient webSocketClient;
|
||||
LiveEventsHandler tikTokEventHandler;
|
||||
LiveClientSettings clientSettings;
|
||||
ListenersManager listenersManager;
|
||||
Logger logger;
|
||||
|
||||
@Before
|
||||
public void onBefore() {
|
||||
messageHandler = Mockito.mock(LiveMessagesHandler.class);
|
||||
giftsManager = Mockito.mock(GiftsManager.class);
|
||||
tikTokLiveMeta = Mockito.mock(TikTokRoomInfo.class);
|
||||
tiktokHttpClient = Mockito.mock(LiveHttpClient.class);
|
||||
webSocketClient = Mockito.mock(LiveSocketClient.class);
|
||||
tikTokEventHandler = Mockito.mock(LiveEventsHandler.class);
|
||||
clientSettings = Mockito.mock(LiveClientSettings.class);
|
||||
listenersManager = Mockito.mock(ListenersManager.class);
|
||||
logger = Mockito.mock(Logger.class);
|
||||
|
||||
sut = new TikTokLiveClient(messageHandler,
|
||||
giftsManager,
|
||||
tikTokLiveMeta,
|
||||
tiktokHttpClient,
|
||||
webSocketClient,
|
||||
tikTokEventHandler,
|
||||
clientSettings,
|
||||
listenersManager,
|
||||
logger);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldThrownWhenAlreadyConnected() {
|
||||
tikTokLiveMeta.setConnectionState(ConnectionState.CONNECTED);
|
||||
Assert.assertThrows(TikTokLiveException.class, () ->
|
||||
{
|
||||
sut.connect();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowWhenUserIsOffline() {
|
||||
|
||||
var request = new LiveData.Request("X");
|
||||
var response = new LiveData.Response();
|
||||
response.setLiveStatus(LiveData.LiveStatus.HostOffline);
|
||||
Mockito.when(tiktokHttpClient.fetchLiveData(request)).thenReturn(response);
|
||||
Assert.assertThrows(TikTokLiveException.class, () ->
|
||||
{
|
||||
sut.connect();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowWhenUserNotFound()
|
||||
{
|
||||
var request = new LiveData.Request("X");
|
||||
var response = new LiveData.Response();
|
||||
response.setLiveStatus(LiveData.LiveStatus.HostNotFound);
|
||||
Mockito.when(tiktokHttpClient.fetchLiveData(request)).thenReturn(response);
|
||||
Assert.assertThrows(TikTokLiveException.class, () ->
|
||||
{
|
||||
sut.connect();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowWhenAgeRestricted()
|
||||
{
|
||||
Mockito.when(tiktokHttpClient.fetchLiveData(new LiveData.Request("X")))
|
||||
.thenReturn(new LiveData.Response());
|
||||
Assert.assertThrows(TikTokLiveException.class, () ->
|
||||
{
|
||||
sut.connect();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldConnect() {
|
||||
// sut.connect();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user