Skip to content

Commit 773b671

Browse files
AudricVStypox
andcommitted
Add postWithContentType and postWithContentTypeJson utility methods in Downloader
Co-authored-by: Stypox <[email protected]>
1 parent fcfb68f commit 773b671

File tree

1 file changed

+94
-3
lines changed
  • extractor/src/main/java/org/schabi/newpipe/extractor/downloader

1 file changed

+94
-3
lines changed

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

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import javax.annotation.Nonnull;
88
import javax.annotation.Nullable;
99
import java.io.IOException;
10+
import java.util.Collections;
11+
import java.util.HashMap;
1012
import java.util.List;
1113
import java.util.Map;
1214

@@ -39,7 +41,7 @@ public Response get(final String url) throws IOException, ReCaptchaException {
3941
* @param localization the source of the value of the {@code Accept-Language} header
4042
* @return the result of the GET request
4143
*/
42-
public Response get(final String url, @Nullable final Localization localization)
44+
public Response get(final String url, final Localization localization)
4345
throws IOException, ReCaptchaException {
4446
return get(url, null, localization);
4547
}
@@ -70,7 +72,7 @@ public Response get(final String url, @Nullable final Map<String, List<String>>
7072
*/
7173
public Response get(final String url,
7274
@Nullable final Map<String, List<String>> headers,
73-
@Nullable final Localization localization)
75+
final Localization localization)
7476
throws IOException, ReCaptchaException {
7577
return execute(Request.newBuilder()
7678
.get(url)
@@ -136,7 +138,7 @@ public Response post(final String url,
136138
public Response post(final String url,
137139
@Nullable final Map<String, List<String>> headers,
138140
@Nullable final byte[] dataToSend,
139-
@Nullable final Localization localization)
141+
final Localization localization)
140142
throws IOException, ReCaptchaException {
141143
return execute(Request.newBuilder()
142144
.post(url, dataToSend)
@@ -145,6 +147,95 @@ public Response post(final String url,
145147
.build());
146148
}
147149

150+
/**
151+
* Convenient method to send a POST request using the specified value of the
152+
* {@code Content-Type} header with a given {@link Localization}.
153+
*
154+
* @param url the URL that is pointing to the wanted resource
155+
* @param headers a list of headers that will be used in the request.
156+
* Any default headers <b>should</b> be overridden by these.
157+
* @param dataToSend byte array that will be sent when doing the request.
158+
* @param localization the source of the value of the {@code Accept-Language} header
159+
* @param contentType the mime type of the body sent, which will be set as the value of the
160+
* {@code Content-Type} header
161+
* @return the result of the POST request
162+
* @see #post(String, Map, byte[], Localization)
163+
*/
164+
public Response postWithContentType(final String url,
165+
@Nullable final Map<String, List<String>> headers,
166+
@Nullable final byte[] dataToSend,
167+
final Localization localization,
168+
final String contentType)
169+
throws IOException, ReCaptchaException {
170+
final Map<String, List<String>> actualHeaders = new HashMap<>();
171+
if (headers != null) {
172+
actualHeaders.putAll(headers);
173+
}
174+
actualHeaders.put("Content-Type", Collections.singletonList(contentType));
175+
return post(url, actualHeaders, dataToSend, localization);
176+
}
177+
178+
/**
179+
* Convenient method to send a POST request using the specified value of the
180+
* {@code Content-Type} header.
181+
*
182+
* @param url the URL that is pointing to the wanted resource
183+
* @param headers a list of headers that will be used in the request.
184+
* Any default headers <b>should</b> be overridden by these.
185+
* @param dataToSend byte array that will be sent when doing the request.
186+
* @param contentType the mime type of the body sent, which will be set as the value of the
187+
* {@code Content-Type} header
188+
* @return the result of the POST request
189+
* @see #post(String, Map, byte[], Localization)
190+
*/
191+
public Response postWithContentType(final String url,
192+
@Nullable final Map<String, List<String>> headers,
193+
@Nullable final byte[] dataToSend,
194+
final String contentType)
195+
throws IOException, ReCaptchaException {
196+
return postWithContentType(url, headers, dataToSend, NewPipe.getPreferredLocalization(),
197+
contentType);
198+
}
199+
200+
/**
201+
* Convenient method to send a POST request the JSON mime type as the value of the
202+
* {@code Content-Type} header with a given {@link Localization}.
203+
*
204+
* @param url the URL that is pointing to the wanted resource
205+
* @param headers a list of headers that will be used in the request.
206+
* Any default headers <b>should</b> be overridden by these.
207+
* @param dataToSend byte array that will be sent when doing the request.
208+
* @param localization the source of the value of the {@code Accept-Language} header
209+
* @return the result of the POST request
210+
* @see #post(String, Map, byte[], Localization)
211+
*/
212+
public Response postWithContentTypeJson(final String url,
213+
@Nullable final Map<String, List<String>> headers,
214+
@Nullable final byte[] dataToSend,
215+
final Localization localization)
216+
throws IOException, ReCaptchaException {
217+
return postWithContentType(url, headers, dataToSend, localization, "application/json");
218+
}
219+
220+
/**
221+
* Convenient method to send a POST request the JSON mime type as the value of the
222+
* {@code Content-Type} header.
223+
*
224+
* @param url the URL that is pointing to the wanted resource
225+
* @param headers a list of headers that will be used in the request.
226+
* Any default headers <b>should</b> be overridden by these.
227+
* @param dataToSend byte array that will be sent when doing the request.
228+
* @return the result of the POST request
229+
* @see #post(String, Map, byte[], Localization)
230+
*/
231+
public Response postWithContentTypeJson(final String url,
232+
@Nullable final Map<String, List<String>> headers,
233+
@Nullable final byte[] dataToSend)
234+
throws IOException, ReCaptchaException {
235+
return postWithContentTypeJson(url, headers, dataToSend,
236+
NewPipe.getPreferredLocalization());
237+
}
238+
148239
/**
149240
* Do a request using the specified {@link Request} object.
150241
*

0 commit comments

Comments
 (0)