- add comments next to magic numbers

This commit is contained in:
jacek.wolniewicz
2024-07-04 11:51:45 +02:00
parent 36475c2cf6
commit 3e555a502a
8 changed files with 118 additions and 13 deletions

View File

@@ -99,7 +99,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
//TODO 250 Magic number //TODO 250 Magic number
if (clientSettings.getPingInterval() < 250) 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(); var httpSettings = clientSettings.getHttpSettings();
httpSettings.getParams().put("app_language", clientSettings.getClientLanguage()); httpSettings.getParams().put("app_language", clientSettings.getClientLanguage());
@@ -176,12 +176,10 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
dependance.registerSingleton(LiveClient.class, TikTokLiveClient.class); dependance.registerSingleton(LiveClient.class, TikTokLiveClient.class);
onCustomDependencies.forEach(action -> action.accept(dependance)); onCustomDependencies.forEach(action -> action.accept(dependance));
var container = dependance.build(); var container = dependance.build();
var listenerManager = container.find(ListenersManager.class); var listenerManager = container.find(ListenersManager.class);
listeners.forEach(listenerManager::addListener); listeners.forEach(listenerManager::addListener);
return container.find(LiveClient.class); return container.find(LiveClient.class);
} }

View File

@@ -136,7 +136,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
var url = TIKTOK_URL_WEB + "api-live/user/room"; var url = TIKTOK_URL_WEB + "api-live/user/room";
var result = httpFactory.client(url) var result = httpFactory.client(url)
.withParam("uniqueId", request.getUserName()) .withParam("uniqueId", request.getUserName())
.withParam("sourceType", "54") .withParam("sourceType", "54") //MAGIC NUMBER, WHAT 54 means?
.build() .build()
.toJsonResponse(); .toJsonResponse();
@@ -218,7 +218,7 @@ public class TikTokLiveHttpClient implements LiveHttpClient
private ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) { private ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) {
HttpClientBuilder builder = httpFactory.client(TIKTOK_SIGN_API) HttpClientBuilder builder = httpFactory.client(TIKTOK_SIGN_API)
.withParam("client", "ttlive-java") .withParam("client", "ttlive-java")
.withParam("uuc", "1") .withParam("uuc", "1") //MAGIC NUMBER!
.withParam("room_id", room_id); .withParam("room_id", room_id);
if (clientSettings.getApiKey() != null) if (clientSettings.getApiKey() != null)

View File

@@ -129,7 +129,7 @@ public class TikTokListenersManager implements ListenersManager {
var methodContainer = dependanceContainer.createChildContainer() var methodContainer = dependanceContainer.createChildContainer()
.configure(configuration -> .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 //when TikTokEvent type is encountered in the methods parameters
configuration.onInjection(injectionEvent -> configuration.onInjection(injectionEvent ->
{ {

View File

@@ -53,7 +53,7 @@ public class TikTokGenericEventMapper {
private final Map<Class<?>, Method> methodCache; private final Map<Class<?>, Method> methodCache;
private final Map<TypePair, Constructor<?>> constructorCache; private final Map<TypePair, Constructor<?>> constructorCache;
private static final String PARSE_FIELD = "parseFrom";
public TikTokGenericEventMapper() { public TikTokGenericEventMapper() {
this.methodCache = new HashMap<>(); this.methodCache = new HashMap<>();
this.constructorCache = new HashMap<>(); this.constructorCache = new HashMap<>();
@@ -75,7 +75,7 @@ public class TikTokGenericEventMapper {
public Method getParsingMethod(Class<?> input) throws RuntimeException { public Method getParsingMethod(Class<?> input) throws RuntimeException {
return methodCache.computeIfAbsent(input, aClass -> { return methodCache.computeIfAbsent(input, aClass -> {
try { try {
return aClass.getDeclaredMethod("parseFrom", byte[].class); return aClass.getDeclaredMethod(PARSE_FIELD, byte[].class);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -34,11 +34,12 @@ public class TikTokLiveMapper implements LiveMapper {
private final Map<String, TikTokLiveMapperModel> mappers; private final Map<String, TikTokLiveMapperModel> mappers;
private final LiveMapperHelper mapperUtils; private final LiveMapperHelper mapperUtils;
private final TikTokLiveMapperModel globalMapperModel; private final TikTokLiveMapperModel globalMapperModel;
private static final String GLOBAL_MESSAGE = "GLOBAL MESSAGE";
public TikTokLiveMapper(LiveMapperHelper mapperUtils) { public TikTokLiveMapper(LiveMapperHelper mapperUtils) {
this.mappers = new HashMap<>(); this.mappers = new HashMap<>();
this.mapperUtils = mapperUtils; this.mapperUtils = mapperUtils;
this.globalMapperModel = new TikTokLiveMapperModel("any message"); this.globalMapperModel = new TikTokLiveMapperModel(GLOBAL_MESSAGE);
} }
@Override @Override

View File

@@ -38,7 +38,6 @@ import java.util.*;
public class TikTokGiftEventHandler { public class TikTokGiftEventHandler {
private final Map<Long, WebcastGiftMessage> giftsMessages; private final Map<Long, WebcastGiftMessage> giftsMessages;
private final TikTokRoomInfo tikTokRoomInfo; private final TikTokRoomInfo tikTokRoomInfo;
private final GiftsManager giftsManager; private final GiftsManager giftsManager;
public TikTokGiftEventHandler(GiftsManager giftsManager, TikTokRoomInfo tikTokRoomInfo) { public TikTokGiftEventHandler(GiftsManager giftsManager, TikTokRoomInfo tikTokRoomInfo) {

View File

@@ -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();
}
}

View File

@@ -88,8 +88,6 @@ Do you prefer other programming languages?
{{if item is 2}} {{if item is 2}}
my name is {{item.name}}
{{else}} {{else}}
{{end}} {{end}}
@@ -98,7 +96,6 @@ my name is {{item.name}}
<br> <br>
## Listeners ## Listeners
```java ```java
{{listener-content}} {{listener-content}}
``` ```