Skip to content

Commit 6562dbf

Browse files
committed
Support HttpUrl params for better ergonomics
1 parent 372befa commit 6562dbf

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

src/main/java/com/pipedream/api/resources/proxy/AsyncProxyClient.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.pipedream.api.resources.proxy.types.ProxyResponse;
1414
import java.util.Base64;
1515
import java.util.concurrent.CompletableFuture;
16+
import okhttp3.HttpUrl;
1617

1718
public class AsyncProxyClient {
1819
protected final ClientOptions clientOptions;
@@ -43,6 +44,13 @@ public CompletableFuture<ProxyResponse> get(String url, ProxyGetRequest request)
4344
return this.rawClient.get(url64, request).thenApply(response -> response.body());
4445
}
4546

47+
/**
48+
* Forward an authenticated GET request to an external API using an external user's account credentials
49+
*/
50+
public CompletableFuture<ProxyResponse> get(HttpUrl url, ProxyGetRequest request) {
51+
return get(url.toString(), request);
52+
}
53+
4654
/**
4755
* Forward an authenticated GET request to an external API using an external user's account credentials
4856
*/
@@ -51,6 +59,13 @@ public CompletableFuture<ProxyResponse> get(String url, ProxyGetRequest request,
5159
return this.rawClient.get(url64, request, requestOptions).thenApply(response -> response.body());
5260
}
5361

62+
/**
63+
* Forward an authenticated GET request to an external API using an external user's account credentials
64+
*/
65+
public CompletableFuture<ProxyResponse> get(HttpUrl url, ProxyGetRequest request, RequestOptions requestOptions) {
66+
return get(url.toString(), request, requestOptions);
67+
}
68+
5469
/**
5570
* Forward an authenticated POST request to an external API using an external user's account credentials
5671
*/
@@ -59,6 +74,13 @@ public CompletableFuture<ProxyResponse> post(String url, ProxyPostRequest reques
5974
return this.rawClient.post(url64, request).thenApply(response -> response.body());
6075
}
6176

77+
/**
78+
* Forward an authenticated POST request to an external API using an external user's account credentials
79+
*/
80+
public CompletableFuture<ProxyResponse> post(HttpUrl url, ProxyPostRequest request) {
81+
return post(url.toString(), request);
82+
}
83+
6284
/**
6385
* Forward an authenticated POST request to an external API using an external user's account credentials
6486
*/
@@ -67,6 +89,13 @@ public CompletableFuture<ProxyResponse> post(String url, ProxyPostRequest reques
6789
return this.rawClient.post(url64, request, requestOptions).thenApply(response -> response.body());
6890
}
6991

92+
/**
93+
* Forward an authenticated POST request to an external API using an external user's account credentials
94+
*/
95+
public CompletableFuture<ProxyResponse> post(HttpUrl url, ProxyPostRequest request, RequestOptions requestOptions) {
96+
return post(url.toString(), request, requestOptions);
97+
}
98+
7099
/**
71100
* Forward an authenticated PUT request to an external API using an external user's account credentials
72101
*/
@@ -75,6 +104,13 @@ public CompletableFuture<ProxyResponse> put(String url, ProxyPutRequest request)
75104
return this.rawClient.put(url64, request).thenApply(response -> response.body());
76105
}
77106

107+
/**
108+
* Forward an authenticated PUT request to an external API using an external user's account credentials
109+
*/
110+
public CompletableFuture<ProxyResponse> put(HttpUrl url, ProxyPutRequest request) {
111+
return put(url.toString(), request);
112+
}
113+
78114
/**
79115
* Forward an authenticated PUT request to an external API using an external user's account credentials
80116
*/
@@ -83,6 +119,13 @@ public CompletableFuture<ProxyResponse> put(String url, ProxyPutRequest request,
83119
return this.rawClient.put(url64, request, requestOptions).thenApply(response -> response.body());
84120
}
85121

122+
/**
123+
* Forward an authenticated PUT request to an external API using an external user's account credentials
124+
*/
125+
public CompletableFuture<ProxyResponse> put(HttpUrl url, ProxyPutRequest request, RequestOptions requestOptions) {
126+
return put(url.toString(), request, requestOptions);
127+
}
128+
86129
/**
87130
* Forward an authenticated DELETE request to an external API using an external user's account credentials
88131
*/
@@ -91,6 +134,13 @@ public CompletableFuture<ProxyResponse> delete(String url, ProxyDeleteRequest re
91134
return this.rawClient.delete(url64, request).thenApply(response -> response.body());
92135
}
93136

