mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Pushing broken changes for JW!
This commit is contained in:
@@ -31,8 +31,8 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
public class TikTokLive {
|
public class TikTokLive {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
|
||||||
* @param hostName profile name of Tiktok user could be found in profile link
|
* @param hostName profile name of Tiktok user could be found in profile link
|
||||||
* example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo
|
|
||||||
* @return LiveClientBuilder
|
* @return LiveClientBuilder
|
||||||
*/
|
*/
|
||||||
public static LiveClientBuilder newClient(String hostName) {
|
public static LiveClientBuilder newClient(String hostName) {
|
||||||
@@ -40,9 +40,8 @@ public class TikTokLive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
|
||||||
* @param hostName profile name of Tiktok user could be found in profile link
|
* @param hostName profile name of Tiktok user could be found in profile link
|
||||||
* example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo
|
|
||||||
* @return true if live is Online, false if is offline
|
* @return true if live is Online, false if is offline
|
||||||
*/
|
*/
|
||||||
public static boolean isLiveOnline(String hostName)
|
public static boolean isLiveOnline(String hostName)
|
||||||
@@ -51,9 +50,8 @@ public class TikTokLive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
|
||||||
* @param hostName profile name of Tiktok user could be found in profile link
|
* @param hostName profile name of Tiktok user could be found in profile link
|
||||||
* example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo
|
|
||||||
* @return true if live is Online, false if is offline
|
* @return true if live is Online, false if is offline
|
||||||
*/
|
*/
|
||||||
public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName)
|
public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName)
|
||||||
@@ -62,9 +60,8 @@ public class TikTokLive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
|
||||||
* @param hostName profile name of Tiktok user could be found in profile link
|
* @param hostName profile name of Tiktok user could be found in profile link
|
||||||
* example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo
|
|
||||||
* @return true is hostName name is valid and exists, false if not
|
* @return true is hostName name is valid and exists, false if not
|
||||||
*/
|
*/
|
||||||
public static boolean isHostNameValid(String hostName)
|
public static boolean isHostNameValid(String hostName)
|
||||||
@@ -73,9 +70,8 @@ public class TikTokLive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
|
||||||
* @param hostName profile name of Tiktok user could be found in profile link
|
* @param hostName profile name of Tiktok user could be found in profile link
|
||||||
* example: https://www.tiktok.com/@dostawcavideo hostName would be dostawcavideo
|
|
||||||
* @return true is hostName name is valid and exists, false if not
|
* @return true is hostName name is valid and exists, false if not
|
||||||
*/
|
*/
|
||||||
public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName)
|
public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName)
|
||||||
|
|||||||
@@ -22,69 +22,52 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok;
|
package io.github.jwdeveloper.tiktok;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.common.LoggerFactory;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.*;
|
import io.github.jwdeveloper.tiktok.data.events.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.control.TikTokPreConnectionEvent;
|
import io.github.jwdeveloper.tiktok.data.events.control.TikTokPreConnectionEvent;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.envelop.TikTokChestEvent;
|
import io.github.jwdeveloper.tiktok.data.events.envelop.TikTokChestEvent;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftComboEvent;
|
import io.github.jwdeveloper.tiktok.data.events.gift.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.http.TikTokHttpResponseEvent;
|
import io.github.jwdeveloper.tiktok.data.events.http.TikTokHttpResponseEvent;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollEvent;
|
import io.github.jwdeveloper.tiktok.data.events.poll.TikTokPollEvent;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomInfoEvent;
|
import io.github.jwdeveloper.tiktok.data.events.room.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomPinEvent;
|
import io.github.jwdeveloper.tiktok.data.events.social.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokFollowEvent;
|
import io.github.jwdeveloper.tiktok.data.events.websocket.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokJoinEvent;
|
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokLikeEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.social.TikTokShareEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.websocket.TikTokWebsocketMessageEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.websocket.TikTokWebsocketResponseEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.data.events.websocket.TikTokWebsocketUnhandledMessageEvent;
|
|
||||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||||
import io.github.jwdeveloper.tiktok.gifts.TikTokGiftManager;
|
import io.github.jwdeveloper.tiktok.gifts.TikTokGiftManager;
|
||||||
import io.github.jwdeveloper.tiktok.http.HttpClientFactory;
|
import io.github.jwdeveloper.tiktok.http.HttpClientFactory;
|
||||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
import io.github.jwdeveloper.tiktok.listener.*;
|
||||||
import io.github.jwdeveloper.tiktok.listener.TikTokListenersManager;
|
import io.github.jwdeveloper.tiktok.live.*;
|
||||||
import io.github.jwdeveloper.tiktok.live.GiftManager;
|
import io.github.jwdeveloper.tiktok.live.builder.*;
|
||||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
import io.github.jwdeveloper.tiktok.mappers.*;
|
||||||
import io.github.jwdeveloper.tiktok.live.builder.EventConsumer;
|
|
||||||
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.TikTokGenericEventMapper;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.TikTokLiveMapper;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.TikTokLiveMapperHelper;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.TikTokMapper;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.data.MappingResult;
|
import io.github.jwdeveloper.tiktok.mappers.data.MappingResult;
|
||||||
import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokCommonEventHandler;
|
import io.github.jwdeveloper.tiktok.mappers.handlers.*;
|
||||||
import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokGiftEventHandler;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokRoomInfoEventHandler;
|
|
||||||
import io.github.jwdeveloper.tiktok.mappers.handlers.TikTokSocialMediaEventHandler;
|
|
||||||
import io.github.jwdeveloper.tiktok.messages.webcast.*;
|
import io.github.jwdeveloper.tiktok.messages.webcast.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
|
||||||
import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
|
import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
|
||||||
import io.github.jwdeveloper.tiktok.websocket.TikTokWebSocketClient;
|
import io.github.jwdeveloper.tiktok.websocket.TikTokWebSocketClient;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||||
|
|
||||||
protected final LiveClientSettings clientSettings;
|
protected final LiveClientSettings clientSettings;
|
||||||
protected final Logger logger;
|
|
||||||
protected final TikTokLiveEventHandler tikTokEventHandler;
|
protected final TikTokLiveEventHandler tikTokEventHandler;
|
||||||
protected final List<TikTokEventListener> listeners;
|
protected final List<TikTokEventListener> listeners;
|
||||||
protected Consumer<TikTokMapper> onCustomMappings;
|
protected Consumer<TikTokMapper> onCustomMappings;
|
||||||
|
protected Logger logger;
|
||||||
|
|
||||||
public TikTokLiveClientBuilder(String userName)
|
public TikTokLiveClientBuilder(String userName)
|
||||||
{
|
{
|
||||||
this.clientSettings = LiveClientSettings.createDefault();
|
this.clientSettings = LiveClientSettings.createDefault();
|
||||||
this.clientSettings.setHostName(userName);
|
this.clientSettings.setHostName(userName);
|
||||||
this.tikTokEventHandler = new TikTokLiveEventHandler();
|
this.tikTokEventHandler = new TikTokLiveEventHandler();
|
||||||
this.logger = Logger.getLogger(TikTokLive.class.getSimpleName() + " " + userName);
|
|
||||||
this.listeners = new ArrayList<>();
|
this.listeners = new ArrayList<>();
|
||||||
this.onCustomMappings = (e) -> {
|
this.onCustomMappings = (e) -> {};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveClientBuilder onMapping(Consumer<TikTokMapper> onCustomMappings) {
|
public LiveClientBuilder onMapping(Consumer<TikTokMapper> onCustomMappings) {
|
||||||
@@ -124,6 +107,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
|||||||
httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage());
|
httpSettings.getParams().put("webcast_language", clientSettings.getClientLanguage());
|
||||||
|
|
||||||
|
|
||||||
|
this.logger = LoggerFactory.create(clientSettings.getHostName(), clientSettings);
|
||||||
var handler = new ConsoleHandler();
|
var handler = new ConsoleHandler();
|
||||||
handler.setFormatter(new Formatter() {
|
handler.setFormatter(new Formatter() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -23,8 +23,9 @@
|
|||||||
package io.github.jwdeveloper.tiktok;
|
package io.github.jwdeveloper.tiktok;
|
||||||
|
|
||||||
import com.google.protobuf.InvalidProtocolBufferException;
|
import com.google.protobuf.InvalidProtocolBufferException;
|
||||||
|
import io.github.jwdeveloper.tiktok.common.LoggerFactory;
|
||||||
import io.github.jwdeveloper.tiktok.data.requests.*;
|
import io.github.jwdeveloper.tiktok.data.requests.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.settings.*;
|
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||||
import io.github.jwdeveloper.tiktok.exceptions.*;
|
import io.github.jwdeveloper.tiktok.exceptions.*;
|
||||||
import io.github.jwdeveloper.tiktok.http.*;
|
import io.github.jwdeveloper.tiktok.http.*;
|
||||||
import io.github.jwdeveloper.tiktok.http.mappers.*;
|
import io.github.jwdeveloper.tiktok.http.mappers.*;
|
||||||
@@ -32,9 +33,10 @@ import io.github.jwdeveloper.tiktok.messages.webcast.WebcastResponse;
|
|||||||
|
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class TikTokLiveHttpClient implements LiveHttpClient {
|
public class TikTokLiveHttpClient implements LiveHttpClient
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* <a href="https://github-wiki-see.page/m/isaackogan/TikTokLive/wiki/All-About-Signatures">Signing API by Isaac Kogan</a>
|
* <a href="https://github-wiki-see.page/m/isaackogan/TikTokLive/wiki/All-About-Signatures">Signing API by Isaac Kogan</a>
|
||||||
*/
|
*/
|
||||||
@@ -48,10 +50,12 @@ public class TikTokLiveHttpClient implements LiveHttpClient {
|
|||||||
private final LiveUserDataMapper liveUserDataMapper;
|
private final LiveUserDataMapper liveUserDataMapper;
|
||||||
private final LiveDataMapper liveDataMapper;
|
private final LiveDataMapper liveDataMapper;
|
||||||
private final GiftsDataMapper giftsDataMapper;
|
private final GiftsDataMapper giftsDataMapper;
|
||||||
|
private final Logger logger;
|
||||||
|
|
||||||
public TikTokLiveHttpClient(HttpClientFactory factory, LiveClientSettings settings) {
|
public TikTokLiveHttpClient(HttpClientFactory factory, LiveClientSettings settings) {
|
||||||
this.httpFactory = factory;
|
this.httpFactory = factory;
|
||||||
clientSettings = settings;
|
this.clientSettings = settings;
|
||||||
|
this.logger = LoggerFactory.create("HttpClient", clientSettings);
|
||||||
liveUserDataMapper = new LiveUserDataMapper();
|
liveUserDataMapper = new LiveUserDataMapper();
|
||||||
liveDataMapper = new LiveDataMapper();
|
liveDataMapper = new LiveDataMapper();
|
||||||
giftsDataMapper = new GiftsDataMapper();
|
giftsDataMapper = new GiftsDataMapper();
|
||||||
@@ -169,7 +173,8 @@ public class TikTokLiveHttpClient implements LiveHttpClient {
|
|||||||
try {
|
try {
|
||||||
var optionalHeader = credentialsResponse.headers().firstValue("x-set-tt-cookie");
|
var optionalHeader = credentialsResponse.headers().firstValue("x-set-tt-cookie");
|
||||||
if (optionalHeader.isEmpty()) {
|
if (optionalHeader.isEmpty()) {
|
||||||
throw new TikTokSignServerException("Sign server did not return the set-cookie header");
|
logger.warning("SignServer Headers: "+credentialsResponse.headers().map());
|
||||||
|
throw new TikTokSignServerException("Sign server did not return the x-set-tt-cookie header");
|
||||||
}
|
}
|
||||||
var websocketCookie = optionalHeader.get();
|
var websocketCookie = optionalHeader.get();
|
||||||
var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body());
|
var webcastResponse = WebcastResponse.parseFrom(credentialsResponse.body());
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.common;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ActionResult<T>
|
||||||
|
{
|
||||||
|
private boolean success = true;
|
||||||
|
private T content;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
protected ActionResult(T object) {
|
||||||
|
this.content = object;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ActionResult(T object, boolean success) {
|
||||||
|
this(object);
|
||||||
|
this.success = success;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ActionResult(T object, boolean success, String message) {
|
||||||
|
this(object, success);
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResultBuilder<T> of(T content) {
|
||||||
|
return new ActionResultBuilder<>(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFailure() {
|
||||||
|
return !isSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasMessage() {
|
||||||
|
return message != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasContent() {
|
||||||
|
return content != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResult<T> success() {
|
||||||
|
return new ActionResult<>(null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <Output> ActionResult<Output> cast(Output output) {
|
||||||
|
return new ActionResult<>(output, this.isSuccess(), this.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <Output> ActionResult<Output> cast() {
|
||||||
|
return new ActionResult<>(null, this.isSuccess(), this.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <U> ActionResult<U> map(Function<? super T, ? extends U> mapper) {
|
||||||
|
return hasContent() ? cast(mapper.apply(content)) : cast();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <Input, Output> ActionResult<Output> cast(ActionResult<Input> action, Output output) {
|
||||||
|
return new ActionResult<>(output, action.isSuccess(), action.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResult<T> success(T payload) {
|
||||||
|
return new ActionResult<>(payload, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResult<T> success(T payload, String message) {
|
||||||
|
return new ActionResult<>(payload, true, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResult<T> failure() {
|
||||||
|
return new ActionResult<>(null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResult<T> failure(String message) {
|
||||||
|
return new ActionResult<>(null, false, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ActionResult<T> failure(T target, String message) {
|
||||||
|
return new ActionResult<>(target, false, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.common;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ActionResultBuilder<T>
|
||||||
|
{
|
||||||
|
private final T content;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public ActionResultBuilder(T content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionResultBuilder<T> message(Object... messages) {
|
||||||
|
this.message = Arrays.stream(messages).map(Object::toString).collect(Collectors.joining(" "));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionResult<T> success() {
|
||||||
|
return ActionResult.success(content, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionResult<T> failure() {
|
||||||
|
return ActionResult.success(content, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.common;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||||
|
import io.github.jwdeveloper.tiktok.utils.ConsoleColors;
|
||||||
|
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
|
public class LoggerFactory
|
||||||
|
{
|
||||||
|
public static Logger create(String name, LiveClientSettings settings) {
|
||||||
|
Logger logger = Logger.getLogger(name);
|
||||||
|
var handler = new ConsoleHandler();
|
||||||
|
handler.setFormatter(new Formatter() {
|
||||||
|
@Override
|
||||||
|
public String format(LogRecord record) {
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.append(ConsoleColors.GREEN).append("[").append(record.getLoggerName()).append("] ");
|
||||||
|
sb.append(ConsoleColors.GREEN).append("[").append(record.getLevel()).append("]: ");
|
||||||
|
sb.append(ConsoleColors.WHITE_BRIGHT).append(record.getMessage());
|
||||||
|
sb.append(ConsoleColors.RESET).append("\n");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
logger.setUseParentHandlers(false);
|
||||||
|
logger.addHandler(handler);
|
||||||
|
logger.setLevel(settings.getLogLevel());
|
||||||
|
if (!settings.isPrintToConsole()) {
|
||||||
|
logger.setLevel(Level.OFF);
|
||||||
|
}
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.http;
|
package io.github.jwdeveloper.tiktok.http;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.common.ActionResult;
|
||||||
import io.github.jwdeveloper.tiktok.data.settings.HttpClientSettings;
|
import io.github.jwdeveloper.tiktok.data.settings.HttpClientSettings;
|
||||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
|
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveRequestException;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@@ -34,36 +35,26 @@ import java.util.regex.*;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class HttpClient {
|
public class HttpClient
|
||||||
|
{
|
||||||
protected final HttpClientSettings httpClientSettings;
|
protected final HttpClientSettings httpClientSettings;
|
||||||
protected final String url;
|
protected final String url;
|
||||||
private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)");
|
private final Pattern pattern = Pattern.compile("charset=(.*?)(?=&|$)");
|
||||||
|
|
||||||
public Optional<HttpResponse<byte[]>> toResponse() {
|
public ActionResult<HttpResponse<byte[]>> toResponse() {
|
||||||
var client = prepareClient();
|
var client = prepareClient();
|
||||||
var request = prepareGetRequest();
|
var request = prepareGetRequest();
|
||||||
try {
|
try {
|
||||||
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||||
if (response.statusCode() != 200) {
|
var result = ActionResult.of(response);
|
||||||
return Optional.empty();
|
return response.statusCode() != 200 ? result.message("HttpResponse Code: ", response.statusCode()).failure() : result.success();
|
||||||
}
|
|
||||||
|
|
||||||
return Optional.of(response);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new TikTokLiveRequestException(e);
|
throw new TikTokLiveRequestException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<String> toJsonResponse() {
|
public ActionResult<String> toJsonResponse() {
|
||||||
var optional = toResponse();
|
return toResponse().map(content -> new String(content.body(), charsetFrom(content.headers())));
|
||||||
if (optional.isEmpty()) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
var response = optional.get();
|
|
||||||
var body = response.body();
|
|
||||||
var charset = charsetFrom(response.headers());
|
|
||||||
return Optional.of(new String(body,charset));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Charset charsetFrom(HttpHeaders headers) {
|
private Charset charsetFrom(HttpHeaders headers) {
|
||||||
|
|||||||
Reference in New Issue
Block a user