Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the private key string directly #36

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Add the `gr4vy-java` dependency to your pom.xml:
<dependency>
<groupId>com.github.gr4vy</groupId>
<artifactId>gr4vy-java</artifactId>
<version>0.18.0</version>
<version>0.20.0</version>
</dependency>
```

Expand Down Expand Up @@ -111,7 +111,7 @@ needs to be created before it can be used in this way.
## Initialization

The client can be initialized with the Gr4vy ID (`gr4vyId`) and the location of your
private key string.
private key string (see Setting Private Key below).

```java
Gr4vyClient gr4vyClient = new Gr4vyClient("[YOUR_GR4VY_ID]", "private_key.pem");
Expand All @@ -125,9 +125,19 @@ of the server to use directly.
gr4vyClient.setHost("https://api.acme.gr4vy.app")
```

## Setting Private Key

Your API private key can be created in your admin panel on the **Integrations**
tab.

There are three methods of setting the private key when using the SDK:

1. Setting the private key location in the Gr4vyClient
2. Calling `client.setPrivateKeyString("<PRIVATE_KEY_STRING>");`
3. Setting the environment variable `PRIVATE_KEY`

NOTE: When options 2 & 3 are used, the private key location in the Initialization
can be null.

## Making API calls

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>gr4vy</artifactId>
<packaging>jar</packaging>
<name>gr4vy</name>
<version>0.19.0</version>
<version>0.20.0</version>
<url>https://gr4vy.com</url>
<description>Gr4vy Java SDK</description>

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/gr4vy/sdk/Gr4vyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
public class Gr4vyClient {
private OkHttpClient okClient;
private String privateKeyLocation;
private String privateKeyString = null;
private String host;
private String environment;
private Boolean debug = false;
Expand Down Expand Up @@ -89,6 +90,10 @@ public Gr4vyClient(String gr4vyId, String privateKeyLocation, Boolean debug, Str
this.host = "https://api." + apiPrefix + gr4vyId + ".gr4vy.app";
this.debug = debug;
}

public void setPrivateKeyString(String privateKeyString) {
this.privateKeyString = privateKeyString;
}

public void setMerchantAccountId(String merchantAccountId) {
this.merchantAccountId = merchantAccountId;
Expand Down Expand Up @@ -209,6 +214,9 @@ private static ECPublicKey publicFromPrivate(ECPrivateKey key) throws NoSuchAlgo
}

public String getKey() {
if (this.privateKeyString != null) {
return this.privateKeyString;
}
String value = System.getenv("PRIVATE_KEY");
if (value != null) {
return value;
Expand Down
Loading