Skip to content

Commit

Permalink
refactor: simplify api by moving url params creation to YtApiV3Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoroldvix committed Oct 10, 2024
1 parent ca52fcd commit 4887928
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
20 changes: 0 additions & 20 deletions lib/src/main/java/io/github/thoroldvix/api/YoutubeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,5 @@ public interface YoutubeClient {
* @throws TranscriptRetrievalException If the request to YouTube fails.
*/
String get(YtApiV3Endpoint endpoint, Map<String, String> params) throws TranscriptRetrievalException;


/**
* Creates a string representation of the specified parameters.
*
* @param params A map of parameters to include in the request.
*
* @return A string representation of the specified parameters.
*/
default String createParamsString(Map<String, String> params) {
StringBuilder paramString = new StringBuilder();

for (Map.Entry<String, String> entry : params.entrySet()) {
String value = entry.getValue().replaceAll(" ", "%20");
paramString.append(entry.getKey()).append("=").append(value).append("&");
}

paramString.deleteCharAt(paramString.length() - 1);
return paramString.toString();
}
}

27 changes: 27 additions & 0 deletions lib/src/main/java/io/github/thoroldvix/api/YtApiV3Endpoint.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.thoroldvix.api;

import java.util.Map;


/**
* The YouTube API V3 endpoints. Used by the {@link YoutubeClient}.
*/
Expand All @@ -17,10 +20,34 @@ public enum YtApiV3Endpoint {
this.resource = resource;
}

private static String createParamsString(Map<String, String> params) {
StringBuilder paramString = new StringBuilder();

for (Map.Entry<String, String> entry : params.entrySet()) {
String value = entry.getValue().replaceAll(" ", "%20");
paramString.append(entry.getKey()).append("=").append(value).append("&");
}

paramString.deleteCharAt(paramString.length() - 1);
return paramString.toString();
}

/**
* @return The URL of the endpoint.
*/
public String url() {
return url;
}

/**
* @param params A map of parameters to include in the request.
*
* @return The URL of the endpoint with the specified parameters.
*/
public String url(Map<String, String> params) {
return url + "?" + createParamsString(params);
}

@Override
public String toString() {
return resource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.github.thoroldvix.internal;

import io.github.thoroldvix.api.TranscriptRetrievalException;
import io.github.thoroldvix.api.YoutubeClient;
import io.github.thoroldvix.api.YtApiV3Endpoint;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;

import io.github.thoroldvix.api.TranscriptRetrievalException;
import io.github.thoroldvix.api.YoutubeClient;
import io.github.thoroldvix.api.YtApiV3Endpoint;


/**
* Default implementation of {@link YoutubeClient}.
*/
Expand Down Expand Up @@ -54,10 +55,9 @@ public String get(String url, Map<String, String> headers) throws TranscriptRetr

@Override
public String get(YtApiV3Endpoint endpoint, Map<String, String> params) throws TranscriptRetrievalException {
String paramsString = createParamsString(params);
String errorMessage = String.format("Request to YouTube '%s' endpoint failed.", endpoint);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpoint.url() + "?" + paramsString))
.uri(URI.create(endpoint.url(params)))
.build();

HttpResponse<String> response;
Expand Down

0 comments on commit 4887928

Please sign in to comment.