22
33import javax .annotation .Nonnull ;
44import javax .annotation .Nullable ;
5- import java .util .Arrays ;
65import java .util .Collections ;
76import java .util .List ;
87import java .util .Map ;
8+ import java .util .function .Function ;
99
1010import org .schabi .newpipe .extractor .exceptions .HttpResponseException ;
1111
1212/**
1313 * A Data class used to hold the results from requests made by the Downloader implementation.
1414 */
15+ @ SuppressWarnings ("checkstyle:NeedBraces" )
1516public class Response {
1617 private final int responseCode ;
1718 private final String responseMessage ;
@@ -33,29 +34,6 @@ public Response(final int responseCode,
3334 this .latestUrl = latestUrl ;
3435 }
3536
36- // CHECKSTYLE:OFF
37- /**
38- * Validates the response codes for the given {@link Response}, and throws
39- * a {@link HttpResponseException} if the code is invalid
40- * @param response The response to validate
41- * @param validResponseCodes Expected valid response codes
42- * @throws HttpResponseException Thrown when the response code is not in {@code validResponseCodes},
43- * or when {@code validResponseCodes} is empty and the code is a 4xx or 5xx error.
44- */
45- // CHECKSTYLE:ON
46- public static void validateResponseCode (final Response response ,
47- final int ... validResponseCodes )
48- throws HttpResponseException {
49- final int code = response .responseCode ();
50- final var throwError = (validResponseCodes == null || validResponseCodes .length == 0 )
51- ? code >= 400 && code <= 599
52- : Arrays .stream (validResponseCodes ).noneMatch (c -> c == code );
53-
54- if (throwError ) {
55- throw new HttpResponseException (response );
56- }
57- }
58-
5937 public int responseCode () {
6038 return responseCode ;
6139 }
@@ -107,21 +85,128 @@ public String getHeader(final String name) {
10785 return null ;
10886 }
10987
110- // CHECKSTYLE:OFF
11188 /**
112- * Helper function simply to make it easier to validate response code inline
113- * before getting the code/body/latestUrl/etc.
114- * Validates the response codes for the given {@link Response}, and throws a {@link HttpResponseException} if the code is invalid
115- * @see Response#validateResponseCode(Response, int...)
116- * @param validResponseCodes Expected valid response codes
117- * @return {@link this} response
118- * @throws HttpResponseException Thrown when the response code is not in {@code validResponseCodes},
119- * or when {@code validResponseCodes} is empty and the code is a 4xx or 5xx error.
89+ * Ensure the response code is 2xx
90+ * @return this {@code Response}
91+ * @throws HttpResponseException if the response code is not 2xx
92+ */
93+ public Response ensureSuccessResponseCode () throws HttpResponseException {
94+ return ensureResponseCodeInRange (200 , 299 );
95+ }
96+
97+ /**
98+ * Ensure the response code is 3xx
99+ * @return this {@code Response}
100+ * @throws HttpResponseException if the response code is not 3xx
101+ */
102+ public Response ensureRedirectResponseCode () throws HttpResponseException {
103+ return ensureResponseCodeInRange (300 , 399 );
104+ }
105+
106+ /**
107+ * Ensure the response code is not 4xx or 5xx
108+ * @return this {@code Response}
109+ * @throws HttpResponseException if the response code is client or server error
110+ */
111+ public Response ensureResponseCodeIsNotError () throws HttpResponseException {
112+ return ensureResponseCodeNotInRange (400 , 599 );
113+ }
114+
115+ /**
116+ * Ensure the HTTP response code is within range of min and max inclusive
117+ * @return this Response
118+ * @throws HttpResponseException if the code is outside the range
119+ */
120+ public Response ensureResponseCodeInRange (final int min , final int max )
121+ throws HttpResponseException {
122+ if (responseCode () < min || responseCode () > max ) {
123+ throw new HttpResponseException (this );
124+ }
125+ return this ;
126+ }
127+
128+ public Response ensureResponseCodeNotInRange (
129+ final int min ,
130+ final int max ,
131+ final Function <Response , HttpResponseException > errorSupplier
132+ )
133+ throws HttpResponseException {
134+ if (min > max ) throw new RuntimeException ("min must be less than max" );
135+ if (responseCode () >= min && responseCode () <= max ) {
136+ throw errorSupplier .apply (this );
137+ }
138+ return this ;
139+ }
140+
141+ public Response ensureResponseCodeNotInRange (final int min , final int max )
142+ throws HttpResponseException {
143+ return ensureResponseCodeNotInRange (min , max , HttpResponseException ::new );
144+ }
145+
146+ /**
147+ * Throw exception if response code is a 4xx client error
148+ * @return this {@code Response}
149+ * @throws HttpResponseException if the response code is 4xx
150+ */
151+ public Response throwIfClientError (
152+ final Function <Response , HttpResponseException > errorSupplier
153+ )
154+ throws HttpResponseException {
155+ return throwIfResponseCodeInRange (400 , 499 , errorSupplier );
156+ }
157+
158+ /**
159+ * Throw exception if response code is a 4xx client error
160+ * @return this {@code Response}
161+ * @throws HttpResponseException if the response code is 4xx
162+ */
163+ public Response throwIfClientError ()
164+ throws HttpResponseException {
165+ return throwIfClientError (HttpResponseException ::new );
166+ }
167+
168+ /**
169+ * Throw exception if response code is a 5xx server error
170+ * @return this {@code Response}
171+ * @throws HttpResponseException if the response code is 4xx
120172 */
121- // CHECKSTYLE:ON
122- public Response validateResponseCode (final int ... validResponseCodes )
173+ public Response throwIfServerError (
174+ final Function <Response , HttpResponseException > errorSupplier
175+ )
123176 throws HttpResponseException {
124- validateResponseCode (this , validResponseCodes );
177+ return throwIfResponseCodeInRange (500 , 599 , errorSupplier );
178+ }
179+
180+ public Response throwIfServerError ()
181+ throws HttpResponseException {
182+ return throwIfServerError (HttpResponseException ::new );
183+ }
184+
185+ public Response throwIfResponseCode (
186+ final int errorCode ,
187+ final Function <Response , HttpResponseException > errorSupplier
188+ )
189+ throws HttpResponseException {
190+ if (responseCode () == errorCode ) {
191+ throw errorSupplier .apply (this );
192+ }
125193 return this ;
126194 }
195+
196+ public Response throwIfResponseCode (final int errorCode ) throws HttpResponseException {
197+ return throwIfResponseCode (errorCode , HttpResponseException ::new );
198+ }
199+
200+ public Response throwIfResponseCodeInRange (final int min ,
201+ final int max ,
202+ final Function <Response ,
203+ HttpResponseException > errorSupplier )
204+ throws HttpResponseException {
205+ return ensureResponseCodeNotInRange (min , max , errorSupplier );
206+ }
207+
208+ public Response throwIfResponseCodeInRange (final int min ,
209+ final int max ) throws HttpResponseException {
210+ return throwIfResponseCodeInRange (min , max , HttpResponseException ::new );
211+ }
127212}
0 commit comments