137+
/**
138+
* Forward an authenticated DELETE request to an external API using an external user's account credentials
139+
*/
140+
public CompletableFuture<ProxyResponse> delete(HttpUrl url, ProxyDeleteRequest request) {
141+
return delete(url.toString(), request);
142+
}
143+
94144
/**
95145
* Forward an authenticated DELETE request to an external API using an external user's account credentials
96146
*/
@@ -100,6 +150,14 @@ public CompletableFuture<ProxyResponse> delete(
100150
return this.rawClient.delete(url64, request, requestOptions).thenApply(response -> response.body());
101151
}
102152

153+
/**
154+
* Forward an authenticated DELETE request to an external API using an external user's account credentials
155+
*/
156+
public CompletableFuture<ProxyResponse> delete(
157+
HttpUrl url, ProxyDeleteRequest request, RequestOptions requestOptions) {
158+
return delete(url.toString(), request, requestOptions);
159+
}
160+
103161
/**
104162
* Forward an authenticated PATCH request to an external API using an external user's account credentials
105163
*/
@@ -108,6 +166,13 @@ public CompletableFuture<ProxyResponse> patch(String url, ProxyPatchRequest requ
108166
return this.rawClient.patch(url64, request).thenApply(response -> response.body());
109167
}
110168

169+
/**
170+
* Forward an authenticated PATCH request to an external API using an external user's account credentials
171+
*/
172+
public CompletableFuture<ProxyResponse> patch(HttpUrl url, ProxyPatchRequest request) {
173+
return patch(url.toString(), request);
174+
}
175+
111176
/**
112177
* Forward an authenticated PATCH request to an external API using an external user's account credentials
113178
*/
@@ -116,4 +181,12 @@ public CompletableFuture<ProxyResponse> patch(
116181
final String url64 = encodeUrl(url);
117182
return this.rawClient.patch(url64, request, requestOptions).thenApply(response -> response.body());
118183
}
184+
185+
/**
186+
* Forward an authenticated PATCH request to an external API using an external user's account credentials
187+
*/
188+
public CompletableFuture<ProxyResponse> patch(
189+
HttpUrl url, ProxyPatchRequest request, RequestOptions requestOptions) {
190+
return patch(url.toString(), request, requestOptions);
191+
}
119192
}

src/main/java/com/pipedream/api/resources/proxy/ProxyClient.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.pipedream.api.resources.proxy.requests.ProxyPutRequest;
1313
import com.pipedream.api.resources.proxy.types.ProxyResponse;
1414
import java.util.Base64;
15+
import okhttp3.HttpUrl;
1516

