- TikTokHttpResponseEvent
  - Fixed User attributes in CommentEvent
  - Redesign .onMapper method
This commit is contained in:
JW
2023-12-19 04:01:06 +01:00
parent d6c0d50ac3
commit 3eed982d6b
95 changed files with 3200 additions and 1653 deletions

View File

@@ -27,6 +27,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import io.github.jwdeveloper.tiktok.Constants;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
import io.github.jwdeveloper.tiktok.handlers.TikTokEventObserver;
import io.github.jwdeveloper.tiktok.http.TikTokCookieJar;
import io.github.jwdeveloper.tiktok.http.TikTokHttpClient;
import io.github.jwdeveloper.tiktok.http.TikTokHttpRequestFactory;
@@ -78,7 +79,7 @@ public class GiftOfficialJson {
public static JsonArray getJsonGifts() {
var jar = new TikTokCookieJar();
var tiktokHttpClient = new TikTokHttpClient(jar, new TikTokHttpRequestFactory(jar));
var tiktokHttpClient = new TikTokHttpClient(jar, new TikTokHttpRequestFactory(jar, new TikTokEventObserver()));
var settings = Constants.DefaultClientSettings();

View File

@@ -1,36 +0,0 @@
/*
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.jwdeveloper.tiktok.mockClient;
public class TikTokClientMock
{
public static TikTokMockBuilder create(String host)
{
return new TikTokMockBuilder(host);
}
public static TikTokMockBuilder create()
{
return create("MockHostName");
}
}

View File

@@ -1,122 +0,0 @@
/*
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.jwdeveloper.tiktok.mockClient;
import io.github.jwdeveloper.tiktok.TikTokLiveClientBuilder;
import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.gifts.TikTokGiftManager;
import io.github.jwdeveloper.tiktok.handlers.TikTokMessageHandler;
import io.github.jwdeveloper.tiktok.mappers.events.TikTokGiftEventHandler;
import io.github.jwdeveloper.tiktok.mappers.events.TikTokRoomInfoEventHandler;
import io.github.jwdeveloper.tiktok.mappers.events.TikTokSocialMediaEventHandler;
import io.github.jwdeveloper.tiktok.http.TikTokCookieJar;
import io.github.jwdeveloper.tiktok.http.TikTokHttpClient;
import io.github.jwdeveloper.tiktok.http.TikTokHttpRequestFactory;
import io.github.jwdeveloper.tiktok.listener.TikTokListenersManager;
import io.github.jwdeveloper.tiktok.mappers.TikTokGenericEventMapper;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
import io.github.jwdeveloper.tiktok.mockClient.mocks.ApiServiceMock;
import io.github.jwdeveloper.tiktok.mockClient.mocks.LiveClientMock;
import io.github.jwdeveloper.tiktok.mockClient.mocks.WebsocketClientMock;
import java.util.Base64;
import java.util.List;
import java.util.Stack;
public class TikTokMockBuilder extends TikTokLiveClientBuilder {
Stack<WebcastResponse> responses;
public TikTokMockBuilder(String userName) {
super(userName);
responses = new Stack<>();
}
public TikTokMockBuilder addResponse(String value) {
var bytes = Base64.getDecoder().decode(value);
return addResponse(bytes);
}
public TikTokMockBuilder addResponses(List<String> values) {
for (var value : values) {
try {
addResponse(value);
} catch (Exception e) {
throw new TikTokLiveException(value, e);
}
}
return this;
}
public TikTokMockBuilder addResponse(byte[] bytes) {
try {
var response = WebcastResponse.parseFrom(bytes);
return addResponse(response);
} catch (Exception e) {
throw new RuntimeException("Unable to parse response from bytes", e);
}
}
public TikTokMockBuilder addResponse(WebcastResponse message) {
responses.push(message);
return this;
}
@Override
public LiveClientMock build() {
validate();
var cookie = new TikTokCookieJar();
var tiktokRoomInfo = new TikTokRoomInfo();
tiktokRoomInfo.setHostName(clientSettings.getHostName());
var listenerManager = new TikTokListenersManager(listeners, tikTokEventHandler);
var giftManager = new TikTokGiftManager(logger);
var requestFactory = new TikTokHttpRequestFactory(cookie);
var apiClient = new TikTokHttpClient(cookie, requestFactory);
var apiService = new ApiServiceMock(apiClient, logger, clientSettings);
var mapper = createMapper(giftManager, tiktokRoomInfo);
var handler = new TikTokMessageHandler(tikTokEventHandler, mapper);
var webSocketClient = new WebsocketClientMock(logger, responses, handler);
return new LiveClientMock(tiktokRoomInfo,
apiService,
webSocketClient,
giftManager,
tikTokEventHandler,
clientSettings,
listenerManager,
logger);
}
@Override
public LiveClientMock buildAndConnect() {
var client = build();
client.connect();
return client;
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.jwdeveloper.tiktok.mockClient.mocks;
import io.github.jwdeveloper.tiktok.ClientSettings;
import io.github.jwdeveloper.tiktok.http.TikTokApiService;
import io.github.jwdeveloper.tiktok.http.TikTokHttpClient;
import io.github.jwdeveloper.tiktok.live.LiveRoomMeta;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
import java.util.logging.Logger;
public class ApiServiceMock extends TikTokApiService {
public ApiServiceMock(TikTokHttpClient apiClient, Logger logger, ClientSettings clientSettings) {
super(apiClient, logger, clientSettings);
}
@Override
public void updateSessionId() {
}
@Override
public LiveRoomMeta fetchRoomInfo()
{
var meta = new LiveRoomMeta();
meta.setStatus(LiveRoomMeta.LiveRoomStatus.HostOnline);
return meta;
}
@Override
public WebcastResponse fetchClientData() {
return WebcastResponse.newBuilder().build();
}
@Override
public String fetchRoomId(String userName) {
return "mock-room-id";
}
}

View File

@@ -1,79 +0,0 @@
/*
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.jwdeveloper.tiktok.mockClient.mocks;
import io.github.jwdeveloper.tiktok.ClientSettings;
import io.github.jwdeveloper.tiktok.TikTokLiveClient;
import io.github.jwdeveloper.tiktok.TikTokRoomInfo;
import io.github.jwdeveloper.tiktok.gifts.TikTokGiftManager;
import io.github.jwdeveloper.tiktok.handlers.TikTokEventObserver;
import io.github.jwdeveloper.tiktok.http.TikTokApiService;
import io.github.jwdeveloper.tiktok.listener.TikTokListenersManager;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
import java.util.logging.Logger;
public class LiveClientMock extends TikTokLiveClient {
private final WebsocketClientMock websocketClientMock;
public LiveClientMock(TikTokRoomInfo tikTokLiveMeta,
TikTokApiService tikTokApiService,
WebsocketClientMock webSocketClient,
TikTokGiftManager tikTokGiftManager,
TikTokEventObserver tikTokEventHandler,
ClientSettings clientSettings,
TikTokListenersManager listenersManager,
Logger logger) {
super(tikTokLiveMeta,
tikTokApiService,
webSocketClient,
tikTokGiftManager,
tikTokEventHandler,
clientSettings,
listenersManager,
logger);
this.websocketClientMock = webSocketClient;
}
public void publishMessage(String type, String base64) {
websocketClientMock.addMessage(type, base64);
}
public void publishMessage(Class<?> clazz, String base64) {
websocketClientMock.addMessage(clazz.getSimpleName(), base64);
}
public void publishResponse(String value) {
websocketClientMock.addResponse(value);
}
public void publishResponse(byte[] bytes) {
websocketClientMock.addResponse(bytes);
}
public void publishResponse(WebcastResponse message) {
websocketClientMock.addResponse(message);
}
}

View File

@@ -1,124 +0,0 @@
/*
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.jwdeveloper.tiktok.mockClient.mocks;
import com.google.protobuf.ByteString;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageException;
import io.github.jwdeveloper.tiktok.handlers.TikTokMessageHandler;
import io.github.jwdeveloper.tiktok.live.LiveClient;
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
import io.github.jwdeveloper.tiktok.websocket.SocketClient;
import lombok.Value;
import java.util.Base64;
import java.util.Stack;
import java.util.logging.Logger;
public class WebsocketClientMock implements SocketClient {
Logger logger;
Stack<WebcastResponse> responses;
Stack<MsgStruct> messages;
TikTokMessageHandler messageHandler;
private boolean isRunning;
@Value
public static class MsgStruct {
String messageType;
byte[] messageValue;
}
public WebsocketClientMock(Logger logger, Stack<WebcastResponse> responses, TikTokMessageHandler messageHandler) {
this.logger = logger;
this.responses = responses;
this.messageHandler = messageHandler;
messages = new Stack<>();
}
public WebsocketClientMock addMessage(String type, String value) {
var bytes = Base64.getDecoder().decode(value);
messages.push(new MsgStruct(type, bytes));
return this;
}
public WebsocketClientMock addResponse(String value) {
var bytes = Base64.getDecoder().decode(value);
return addResponse(bytes);
}
public WebsocketClientMock addResponse(byte[] bytes) {
try {
var response = WebcastResponse.parseFrom(bytes);
return addResponse(response);
} catch (Exception e) {
throw new RuntimeException("Unable to parse response from bytes", e);
}
}
public WebsocketClientMock addResponse(WebcastResponse message) {
responses.push(message);
return this;
}
@Override
public void start(WebcastResponse webcastResponse, LiveClient tikTokLiveClient) {
logger.info("Running message: " + responses.size());
isRunning = true;
while (!responses.isEmpty() || !messages.isEmpty()) {
if (!responses.isEmpty()) {
var response = responses.pop();
for (var message : response.getMessagesList()) {
try {
messageHandler.handleSingleMessage(tikTokLiveClient, message);
} catch (Exception e) {
logger.info("Unable to parse message for response " + response.getCursor());
throw new TikTokLiveMessageException(message, response, e);
}
}
}
if (!messages.isEmpty()) {
var messageStr = messages.pop();
try {
var msg = WebcastResponse.Message.newBuilder()
.setMethod(messageStr.messageType)
.setPayload(ByteString.copyFrom(messageStr.getMessageValue()))
.build();
messageHandler.handleSingleMessage(tikTokLiveClient, msg);
} catch (Exception e) {
logger.info("Unable to parse message for response " + messageStr.getMessageType());
throw new TikTokLiveException(e);
}
}
}
}
@Override
public void stop() {
isRunning = true;
}
}