Skip to content

Commit

Permalink
Allow modification of Transloadit-Client header (#81)
Browse files Browse the repository at this point in the history
* Getter and Setter in Transloadit.java

* Getter and Setter in Transloadit.java

* Getter and Setter in Transloadit.java

* Getter and Setter Tests in TransloaditTest.java

* Getter and Setter Tests in TransloaditTest.java

* Finish on Transloadit.class modifications

* Finish modifications

* Update Linter to more recent version in order to solve its issues

* Update Linter to more recent version in order to solve its issues

* Make it possible to add an unknown SDK version

* Make the linter Happy again

* Make the linter Happy again

* Protected modifier for both methods

* Updated header management

* Changed version.properties location

* Changed version.properties location in Transloadit.class in order to fix jUnit test, which obtained the version.properties file from the tus library

* Prepared 0.4.2 Release

* Update CHANGELOG.md

Co-authored-by: Marius <marius@transloadit.com>
  • Loading branch information
cdr-chakotay and Acconut authored Feb 3, 2022
1 parent ec1ff5c commit 17823ed
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.4.2 / 2021-01-29 ###
* Added possibility for SDKs using this SDK to send their own version number to the server in the Transloadit-Client header.
* Resolved some file-name conflicts with the tus-java-client library.

### 0.4.1 / 2021-09-26 ###
* Added debugging features regarding HTTP-requests, which should not be used in production without contacting Transloadit support.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Existing users should take note of the [JCenter shutdown](https://jfrog.com/blog
**Gradle:**

```groovy
implementation 'com.transloadit.sdk:transloadit:0.3.0'
implementation 'com.transloadit.sdk:transloadit:0.4.2'
```

**Maven:**
Expand All @@ -29,7 +29,7 @@ implementation 'com.transloadit.sdk:transloadit:0.3.0'
<dependency>
<groupId>com.transloadit.sdk</groupId>
<artifactId>transloadit</artifactId>
<version>0.4.1</version>
<version>0.4.2</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ targetCompatibility = 1.8

group 'com.transloadit.sdk'

def config = new ConfigSlurper().parse(new File("${projectDir}/src/main/resources/java-sdk-version.properties").toURI().toURL())
def config = new ConfigSlurper().parse(new File("${projectDir}/src/main/resources/java-sdk-version/version.properties").toURI().toURL())
version = config.versionNumber
description = "A Java Integration of the Transloadit's (https://transloadit.com) file uploading and encoding service."

Expand All @@ -26,7 +26,7 @@ dependencies {
implementation 'io.tus.java.client:tus-java-client:0.4.5'
implementation 'joda-time:joda-time:2.10.13'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'org.json:json:20210307'
implementation 'org.json:json:20211205'
implementation 'commons-codec:commons-codec:1.15'
implementation 'io.socket:socket.io-client:1.0.1'
testImplementation 'junit:junit:4.13.2'
Expand Down
31 changes: 10 additions & 21 deletions src/main/java/com/transloadit/sdk/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

/**
Expand All @@ -37,33 +36,23 @@
public class Request {
private Transloadit transloadit;
private OkHttpClient httpClient = new OkHttpClient();
private String version;
private int retryAttemptsRateLimitLeft;
protected int retryAttemptsRequestExceptionLeft;
private ArrayList<String> qualifiedErrorsForRetry;
private int retryDelay;
private String version;

/**
* Constructs a new instance of the {@link Request} object in to prepare a new HTTP-Request to the Transloadit API.
* @param transloadit The {@link Transloadit} Client
*/
Request(Transloadit transloadit) {
this.transloadit = transloadit;
retryAttemptsRateLimitLeft = transloadit.getRetryAttemptsRateLimit();
retryAttemptsRequestExceptionLeft = transloadit.getRetryAttemptsRequestException();
qualifiedErrorsForRetry = transloadit.getQualifiedErrorsForRetry();
retryDelay = transloadit.getRetryDelay();
Properties prop = new Properties();
InputStream in = getClass().getClassLoader().getResourceAsStream("java-sdk-version.properties");
try {
prop.load(in);
version = "java-sdk:" + prop.getProperty("versionNumber").replace("'", "");
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NullPointerException npe) {
version = "java-sdk:unknown";
}
this.retryAttemptsRateLimitLeft = transloadit.getRetryAttemptsRateLimit();
this.retryAttemptsRequestExceptionLeft = transloadit.getRetryAttemptsRequestException();
this.qualifiedErrorsForRetry = transloadit.getQualifiedErrorsForRetry();
this.retryDelay = transloadit.getRetryDelay();
this.version = transloadit.getVersionInfo();
}

/**
Expand All @@ -79,7 +68,7 @@ okhttp3.Response get(String url, Map<String, Object> params)
String fullUrl = getFullUrl(url);
okhttp3.Request request = new okhttp3.Request.Builder()
.url(addUrlParams(fullUrl, toPayload(params)))
.addHeader("Transloadit-Client", version)
.addHeader("Transloadit-Client", this.version)
.build();

try {
Expand Down Expand Up @@ -128,7 +117,7 @@ okhttp3.Response post(String url, Map<String, Object> params,

okhttp3.Request request = new okhttp3.Request.Builder().url(getFullUrl(url))
.post(getBody(payload, files, fileStreams))
.addHeader("Transloadit-Client", version)
.addHeader("Transloadit-Client", this.version)
.build();

try {
Expand Down Expand Up @@ -176,7 +165,7 @@ okhttp3.Response delete(String url, Map<String, Object> params)
okhttp3.Request request = new okhttp3.Request.Builder()
.url(getFullUrl(url))
.delete(getBody(toPayload(params), null))
.addHeader("Transloadit-Client", version)
.addHeader("Transloadit-Client", this.version)
.build();

try {
Expand Down Expand Up @@ -206,7 +195,7 @@ okhttp3.Response put(String url, Map<String, Object> data)
okhttp3.Request request = new okhttp3.Request.Builder()
.url(getFullUrl(url))
.put(getBody(toPayload(data), null))
.addHeader("Transloadit-Client", version)
.addHeader("Transloadit-Client", this.version)
.build();

try {
Expand Down
43 changes: 38 additions & 5 deletions src/main/java/com/transloadit/sdk/Transloadit.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import com.transloadit.sdk.response.Response;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import java.util.Properties;

/**
* This class serves as a client interface to the Transloadit API.
Expand All @@ -33,6 +35,7 @@ public class Transloadit {
protected int retryAttemptsRequestException = 0; // default value
protected ArrayList<String> qualifiedErrorsForRetry;
protected int retryDelay = 0; // default value
protected String versionInfo;

/**
* A new instance to transloadit client.
Expand All @@ -47,8 +50,10 @@ public Transloadit(String key, @Nullable String secret, long duration, String ho
this.secret = secret;
this.duration = duration;
this.hostUrl = hostUrl;
shouldSignRequest = secret != null;
qualifiedErrorsForRetry = new ArrayList<String>(Collections.singletonList("java.net.SocketTimeoutException"));
this.shouldSignRequest = secret != null;
this.qualifiedErrorsForRetry = new ArrayList<String>(
Collections.singletonList("java.net.SocketTimeoutException"));
this.versionInfo = loadVersionInfo();
}
/**
* A new instance to transloadit client.
Expand Down Expand Up @@ -95,6 +100,36 @@ public void setRequestSigning(boolean flag) throws LocalOperationException {
}
}

/**
* Loads the current version from the version.properties File and builds an Info String for the
* Transloadit-Client header.
* @return String with content: java-sdk:version
*/
protected String loadVersionInfo() {
Properties prop = new Properties();
String versionInfo;
InputStream in = getClass().getClassLoader().getResourceAsStream("java-sdk-version/version.properties");
try {
prop.load(in);
versionInfo = "java-sdk:" + prop.getProperty("versionNumber").replace("'", "");
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NullPointerException npe) {
versionInfo = "java-sdk:unknown";
}
return versionInfo;
}

/**
* Returns a version Info String, which can be sent as header-value with a {@link Request}.
* @return Version info as Header-String
*/
String getVersionInfo() {
return this.versionInfo;
}


/**
* Adjusts number of retry attempts that should be taken if a "RATE_LIMIT_REACHED" error appears
* during assembly processing.
Expand Down Expand Up @@ -395,6 +430,4 @@ public void setRetryDelay(int delay) throws LocalOperationException {
this.retryDelay = delay;
}
}


}
1 change: 0 additions & 1 deletion src/main/resources/java-sdk-version.properties

This file was deleted.

1 change: 1 addition & 0 deletions src/main/resources/java-sdk-version/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
versionNumber='0.4.2'
3 changes: 2 additions & 1 deletion src/test/java/com/transloadit/sdk/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public void setUp() throws Exception {
public void get() throws Exception {
request.get("/foo");


mockServerClient.verify(HttpRequest.request()
.withPath("/foo")
.withMethod("GET")
.withHeader("Transloadit-Client", "java-sdk:0.4.1"));
.withHeader("Transloadit-Client", "java-sdk:0.4.2"));

}

Expand Down
20 changes: 17 additions & 3 deletions src/test/java/com/transloadit/sdk/TransloaditTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
//CHECKSTYLE:OFF
import java.util.Map; // Suppress warning as the Map import is needed for the JavaDoc Comments
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//CHECKSTYLE:ON

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


/**
* Unit test for {@link Transloadit} class. Api-Responses are simulated by mocking the server's response.
*/
Expand Down Expand Up @@ -235,7 +237,7 @@ public void getBill() throws LocalOperationException, RequestException, IOExcept
* {@link Transloadit#setQualifiedErrorsForRetry(ArrayList)} are working as supposed.
*/
@Test
public void getAndsetqualifiedErrorsForRetry() {
public void getAndSetqualifiedErrorsForRetry() {
ArrayList<String> exceptionTemplate = new ArrayList<String>();
exceptionTemplate.add("java.net.SocketTimeoutException");
exceptionTemplate.add("Socket.blah.Exception");
Expand All @@ -258,7 +260,7 @@ public void getAndsetqualifiedErrorsForRetry() {
* @throws LocalOperationException
*/
@Test
public void getAndsetTimeoutRetry() throws LocalOperationException {
public void getAndSetTimeoutRetry() throws LocalOperationException {
assertEquals(0, transloadit.getRetryDelay());
transloadit.setRetryDelay(5);
assertEquals(5, transloadit.getRetryDelay());
Expand All @@ -271,5 +273,17 @@ public void getAndsetTimeoutRetry() throws LocalOperationException {
assertTrue(exception instanceof LocalOperationException);

}

/**
* Tests if the version Info is obtained correctly with {@link Transloadit#loadVersionInfo()}.
*/
@Test
public void loadVersionInfo() {
String info = transloadit.loadVersionInfo();
Pattern versionPattern = Pattern.compile(
"^[a-z-]*[:]([0-9]+)\\.([0-9]+)\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = versionPattern.matcher(info);
assertTrue(matcher.find());
}
}

0 comments on commit 17823ed

Please sign in to comment.