Skip to content

Commit 103433a

Browse files
committed
refactor: move request param creation to YoutubeClient to avoid duplication in implementations
1 parent f8d1033 commit 103433a

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

lib/src/main/java/io/github/thoroldvix/api/YoutubeClient.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.util.Map;
55

6+
67
/**
78
* Responsible for sending GET requests to YouTube.
89
*/
@@ -13,7 +14,9 @@ public interface YoutubeClient {
1314
*
1415
* @param url The URL to which the GET request is made.
1516
* @param headers A map of additional headers to include in the request.
17+
*
1618
* @return The body of the response as a {@link String}.
19+
*
1720
* @throws TranscriptRetrievalException If the request to YouTube fails.
1821
*/
1922
String get(String url, Map<String, String> headers) throws TranscriptRetrievalException;
@@ -24,9 +27,31 @@ public interface YoutubeClient {
2427
*
2528
* @param endpoint The endpoint to which the GET request is made.
2629
* @param params A map of parameters to include in the request.
30+
*
2731
* @return The body of the response as a {@link String}.
32+
*
2833
* @throws TranscriptRetrievalException If the request to YouTube fails.
2934
*/
3035
String get(YtApiV3Endpoint endpoint, Map<String, String> params) throws TranscriptRetrievalException;
36+
37+
38+
/**
39+
* Creates a string representation of the specified parameters.
40+
*
41+
* @param params A map of parameters to include in the request.
42+
*
43+
* @return A string representation of the specified parameters.
44+
*/
45+
default String createParamsString(Map<String, String> params) {
46+
StringBuilder paramString = new StringBuilder();
47+
48+
for (Map.Entry<String, String> entry : params.entrySet()) {
49+
String value = entry.getValue().replaceAll(" ", "%20");
50+
paramString.append(entry.getKey()).append("=").append(value).append("&");
51+
}
52+
53+
paramString.deleteCharAt(paramString.length() - 1);
54+
return paramString.toString();
55+
}
3156
}
3257

lib/src/main/java/io/github/thoroldvix/internal/DefaultYoutubeClient.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ public String get(YtApiV3Endpoint endpoint, Map<String, String> params) throws T
7777
return response.body();
7878
}
7979

80-
private String createParamsString(Map<String, String> params) {
81-
StringBuilder paramString = new StringBuilder();
82-
83-
for (Map.Entry<String, String> entry : params.entrySet()) {
84-
String value = formatValue(entry.getValue());
85-
paramString.append(entry.getKey()).append("=").append(value).append("&");
86-
}
87-
88-
paramString.deleteCharAt(paramString.length() - 1);
89-
return paramString.toString();
90-
}
91-
92-
private String formatValue(String value) {
93-
return value.replaceAll(" ", "%20");
94-
}
95-
9680
private String[] createHeaders(Map<String, String> headers) {
9781
String[] headersArray = new String[headers.size() * 2];
9882
int i = 0;

0 commit comments

Comments
 (0)