1617
public class ProxyClient {
1718
protected final ClientOptions clientOptions;
@@ -42,6 +43,13 @@ public ProxyResponse get(String url, ProxyGetRequest request) {
4243
return this.rawClient.get(url64, request).body();
4344
}
4445

46+
/**
47+
* Forward an authenticated GET request to an external API using an external user's account credentials
48+
*/
49+
public ProxyResponse get(HttpUrl url, ProxyGetRequest request) {
50+
return get(url.toString(), request);
51+
}
52+
4553
/**
4654
* Forward an authenticated GET request to an external API using an external user's account credentials
4755
*/
@@ -50,6 +58,13 @@ public ProxyResponse get(String url, ProxyGetRequest request, RequestOptions req
5058
return this.rawClient.get(url64, request, requestOptions).body();
5159
}
5260

61+
/**
62+
* Forward an authenticated GET request to an external API using an external user's account credentials
63+
*/
64+
public ProxyResponse get(HttpUrl url, ProxyGetRequest request, RequestOptions requestOptions) {
65+
return get(url.toString(), request, requestOptions);
66+
}
67+
5368
/**
5469
* Forward an authenticated POST request to an external API using an external user's account credentials
5570
*/
@@ -58,6 +73,13 @@ public ProxyResponse post(String url, ProxyPostRequest request) {
5873
return this.rawClient.post(url64, request).body();
5974
}
6075

76+
/**
77+
* Forward an authenticated POST request to an external API using an external user's account credentials
78+
*/
79+
public ProxyResponse post(HttpUrl url, ProxyPostRequest request) {
80+
return post(url.toString(), request);
81+
}
82+
6183
/**
6284
* Forward an authenticated POST request to an external API using an external user's account credentials
6385
*/
@@ -66,6 +88,13 @@ public ProxyResponse post(String url, ProxyPostRequest request, RequestOptions r
6688
return this.rawClient.post(url64, request, requestOptions).body();
6789
}
6890

91+
/**
92+
* Forward an authenticated POST request to an external API using an external user's account credentials
93+
*/
94+
public ProxyResponse post(HttpUrl url, ProxyPostRequest request, RequestOptions requestOptions) {
95+
return post(url.toString(), request, requestOptions);
96+
}
97+
6998
/**
7099
* Forward an authenticated PUT request to an external API using an external user's account credentials
71100
*/
@@ -74,6 +103,13 @@ public ProxyResponse put(String url, ProxyPutRequest request) {
74103
return this.rawClient.put(url64, request).body();
75104
}
76105

106+
/**
107+
* Forward an authenticated PUT request to an external API using an external user's account credentials
108+
*/
109+
public ProxyResponse put(HttpUrl url, ProxyPutRequest request) {
110+
return put(url.toString(), request);
111+
}
112+
77113
/**
78114
* Forward an authenticated PUT request to an external API using an external user's account credentials
79115
*/
@@ -82,6 +118,13 @@ public ProxyResponse put(String url, ProxyPutRequest request, RequestOptions req
82118
return this.rawClient.put(url64, request, requestOptions).body();
83119
}
84120

121+
/**
122+
* Forward an authenticated PUT request to an external API using an external user's account credentials
123+
*/
124+
public ProxyResponse put(HttpUrl url, ProxyPutRequest request, RequestOptions requestOptions) {
125+
return put(url.toString(), request, requestOptions);
126+
}
127+
85128
/**
86129
* Forward an authenticated DELETE request to an external API using an external user's account credentials
87130
*/
@@ -90,6 +133,13 @@ public ProxyResponse delete(String url, ProxyDeleteRequest request) {
90133
return this.rawClient.delete(url64, request).body();
91134
}
92135

136+
/**
137+
* Forward an authenticated DELETE request to an external API using an external user's account credentials
138+
*/
139+
public ProxyResponse delete(HttpUrl url, ProxyDeleteRequest request) {
140+
return delete(url.toString(), request);
141+
}
142+
93143
/**
94144
* Forward an authenticated DELETE request to an external API using an external user's account credentials
95145
*/
@@ -98,6 +148,13 @@ public ProxyResponse delete(String url, ProxyDeleteRequest request, RequestOptio
98148
return this.rawClient.delete(url64, request, requestOptions).body();
99149
}
100150

151+
/**
152+
* Forward an authenticated DELETE request to an external API using an external user's account credentials
153+
*/
154+
public ProxyResponse delete(HttpUrl url, ProxyDeleteRequest request, RequestOptions requestOptions) {
155+
return delete(url.toString(), request, requestOptions);
156+
}
157+
101158
/**
102159
* Forward an authenticated PATCH request to an external API using an external user's account credentials
103160
*/
@@ -106,11 +163,25 @@ public ProxyResponse patch(String url, ProxyPatchRequest request) {
106163
return this.rawClient.patch(url64, request).body();
107164
}
108165

166+
/**
167+
* Forward an authenticated PATCH request to an external API using an external user's account credentials
168+
*/
169+
public ProxyResponse patch(HttpUrl url, ProxyPatchRequest request) {
170+
return patch(url.toString(), request);
171+
}
172+
109173
/**
110174
* Forward an authenticated PATCH request to an external API using an external user's account credentials
111175
*/
112176
public ProxyResponse patch(String url, ProxyPatchRequest request, RequestOptions requestOptions) {
113177
final String url64 = encodeUrl(url);
114178
return this.rawClient.patch(url64, request, requestOptions).body();
115179
}
180+
181+
/**
182+
* Forward an authenticated PATCH request to an external API using an external user's account credentials
183+
*/
184+
public ProxyResponse patch(HttpUrl url, ProxyPatchRequest request, RequestOptions requestOptions) {
185+
return patch(url.toString(), request, requestOptions);
186+
}
116187
}

