mirror of
https://github.com/LukeHagar/plexjava.git
synced 2025-12-11 04:20:58 +00:00
SDK update generated by liblab
This commit is contained in:
10
src/main/java/com/plexsdk/http/ActivitiesClient.java
Normal file
10
src/main/java/com/plexsdk/http/ActivitiesClient.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface ActivitiesClient {
|
||||
ResponseWithHeaders<com.plexsdk.models.GetServerActivitiesResponse> getServerActivities()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> cancelServerActivities(String activityUUID) throws ApiException;
|
||||
}
|
||||
13
src/main/java/com/plexsdk/http/ButlerClient.java
Normal file
13
src/main/java/com/plexsdk/http/ButlerClient.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface ButlerClient {
|
||||
ResponseWithHeaders<com.plexsdk.models.GetButlerTasksResponse> getButlerTasks()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> startAllTasks() throws ApiException;
|
||||
ResponseWithHeaders<String> stopAllTasks() throws ApiException;
|
||||
ResponseWithHeaders<String> startTask(String taskName) throws ApiException;
|
||||
ResponseWithHeaders<String> stopTask(String taskName) throws ApiException;
|
||||
}
|
||||
10
src/main/java/com/plexsdk/http/HubsClient.java
Normal file
10
src/main/java/com/plexsdk/http/HubsClient.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface HubsClient {
|
||||
ResponseWithHeaders<String> getGlobalHubs(Float count, Float onlyTransient) throws ApiException;
|
||||
ResponseWithHeaders<String> getLibraryHubs(Float sectionId, Float count, Float onlyTransient)
|
||||
throws ApiException;
|
||||
}
|
||||
23
src/main/java/com/plexsdk/http/LibraryClient.java
Normal file
23
src/main/java/com/plexsdk/http/LibraryClient.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface LibraryClient {
|
||||
ResponseWithHeaders<String> getFileHash(String url, Float type) throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetRecentlyAddedResponse> getRecentlyAdded()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> getLibraries() throws ApiException;
|
||||
ResponseWithHeaders<String> getLibrary(Float sectionId, Float includeDetails) throws ApiException;
|
||||
ResponseWithHeaders<String> deleteLibrary(Float sectionId) throws ApiException;
|
||||
ResponseWithHeaders<String> getLibraryItems(Float sectionId, Float type, String filter)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> refreshLibrary(Float sectionId) throws ApiException;
|
||||
ResponseWithHeaders<String> getLatestLibraryItems(Float sectionId, Float type, String filter)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> getCommonLibraryItems(Float sectionId, Float type, String filter)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> getMetadata(Float ratingKey) throws ApiException;
|
||||
ResponseWithHeaders<String> getMetadataChildren(Float ratingKey) throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetOnDeckResponse> getOnDeck() throws ApiException;
|
||||
}
|
||||
11
src/main/java/com/plexsdk/http/LogClient.java
Normal file
11
src/main/java/com/plexsdk/http/LogClient.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface LogClient {
|
||||
ResponseWithHeaders<String> logLine(Float level, String message, String source)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> logMultiLine() throws ApiException;
|
||||
ResponseWithHeaders<String> enablePaperTrail() throws ApiException;
|
||||
}
|
||||
11
src/main/java/com/plexsdk/http/MediaClient.java
Normal file
11
src/main/java/com/plexsdk/http/MediaClient.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface MediaClient {
|
||||
ResponseWithHeaders<String> markPlayed(Float key) throws ApiException;
|
||||
ResponseWithHeaders<String> markUnplayed(Float key) throws ApiException;
|
||||
ResponseWithHeaders<String> updatePlayProgress(String key, Float time, String state)
|
||||
throws ApiException;
|
||||
}
|
||||
74
src/main/java/com/plexsdk/http/ModelConverter.java
Normal file
74
src/main/java/com/plexsdk/http/ModelConverter.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
public final class ModelConverter {
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
static {
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
|
||||
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
||||
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
|
||||
}
|
||||
|
||||
private ModelConverter() {}
|
||||
|
||||
public static <T> T convert(final Response response, final Class<T> clazz) {
|
||||
final ResponseBody body = response.body();
|
||||
try {
|
||||
return mapper.readValue(body.string(), clazz);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T convert(final String response, final Class<T> clazz) {
|
||||
try {
|
||||
return mapper.readValue(response, clazz);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T convert(Response response, TypeReference<T> typeReference) {
|
||||
try {
|
||||
return convert(response.body().string(), typeReference);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T convert(String response, TypeReference<T> typeReference) {
|
||||
try {
|
||||
return mapper.readValue(response, typeReference);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String modelToJson(final Object model) {
|
||||
try {
|
||||
return mapper.writeValueAsString(model);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
23
src/main/java/com/plexsdk/http/PlaylistsClient.java
Normal file
23
src/main/java/com/plexsdk/http/PlaylistsClient.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface PlaylistsClient {
|
||||
ResponseWithHeaders<String> createPlaylist(
|
||||
String title,
|
||||
String type,
|
||||
Float smart,
|
||||
String uri,
|
||||
Float playQueueID
|
||||
) throws ApiException;
|
||||
ResponseWithHeaders<String> getPlaylists(String playlistType, Float smart) throws ApiException;
|
||||
ResponseWithHeaders<String> getPlaylist(Float playlistID) throws ApiException;
|
||||
ResponseWithHeaders<String> updatePlaylist(Float playlistID) throws ApiException;
|
||||
ResponseWithHeaders<String> deletePlaylist(Float playlistID) throws ApiException;
|
||||
ResponseWithHeaders<String> getPlaylistContents(Float playlistID, Float type) throws ApiException;
|
||||
ResponseWithHeaders<String> addPlaylistContents(Float playlistID, String uri, Float playQueueID)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> clearPlaylistContents(Float playlistID) throws ApiException;
|
||||
ResponseWithHeaders<String> uploadPlaylist(String path, Float force) throws ApiException;
|
||||
}
|
||||
22
src/main/java/com/plexsdk/http/ResponseWithHeaders.java
Normal file
22
src/main/java/com/plexsdk/http/ResponseWithHeaders.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import okhttp3.Headers;
|
||||
|
||||
public class ResponseWithHeaders<T> {
|
||||
|
||||
private T data;
|
||||
private Headers headers;
|
||||
|
||||
public ResponseWithHeaders(T data, Headers headers) {
|
||||
this.data = data;
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public Headers getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
13
src/main/java/com/plexsdk/http/SearchClient.java
Normal file
13
src/main/java/com/plexsdk/http/SearchClient.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface SearchClient {
|
||||
ResponseWithHeaders<String> performSearch(String query, Float sectionId, Float limit)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> performVoiceSearch(String query, Float sectionId, Float limit)
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetSearchResultsResponse> getSearchResults(String query)
|
||||
throws ApiException;
|
||||
}
|
||||
9
src/main/java/com/plexsdk/http/SecurityClient.java
Normal file
9
src/main/java/com/plexsdk/http/SecurityClient.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface SecurityClient {
|
||||
ResponseWithHeaders<String> getTransientToken(String type, String scope) throws ApiException;
|
||||
ResponseWithHeaders<String> getSourceConnectionInformation(String source) throws ApiException;
|
||||
}
|
||||
28
src/main/java/com/plexsdk/http/ServerClient.java
Normal file
28
src/main/java/com/plexsdk/http/ServerClient.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface ServerClient {
|
||||
ResponseWithHeaders<com.plexsdk.models.GetServerCapabilitiesResponse> getServerCapabilities()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> getServerPreferences() throws ApiException;
|
||||
ResponseWithHeaders<
|
||||
java.util.List<com.plexsdk.models.GetAvailableClientsResponse>
|
||||
> getAvailableClients() throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetDevicesResponse> getDevices() throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetServerIdentityResponse> getServerIdentity()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetMyPlexAccountResponse> getMyPlexAccount()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> getResizedPhoto(
|
||||
Float width,
|
||||
Float height,
|
||||
Integer opacity,
|
||||
Float blur,
|
||||
Float minSize,
|
||||
Float upscale,
|
||||
String url
|
||||
) throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetServerListResponse> getServerList() throws ApiException;
|
||||
}
|
||||
12
src/main/java/com/plexsdk/http/SessionsClient.java
Normal file
12
src/main/java/com/plexsdk/http/SessionsClient.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface SessionsClient {
|
||||
ResponseWithHeaders<String> getSessions() throws ApiException;
|
||||
ResponseWithHeaders<String> getSessionHistory() throws ApiException;
|
||||
ResponseWithHeaders<com.plexsdk.models.GetTranscodeSessionsResponse> getTranscodeSessions()
|
||||
throws ApiException;
|
||||
ResponseWithHeaders<String> stopTranscodeSession(String sessionKey) throws ApiException;
|
||||
}
|
||||
10
src/main/java/com/plexsdk/http/UpdaterClient.java
Normal file
10
src/main/java/com/plexsdk/http/UpdaterClient.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface UpdaterClient {
|
||||
ResponseWithHeaders<String> getUpdateStatus() throws ApiException;
|
||||
ResponseWithHeaders<String> checkForUpdates(String download) throws ApiException;
|
||||
ResponseWithHeaders<String> applyUpdates(String tonight, String skip) throws ApiException;
|
||||
}
|
||||
37
src/main/java/com/plexsdk/http/VideoClient.java
Normal file
37
src/main/java/com/plexsdk/http/VideoClient.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.plexsdk.http;
|
||||
|
||||
import com.plexsdk.exceptions.ApiException;
|
||||
import java.util.List;
|
||||
|
||||
public interface VideoClient {
|
||||
ResponseWithHeaders<String> startUniversalTranscode(
|
||||
Float hasMDE,
|
||||
String path,
|
||||
Float mediaIndex,
|
||||
Float partIndex,
|
||||
String protocol,
|
||||
Float fastSeek,
|
||||
Float directPlay,
|
||||
Float directStream,
|
||||
Float subtitleSize,
|
||||
String subtites,
|
||||
Float audioBoost,
|
||||
String location,
|
||||
Float mediaBufferSize,
|
||||
String session,
|
||||
Float addDebugOverlay,
|
||||
Float autoAdjustQuality
|
||||
) throws ApiException;
|
||||
ResponseWithHeaders<String> getTimeline(
|
||||
Float ratingKey,
|
||||
String key,
|
||||
String state,
|
||||
Float hasMDE,
|
||||
Float time,
|
||||
Float duration,
|
||||
String context,
|
||||
Float playQueueItemID,
|
||||
Float playBackTime,
|
||||
Float row
|
||||
) throws ApiException;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.plexsdk.http.interceptors;
|
||||
|
||||
import com.plexsdk.Configuration;
|
||||
import java.io.IOException;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class ApiKeyInterceptor implements Interceptor {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String apiKey;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String apiKeyHeader;
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
return chain.proceed(addApiKeyToRequest(chain.request()));
|
||||
}
|
||||
|
||||
private Request addApiKeyToRequest(Request request) {
|
||||
return this.apiKey != null && !this.apiKey.isEmpty()
|
||||
? request.newBuilder().addHeader(resolveHeader(), this.apiKey).build()
|
||||
: request;
|
||||
}
|
||||
|
||||
private String resolveHeader() {
|
||||
return this.apiKeyHeader != null && !this.apiKeyHeader.isEmpty()
|
||||
? this.apiKeyHeader
|
||||
: Configuration.DEFAULT_API_KEY_HEADER;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.plexsdk.http.interceptors;
|
||||
|
||||
import com.plexsdk.Configuration;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Request.Builder;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class DefaultHeadersInterceptor implements Interceptor {
|
||||
|
||||
private final Map<String, String> defaultHeaders = new HashMap<>();
|
||||
|
||||
public DefaultHeadersInterceptor() {
|
||||
defaultHeaders.put("User-Agent", Configuration.USER_AGENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
return chain.proceed(addDefaultHeadersToRequest(chain.request()));
|
||||
}
|
||||
|
||||
private Request addDefaultHeadersToRequest(Request request) {
|
||||
if (defaultHeaders.isEmpty()) {
|
||||
return request;
|
||||
}
|
||||
|
||||
Builder requestBuilder = request.newBuilder();
|
||||
|
||||
defaultHeaders.forEach(requestBuilder::addHeader);
|
||||
|
||||
return requestBuilder.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.plexsdk.http.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.stream.IntStream;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class RetryInterceptor implements Interceptor {
|
||||
|
||||
private static final int MAX_RETRIES = 3;
|
||||
private static final double RETRY_DELAY = 150;
|
||||
private static final int[] RETRYABLE_CODES = { 500, 503, 504 };
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
// Try the request and if it fails use exponential backoff to retry
|
||||
Request request = chain.request();
|
||||
Response response = chain.proceed(request);
|
||||
int tryCount = 1;
|
||||
while (!response.isSuccessful() && isRetryable(response) && tryCount - 1 < MAX_RETRIES) {
|
||||
try {
|
||||
Thread.sleep((long) Math.pow(RETRY_DELAY, tryCount - 1));
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
response.close();
|
||||
response = chain.proceed(request);
|
||||
tryCount++;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private boolean isRetryable(Response response) {
|
||||
final int statusCode = response.code();
|
||||
return IntStream.of(RETRYABLE_CODES).anyMatch(x -> x == statusCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.plexsdk.http.util;
|
||||
|
||||
import static com.plexsdk.http.ModelConverter.modelToJson;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Provides static methods to convert values (object, array, boxed types) into string representation for http url.
|
||||
* Usually used for converting objects to be http url friendly (e.g. query or header parameters)
|
||||
*/
|
||||
public class HttpArgumentConverter {
|
||||
|
||||
/**
|
||||
* Converts any value (object, array, primitive) into a string representation.
|
||||
*
|
||||
* @param value The value (object, array or primitive) to be converted.
|
||||
* @return A string representation of the input value in a format that can be used as url query or header parameter value.
|
||||
*/
|
||||
public static String toStringArgument(final Object value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
if (
|
||||
value instanceof String ||
|
||||
value instanceof Number ||
|
||||
value instanceof Boolean ||
|
||||
value instanceof Enum<?>
|
||||
) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
if (value instanceof List<?>) {
|
||||
List<?> list = (List<?>) value;
|
||||
return list
|
||||
.stream()
|
||||
.map(HttpArgumentConverter::toStringArgument)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
return modelToJson(value);
|
||||
}
|
||||
}
|
||||
83
src/main/java/com/plexsdk/http/util/HttpHeaders.java
Normal file
83
src/main/java/com/plexsdk/http/util/HttpHeaders.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package com.plexsdk.http.util;
|
||||
|
||||
import static com.plexsdk.http.util.HttpArgumentConverter.toStringArgument;
|
||||
|
||||
import com.plexsdk.exceptions.ArgumentCannotBeNullException;
|
||||
import okhttp3.Headers;
|
||||
|
||||
/**
|
||||
* HttpHeaders represents a utility class for building HTTP headers.
|
||||
*/
|
||||
public class HttpHeaders {
|
||||
|
||||
/**
|
||||
* Private constructor to prevent direct instantiation of the class.
|
||||
*/
|
||||
private HttpHeaders() {}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the HttpHeaders.Builder.
|
||||
*
|
||||
* @return A new instance of HttpHeaders.Builder.
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder class for constructing HTTP headers.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private final Headers.Builder headersBuilder;
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the Builder.
|
||||
*/
|
||||
private Builder() {
|
||||
this.headersBuilder = new Headers.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a required header with the specified name and value to the headers' builder.
|
||||
*
|
||||
* @param name The name of the header.
|
||||
* @param value The value of the header.
|
||||
* @return The current Builder instance.
|
||||
* @throws ArgumentCannotBeNullException If the value is null.
|
||||
*/
|
||||
public Builder addRequiredHeader(String name, Object value)
|
||||
throws ArgumentCannotBeNullException {
|
||||
if (value == null) {
|
||||
throw new ArgumentCannotBeNullException(name);
|
||||
}
|
||||
this.headersBuilder.add(name, toStringArgument(value));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an optional header with the specified name and value to the headers' builder.
|
||||
*
|
||||
* @param name The name of the header.
|
||||
* @param value The value of the header.
|
||||
* @return The current Builder instance.
|
||||
*/
|
||||
public Builder addOptionalHeader(String name, Object value) {
|
||||
if (value != null) {
|
||||
this.headersBuilder.add(name, toStringArgument(value));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the headers using the configured values in the builder.
|
||||
*
|
||||
* @return The constructed Headers object.
|
||||
*/
|
||||
public Headers build() {
|
||||
return this.headersBuilder.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
102
src/main/java/com/plexsdk/http/util/HttpUrl.java
Normal file
102
src/main/java/com/plexsdk/http/util/HttpUrl.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package com.plexsdk.http.util;
|
||||
|
||||
import static com.plexsdk.http.util.HttpArgumentConverter.toStringArgument;
|
||||
|
||||
import com.plexsdk.exceptions.ArgumentCannotBeNullException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The HttpUrl class provides a convenient way to construct HTTP URLs with query parameters and path parameters.
|
||||
*/
|
||||
public class HttpUrl {
|
||||
|
||||
/**
|
||||
* Private constructor to prevent direct instantiation of the class.
|
||||
*/
|
||||
private HttpUrl() {}
|
||||
|
||||
/**
|
||||
* Creates a new instance of the Builder class to start constructing an HTTP URL.
|
||||
*
|
||||
* @param url The base URL for the HTTP request.
|
||||
* @return A new instance of the Builder class.
|
||||
*/
|
||||
public static Builder builder(String url) {
|
||||
return new Builder(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Builder class provides methods to add query parameters and path parameters to an HTTP URL.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private final okhttp3.HttpUrl.Builder httpUrlBuilder;
|
||||
|
||||
/**
|
||||
* Constructs a new Builder instance with the specified base URL.
|
||||
*
|
||||
* @param url The base URL for the HTTP request.
|
||||
*/
|
||||
private Builder(String url) {
|
||||
this.httpUrlBuilder = Objects.requireNonNull(okhttp3.HttpUrl.parse(url)).newBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a required query parameter to the HTTP URL.
|
||||
*
|
||||
* @param name The name of the query parameter.
|
||||
* @param value The value of the query parameter.
|
||||
* @return The Builder instance.
|
||||
* @throws ArgumentCannotBeNullException If the value is null.
|
||||
*/
|
||||
public Builder addRequiredQueryParameter(String name, Object value)
|
||||
throws ArgumentCannotBeNullException {
|
||||
if (value == null) {
|
||||
throw new ArgumentCannotBeNullException(name);
|
||||
}
|
||||
this.httpUrlBuilder.addQueryParameter(name, toStringArgument(value));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an optional query parameter to the HTTP URL.
|
||||
*
|
||||
* @param name The name of the query parameter.
|
||||
* @param value The value of the query parameter.
|
||||
* @return The Builder instance.
|
||||
*/
|
||||
public Builder addOptionalQueryParameter(String name, Object value) {
|
||||
if (value != null) {
|
||||
this.httpUrlBuilder.addQueryParameter(name, toStringArgument(value));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a path parameter to the HTTP URL.
|
||||
*
|
||||
* @param value The value of the path parameter.
|
||||
* @return The Builder instance.
|
||||
* @throws ArgumentCannotBeNullException If the value is null.
|
||||
*/
|
||||
public Builder addPathParameter(String value) throws ArgumentCannotBeNullException {
|
||||
if (value == null || value.isEmpty()) {
|
||||
throw new ArgumentCannotBeNullException();
|
||||
}
|
||||
this.httpUrlBuilder.addPathSegment(value);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the final HTTP URL.
|
||||
*
|
||||
* @return The constructed HTTP URL as a string.
|
||||
*/
|
||||
public String build() {
|
||||
return this.httpUrlBuilder.build().toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user