Skip to content

Commit 58fd08b

Browse files
committed
Expose Request to ProxyProvider.
1 parent 244ade7 commit 58fd08b

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Page download(Request request, Task task) {
7777
}
7878
CloseableHttpResponse httpResponse = null;
7979
CloseableHttpClient httpClient = getHttpClient(task.getSite());
80-
Proxy proxy = proxyProvider != null ? proxyProvider.getProxy(task) : null;
80+
Proxy proxy = proxyProvider != null ? proxyProvider.getProxy(request, task) : null;
8181
HttpClientRequestContext requestContext = httpUriRequestConverter.convert(request, task.getSite(), proxy);
8282
Page page = Page.fail();
8383
try {

webmagic-core/src/main/java/us/codecraft/webmagic/proxy/ProxyProvider.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package us.codecraft.webmagic.proxy;
22

33
import us.codecraft.webmagic.Page;
4+
import us.codecraft.webmagic.Request;
45
import us.codecraft.webmagic.Task;
56

67
/**
@@ -23,7 +24,23 @@ public interface ProxyProvider {
2324
* Get a proxy for task by some strategy.
2425
* @param task the download task
2526
* @return proxy
27+
* @deprecated Use {@link #getProxy(Request, Task)} instead.
2628
*/
27-
Proxy getProxy(Task task);
29+
@Deprecated
30+
default Proxy getProxy(Task task) {
31+
throw new UnsupportedOperationException();
32+
}
33+
34+
/**
35+
* Returns a proxy for the request.
36+
*
37+
* @param request the request
38+
* @param task the download task
39+
* @return proxy
40+
* @since 0.9.0
41+
*/
42+
default Proxy getProxy(Request request, Task task) {
43+
return this.getProxy(task);
44+
}
2845

2946
}

webmagic-core/src/main/java/us/codecraft/webmagic/proxy/SimpleProxyProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package us.codecraft.webmagic.proxy;
22

33
import us.codecraft.webmagic.Page;
4+
import us.codecraft.webmagic.Request;
45
import us.codecraft.webmagic.Task;
56

67
import java.util.ArrayList;
@@ -44,7 +45,7 @@ public void returnProxy(Proxy proxy, Page page, Task task) {
4445
}
4546

4647
@Override
47-
public Proxy getProxy(Task task) {
48+
public Proxy getProxy(Request request, Task task) {
4849
return proxies.get(incrForLoop());
4950
}
5051

webmagic-core/src/test/java/us/codecraft/webmagic/proxy/SimpleProxyProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void test_get_proxy() throws Exception {
2020
Proxy originProxy1 = new Proxy("127.0.0.1", 1087);
2121
Proxy originProxy2 = new Proxy("127.0.0.1", 1088);
2222
SimpleProxyProvider proxyProvider = SimpleProxyProvider.from(originProxy1, originProxy2);
23-
Proxy proxy = proxyProvider.getProxy(TASK);
23+
Proxy proxy = proxyProvider.getProxy(null, TASK);
2424
assertThat(proxy).isEqualTo(originProxy1);
2525
proxy = proxyProvider.getProxy(TASK);
2626
assertThat(proxy).isEqualTo(originProxy2);

0 commit comments

Comments
 (0)