Skip to content

Commit c7357b3

Browse files
committed
Draft
1 parent 44cf3fa commit c7357b3

File tree

8 files changed

+265
-47
lines changed

8 files changed

+265
-47
lines changed

.fernignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Specify files that shouldn't be modified by Fern
22
.github/workflows/auto-close-empty-prs.yml
3+
.github/workflows/ci.yml
34
LICENSE
45

6+
src/main/java/com/pipedream/api/AsyncBaseClient.java
7+
src/main/java/com/pipedream/api/AsyncBaseClientBuilder.java
8+
src/main/java/com/pipedream/api/BaseClient.java
9+
src/main/java/com/pipedream/api/BaseClientBuilder.java
10+
src/main/java/com/pipedream/api/PipedreamClient.java
11+
src/main/java/com/pipedream/api/PipedreamClientBuilder.java
12+
src/main/java/com/pipedream/api/AsyncPipedreamClient.java
13+
src/main/java/com/pipedream/api/AsyncPipedreamClientBuilder.java

src/main/java/com/pipedream/api/AsyncBaseClientBuilder.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Optional;
1111
import okhttp3.OkHttpClient;
1212

13-
public class AsyncBaseClientBuilder {
13+
public class AsyncBaseClientBuilder<T extends AsyncBaseClientBuilder<T>> {
1414
private Optional<Integer> timeout = Optional.empty();
1515

1616
private Optional<Integer> maxRetries = Optional.empty();
@@ -29,65 +29,68 @@ public class AsyncBaseClientBuilder {
2929
* Sets clientId.
3030
* Defaults to the PIPEDREAM_CLIENT_ID environment variable.
3131
*/
32-
public AsyncBaseClientBuilder clientId(String clientId) {
32+
@SuppressWarnings("unchecked")
33+
public T clientId(String clientId) {
3334
this.clientId = clientId;
34-
return this;
35+
return (T) this;
3536
}
3637

3738
/**
3839
* Sets clientSecret.
3940
* Defaults to the PIPEDREAM_CLIENT_SECRET environment variable.
4041
*/
41-
public AsyncBaseClientBuilder clientSecret(String clientSecret) {
42+
@SuppressWarnings("unchecked")
43+
public T clientSecret(String clientSecret) {
4244
this.clientSecret = clientSecret;
43-
return this;
45+
return (T) this;
4446
}
4547

4648
/**
4749
* Sets projectEnvironment
4850
*/
49-
public AsyncBaseClientBuilder projectEnvironment(String projectEnvironment) {
51+
@SuppressWarnings("unchecked")
52+
public T projectEnvironment(String projectEnvironment) {
5053
this.projectEnvironment = projectEnvironment;
51-
return this;
54+
return (T) this;
5255
}
5356

54-
public AsyncBaseClientBuilder environment(Environment environment) {
57+
@SuppressWarnings("unchecked")
58+
public T environment(Environment environment) {
5559
this.environment = environment;
56-
return this;
60+
return (T) this;
5761
}
5862

59-
public AsyncBaseClientBuilder url(String url) {
63+
@SuppressWarnings("unchecked")
64+
public T url(String url) {
6065
this.environment = Environment.custom(url);
61-
return this;
66+
return (T) this;
6267
}
6368

6469
/**
6570
* Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
6671
*/
67-
public AsyncBaseClientBuilder timeout(int timeout) {
72+
@SuppressWarnings("unchecked")
73+
public T timeout(int timeout) {
6874
this.timeout = Optional.of(timeout);
69-
return this;
75+
return (T) this;
7076
}
7177

7278
/**
7379
* Sets the maximum number of retries for the client. Defaults to 2 retries.
7480
*/
75-
public AsyncBaseClientBuilder maxRetries(int maxRetries) {
81+
@SuppressWarnings("unchecked")
82+
public T maxRetries(int maxRetries) {
7683
this.maxRetries = Optional.of(maxRetries);
77-
return this;
84+
return (T) this;
7885
}
7986

8087
/**
8188
* Sets the underlying OkHttp client
8289
*/
83-
public AsyncBaseClientBuilder httpClient(OkHttpClient httpClient) {
90+
@SuppressWarnings("unchecked")
91+
public T httpClient(OkHttpClient httpClient) {
8492
this.httpClient = httpClient;
85-
return this;
86-
}
87-
88-
public AsyncBaseClientBuilder projectId(String projectId) {
89-
clientOptionsBuilder.projectId(projectId);
90-
return this;
93+
return (T) this;
9194
}
9295

9396
protected ClientOptions buildClientOptions() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pipedream.api;
2+
3+
import com.pipedream.api.core.ClientOptions;
4+
import com.pipedream.api.core.Environment;
5+
import org.immutables.value.Value;
6+
7+
import java.util.Optional;
8+
9+
@Value
10+
public class AsyncPipedreamClient extends AsyncBaseClient {
11+
public AsyncPipedreamClient(final ClientOptions clientOptions) {
12+
super(clientOptions);
13+
}
14+
15+
public static AsyncPipedreamClientBuilder builder() {
16+
return new AsyncPipedreamClientBuilder()
17+
.clientId(System.getenv("PIPEDREAM_CLIENT_ID"))
18+
.clientSecret(System.getenv("PIPEDREAM_CLIENT_SECRET"))
19+
.environment(Environment.PROD)
20+
.projectEnvironment(System.getenv("PIPEDREAM_PROJECT_ENVIRONMENT"))
21+
.projectId(System.getenv("PIPEDREAM_PROJECT_ID"));
22+
23+
}
24+
25+
/**
26+
* Returns an access token that can be used to authenticate API requests
27+
*
28+
* @return the access token string (if available)
29+
*/
30+
public Optional<String> rawAccessToken() {
31+
final String authorizationHeader = this.clientOptions
32+
.headers(null)
33+
.get("Authorization");
34+
35+
// The header might not be defined, so we wrap it as an Optional to
36+
// further process it. The processing consists of removing the `Bearer`
37+
// or `Basic` prefix from the header value.
38+
return Optional
39+
.ofNullable(authorizationHeader)
40+
.map(h -> h.replaceFirst("^.*?\\s+", ""));
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pipedream.api;
2+
3+
import com.pipedream.api.core.ClientOptions;
4+
import com.pipedream.api.core.Environment;
5+
import org.apache.commons.text.StringSubstitutor;
6+
import org.apache.commons.text.lookup.StringLookupFactory;
7+
8+
/**
9+
* Builder for creating PipedreamClient instances.
10+
*/
11+
public final class AsyncPipedreamClientBuilder extends AsyncBaseClientBuilder<AsyncPipedreamClientBuilder> {
12+
private String projectId;
13+
14+
public AsyncPipedreamClient build() {
15+
return new AsyncPipedreamClient(buildClientOptions());
16+
}
17+
18+
public AsyncPipedreamClientBuilder environment(final Environment environment) {
19+
final String patchedUrl = patchUrl(environment.getUrl());
20+
final Environment withPatchedUrl = Environment.custom(patchedUrl);
21+
super.environment(withPatchedUrl);
22+
return this;
23+
}
24+
25+
public AsyncPipedreamClientBuilder projectId(final String projectId) {
26+
this.projectId = projectId;
27+
return this;
28+
}
29+
30+
@Override
31+
public void setVariables(ClientOptions.Builder builder) {
32+
builder.projectId(this.projectId);
33+
}
34+
35+
private static String patchUrl(final String templateUrl) {
36+
StringSubstitutor sub = new StringSubstitutor(
37+
StringLookupFactory.INSTANCE.environmentVariableStringLookup()
38+
);
39+
40+
return sub.replace(templateUrl);
41+
}
42+
}

src/main/java/com/pipedream/api/BaseClientBuilder.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Optional;
1111
import okhttp3.OkHttpClient;
1212

13-
public class BaseClientBuilder {
13+
public class BaseClientBuilder<T extends BaseClientBuilder<T>> {
1414
private Optional<Integer> timeout = Optional.empty();
1515

1616
private Optional<Integer> maxRetries = Optional.empty();
@@ -29,65 +29,68 @@ public class BaseClientBuilder {
2929
* Sets clientId.
3030
* Defaults to the PIPEDREAM_CLIENT_ID environment variable.
3131
*/
32-
public BaseClientBuilder clientId(String clientId) {
32+
@SuppressWarnings("unchecked")
33+
public T clientId(String clientId) {
3334
this.clientId = clientId;
34-
return this;
35+
return (T) this;
3536
}
3637

3738
/**
3839
* Sets clientSecret.
3940
* Defaults to the PIPEDREAM_CLIENT_SECRET environment variable.
4041
*/
41-
public BaseClientBuilder clientSecret(String clientSecret) {
42+
@SuppressWarnings("unchecked")
43+
public T clientSecret(String clientSecret) {
4244
this.clientSecret = clientSecret;
43-
return this;
45+
return (T) this;
4446
}
4547

4648
/**
4749
* Sets projectEnvironment
4850
*/
49-
public BaseClientBuilder projectEnvironment(String projectEnvironment) {
51+
@SuppressWarnings("unchecked")
52+
public T projectEnvironment(String projectEnvironment) {
5053
this.projectEnvironment = projectEnvironment;
51-
return this;
54+
return (T) this;
5255
}
5356

54-
public BaseClientBuilder environment(Environment environment) {
57+
@SuppressWarnings("unchecked")
58+
public T environment(Environment environment) {
5559
this.environment = environment;
56-
return this;
60+
return (T) this;
5761
}
5862

59-
public BaseClientBuilder url(String url) {
63+
@SuppressWarnings("unchecked")
64+
public T url(String url) {
6065
this.environment = Environment.custom(url);
61-
return this;
66+
return (T) this;
6267
}
6368

6469
/**
6570
* Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
6671
*/
67-
public BaseClientBuilder timeout(int timeout) {
72+
@SuppressWarnings("unchecked")
73+
public T timeout(int timeout) {
6874
this.timeout = Optional.of(timeout);
69-
return this;
75+
return (T) this;
7076
}
7177

7278
/**
7379
* Sets the maximum number of retries for the client. Defaults to 2 retries.
7480
*/
75-
public BaseClientBuilder maxRetries(int maxRetries) {
81+
@SuppressWarnings("unchecked")
82+
public T maxRetries(int maxRetries) {
7683
this.maxRetries = Optional.of(maxRetries);
77-
return this;
84+
return (T) this;
7885
}
7986

8087
/**
8188
* Sets the underlying OkHttp client
8289
*/
83-
public BaseClientBuilder httpClient(OkHttpClient httpClient) {
90+
@SuppressWarnings("unchecked")
91+
public T httpClient(OkHttpClient httpClient) {
8492
this.httpClient = httpClient;
85-
return this;
86-
}
87-
88-
public BaseClientBuilder projectId(String projectId) {
89-
clientOptionsBuilder.projectId(projectId);
90-
return this;
93+
return (T) this;
9194
}
9295

9396
protected ClientOptions buildClientOptions() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pipedream.api;
2+
3+
import com.pipedream.api.core.ClientOptions;
4+
import com.pipedream.api.core.Environment;
5+
import org.immutables.value.Value;
6+
7+
import java.util.Optional;
8+
9+
@Value
10+
public class PipedreamClient extends BaseClient {
11+
public PipedreamClient(final ClientOptions clientOptions) {
12+
super(clientOptions);
13+
}
14+
15+
public static PipedreamClientBuilder builder() {
16+
return new PipedreamClientBuilder()
17+
.clientId(System.getenv("PIPEDREAM_CLIENT_ID"))
18+
.clientSecret(System.getenv("PIPEDREAM_CLIENT_SECRET"))
19+
.environment(Environment.PROD)
20+
.projectEnvironment(System.getenv("PIPEDREAM_PROJECT_ENVIRONMENT"))
21+
.projectId(System.getenv("PIPEDREAM_PROJECT_ID"));
22+
23+
}
24+
25+
/**
26+
* Returns an access token that can be used to authenticate API requests
27+
*
28+
* @return the access token string (if available)
29+
*/
30+
public Optional<String> rawAccessToken() {
31+
final String authorizationHeader = this.clientOptions
32+
.headers(null)
33+
.get("Authorization");
34+
35+
// The header might not be defined, so we wrap it as an Optional to
36+
// further process it. The processing consists of removing the `Bearer`
37+
// or `Basic` prefix from the header value.
38+
return Optional
39+
.ofNullable(authorizationHeader)
40+
.map(h -> h.replaceFirst("^.*?\\s+", ""));
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pipedream.api;
2+
3+
import com.pipedream.api.core.ClientOptions;
4+
import com.pipedream.api.core.Environment;
5+
import org.apache.commons.text.StringSubstitutor;
6+
import org.apache.commons.text.lookup.StringLookupFactory;
7+
8+
/**
9+
* Builder for creating PipedreamClient instances.
10+
*/
11+
public final class PipedreamClientBuilder extends BaseClientBuilder<PipedreamClientBuilder> {
12+
private String projectId;
13+
14+
public PipedreamClient build() {
15+
return new PipedreamClient(buildClientOptions());
16+
}
17+
18+
public PipedreamClientBuilder environment(final Environment environment) {
19+
final String patchedUrl = patchUrl(environment.getUrl());
20+
final Environment withPatchedUrl = Environment.custom(patchedUrl);
21+
super.environment(withPatchedUrl);
22+
return this;
23+
}
24+
25+
public PipedreamClientBuilder projectId(final String projectId) {
26+
this.projectId = projectId;
27+
return this;
28+
}
29+
30+
@Override
31+
public void setVariables(ClientOptions.Builder builder) {
32+
builder.projectId(this.projectId);
33+
}
34+
35+
private static String patchUrl(final String templateUrl) {
36+
StringSubstitutor sub = new StringSubstitutor(
37+
StringLookupFactory.INSTANCE.environmentVariableStringLookup()
38+
);
39+
40+
return sub.replace(templateUrl);
41+
}
42+
}

0 commit comments

Comments
 (0)