Skip to content

Commit ae4e8fc

Browse files
Move validateResponseCode to Response
Make HttpResponseException extend ExtractionException Remove HttpUtils
1 parent 58094d8 commit ae4e8fc

File tree

4 files changed

+30
-43
lines changed

4 files changed

+30
-43
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
5+
import java.util.Arrays;
56
import java.util.Collections;
67
import java.util.List;
78
import java.util.Map;
89

910
import org.schabi.newpipe.extractor.exceptions.HttpResponseException;
10-
import org.schabi.newpipe.extractor.utils.HttpUtils;
1111

1212
/**
1313
* A Data class used to hold the results from requests made by the Downloader implementation.
@@ -33,6 +33,28 @@ public Response(final int responseCode,
3333
this.latestUrl = latestUrl;
3434
}
3535

36+
/**
37+
* Validates the response codes for the given {@link Response}, and throws
38+
* a {@link HttpResponseException} if the code is invalid
39+
* @param response The response to validate
40+
* @param validResponseCodes Expected valid response codes
41+
* @throws HttpResponseException Thrown when the response code is not in {@code validResponseCodes},
42+
* or when {@code validResponseCodes} is empty and the code is a 4xx or 5xx error.
43+
*/
44+
// CHECKSTYLE:ON
45+
public static void validateResponseCode(final Response response,
46+
final int... validResponseCodes)
47+
throws HttpResponseException {
48+
final int code = response.responseCode();
49+
final var throwError = (validResponseCodes == null || validResponseCodes.length == 0)
50+
? code >= 400 && code <= 599
51+
: Arrays.stream(validResponseCodes).noneMatch(c -> c == code);
52+
53+
if (throwError) {
54+
throw new HttpResponseException(response);
55+
}
56+
}
57+
3658
public int responseCode() {
3759
return responseCode;
3860
}
@@ -83,12 +105,13 @@ public String getHeader(final String name) {
83105

84106
return null;
85107
}
108+
86109
// CHECKSTYLE:OFF
87110
/**
88111
* Helper function simply to make it easier to validate response code inline
89112
* before getting the code/body/latestUrl/etc.
90113
* Validates the response codes for the given {@link Response}, and throws a {@link HttpResponseException} if the code is invalid
91-
* @see HttpUtils#validateResponseCode(Response, int...)
114+
* @see Response#validateResponseCode(Response, int...)
92115
* @param validResponseCodes Expected valid response codes
93116
* @return {@link this} response
94117
* @throws HttpResponseException Thrown when the response code is not in {@code validResponseCodes},
@@ -97,7 +120,7 @@ public String getHeader(final String name) {
97120
// CHECKSTYLE:ON
98121
public Response validateResponseCode(final int... validResponseCodes)
99122
throws HttpResponseException {
100-
HttpUtils.validateResponseCode(this, validResponseCodes);
123+
validateResponseCode(this, validResponseCodes);
101124
return this;
102125
}
103126
}

extractor/src/main/java/org/schabi/newpipe/extractor/exceptions/HttpResponseException.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.schabi.newpipe.extractor.exceptions;
22

3-
import java.io.IOException;
43
import org.schabi.newpipe.extractor.downloader.Response;
54

6-
public class HttpResponseException extends IOException {
5+
public class HttpResponseException extends ExtractionException {
76
public HttpResponseException(final Response response) {
87
this("Error in HTTP Response for " + response.latestUrl() + "\n\t"
98
+ response.responseCode() + " - " + response.responseMessage());

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
66
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
77
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
8-
import static org.schabi.newpipe.extractor.utils.HttpUtils.validateResponseCode;
8+
import static org.schabi.newpipe.extractor.downloader.Response.validateResponseCode;
99

1010
import com.grack.nanojson.JsonArray;
1111
import com.grack.nanojson.JsonObject;
@@ -21,6 +21,7 @@
2121
import org.schabi.newpipe.extractor.downloader.Downloader;
2222
import org.schabi.newpipe.extractor.downloader.Response;
2323
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
24+
import org.schabi.newpipe.extractor.exceptions.HttpResponseException;
2425
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2526
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
2627
import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudChannelInfoItemExtractor;
@@ -178,7 +179,7 @@ public static JsonObject resolveFor(@Nonnull final Downloader downloader, final
178179
* @return the url resolved
179180
*/
180181
public static String resolveUrlWithEmbedPlayer(final String apiUrl) throws IOException,
181-
ReCaptchaException {
182+
ReCaptchaException, HttpResponseException {
182183

183184
final var response = NewPipe.getDownloader().get("https://w.soundcloud.com/player/?url="
184185
+ Utils.encodeUrlUtf8(apiUrl), SoundCloud.getLocalization());

extractor/src/main/java/org/schabi/newpipe/extractor/utils/HttpUtils.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)