src/test/java/com/pipedream/api/ProxyClientWireTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.concurrent.CompletableFuture;
1212
import java.util.concurrent.TimeUnit;
13+
import okhttp3.HttpUrl;
1314
import okhttp3.mockwebserver.MockResponse;
1415
import okhttp3.mockwebserver.MockWebServer;
1516
import okhttp3.mockwebserver.RecordedRequest;
@@ -295,4 +296,94 @@ public void testAsyncGetRedirectFollowed() throws Exception {
295296
assertNotNull(secondRequest.getPath());
296297
assertTrue(secondRequest.getPath().contains("async-redirected"));
297298
}
299+
300+
// ==================== HttpUrl Overload Tests ====================
301+
302+
@Test
303+
public void testGetWithHttpUrl() throws Exception {
304+
String jsonBody = "{\"key\":\"value\"}";
305+
server.enqueue(new MockResponse()
306+
.setResponseCode(200)
307+
.setHeader("Content-Type", "application/json")
308+
.setBody(jsonBody));
309+
310+
// Build URL with query parameters using HttpUrl
311+
HttpUrl url = HttpUrl.parse(TEST_URL)
312+
.newBuilder()
313+
.addQueryParameter("fields", "name,mimeType")
314+
.addQueryParameter("limit", "10")
315+
.build();
316+
317+
try (ProxyResponse response = client.proxy().get(url, createGetRequest())) {
318+
assertTrue(response.isJson(), "Response should be JSON");
319+
320+
@SuppressWarnings("unchecked")
321+
Map<String, Object> jsonMap = (Map<String, Object>) response.json();
322+
assertEquals("value", jsonMap.get("key"));
323+
}
324+
325+
// Verify request was made correctly
326+
RecordedRequest request = server.takeRequest();
327+
assertNotNull(request);
328+
assertEquals("GET", request.getMethod());
329+
330+
// Verify the base64-encoded URL in the path contains the query parameters
331+
String path = request.getPath();
332+
assertNotNull(path);
333+
// The URL with query params should be base64-encoded in the path
334+
// We can't easily decode it here, but we verify the request was made
335+
}
336+
337+
@Test
338+
public void testGetWithHttpUrlAndSpecialCharacters() throws Exception {
339+
String jsonBody = "{\"result\":\"ok\"}";
340+
server.enqueue(new MockResponse()
341+
.setResponseCode(200)
342+
.setHeader("Content-Type", "application/json")
343+
.setBody(jsonBody));
344+
345+
// Build URL with query parameters containing special characters
346+
HttpUrl url = HttpUrl.parse(TEST_URL)
347+
.newBuilder()
348+
.addQueryParameter("filter", "status=active&type=user")
349+
.addQueryParameter("name", "John Doe")
350+
.build();
351+
352+
try (ProxyResponse response = client.proxy().get(url, createGetRequest())) {
353+
assertTrue(response.isJson(), "Response should be JSON");
354+
}
355+
356+
RecordedRequest request = server.takeRequest();
357+
assertNotNull(request);
358+
assertEquals("GET", request.getMethod());
359+
}
360+
361+
@Test
362+
public void testAsyncGetWithHttpUrl() throws Exception {
363+
String jsonBody = "{\"async\":true}";
364+
server.enqueue(new MockResponse()
365+
.setResponseCode(200)
366+
.setHeader("Content-Type", "application/json")
367+
.setBody(jsonBody));
368+
369+
// Build URL with query parameters using HttpUrl
370+
HttpUrl url = HttpUrl.parse(TEST_URL)
371+
.newBuilder()
372+
.addQueryParameter("page", "1")
373+
.addQueryParameter("size", "20")
374+
.build();
375+
376+
CompletableFuture<ProxyResponse> future = asyncClient.proxy().get(url, createGetRequest());
377+
try (ProxyResponse response = future.get(10, TimeUnit.SECONDS)) {
378+
assertTrue(response.isJson(), "Response should be JSON");
379+
380+
@SuppressWarnings("unchecked")
381+
Map<String, Object> jsonMap = (Map<String, Object>) response.json();
382+
assertEquals(true, jsonMap.get("async"));
383+
}
384+
385+
RecordedRequest request = server.takeRequest();
386+
assertNotNull(request);
387+
assertEquals("GET", request.getMethod());
388+
}
298389
}

0 commit comments

Comments
 (0)