Skip to content

Commit 17823ed

Browse files
cdr-chakotayAcconut
andauthoredFeb 3, 2022
Allow modification of Transloadit-Client header (#81)
* 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>
1 parent ec1ff5c commit 17823ed

File tree

9 files changed

+76
-35
lines changed

9 files changed

+76
-35
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 0.4.2 / 2021-01-29 ###
2+
* Added possibility for SDKs using this SDK to send their own version number to the server in the Transloadit-Client header.
3+
* Resolved some file-name conflicts with the tus-java-client library.
4+
15
### 0.4.1 / 2021-09-26 ###
26
* Added debugging features regarding HTTP-requests, which should not be used in production without contacting Transloadit support.
37

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Existing users should take note of the [JCenter shutdown](https://jfrog.com/blog
2020
**Gradle:**
2121

2222
```groovy
23-
implementation 'com.transloadit.sdk:transloadit:0.3.0'
23+
implementation 'com.transloadit.sdk:transloadit:0.4.2'
2424
```
2525

2626
**Maven:**
@@ -29,7 +29,7 @@ implementation 'com.transloadit.sdk:transloadit:0.3.0'
2929
<dependency>
3030
<groupId>com.transloadit.sdk</groupId>
3131
<artifactId>transloadit</artifactId>
32-
<version>0.4.1</version>
32+
<version>0.4.2</version>
3333
</dependency>
3434
```
3535

‎build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ targetCompatibility = 1.8
1111

1212
group 'com.transloadit.sdk'
1313

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

@@ -26,7 +26,7 @@ dependencies {
2626
implementation 'io.tus.java.client:tus-java-client:0.4.5'
2727
implementation 'joda-time:joda-time:2.10.13'
2828
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
29-
implementation 'org.json:json:20210307'
29+
implementation 'org.json:json:20211205'
3030
implementation 'commons-codec:commons-codec:1.15'
3131
implementation 'io.socket:socket.io-client:1.0.1'
3232
testImplementation 'junit:junit:4.13.2'

‎src/main/java/com/transloadit/sdk/Request.java

+10-21
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.ArrayList;
2929
import java.util.HashMap;
3030
import java.util.Map;
31-
import java.util.Properties;
3231
import java.util.Random;
3332

3433
/**
@@ -37,33 +36,23 @@
3736
public class Request {
3837
private Transloadit transloadit;
3938
private OkHttpClient httpClient = new OkHttpClient();
40-
private String version;
4139
private int retryAttemptsRateLimitLeft;
4240
protected int retryAttemptsRequestExceptionLeft;
4341
private ArrayList<String> qualifiedErrorsForRetry;
4442
private int retryDelay;
43+
private String version;
4544

4645
/**
4746
* Constructs a new instance of the {@link Request} object in to prepare a new HTTP-Request to the Transloadit API.
4847
* @param transloadit The {@link Transloadit} Client
4948
*/
5049
Request(Transloadit transloadit) {
5150
this.transloadit = transloadit;
52-
retryAttemptsRateLimitLeft = transloadit.getRetryAttemptsRateLimit();
53-
retryAttemptsRequestExceptionLeft = transloadit.getRetryAttemptsRequestException();
54-
qualifiedErrorsForRetry = transloadit.getQualifiedErrorsForRetry();
55-
retryDelay = transloadit.getRetryDelay();
56-
Properties prop = new Properties();
57-
InputStream in = getClass().getClassLoader().getResourceAsStream("java-sdk-version.properties");
58-
try {
59-
prop.load(in);
60-
version = "java-sdk:" + prop.getProperty("versionNumber").replace("'", "");
61-
in.close();
62-
} catch (IOException e) {
63-
throw new RuntimeException(e);
64-
} catch (NullPointerException npe) {
65-
version = "java-sdk:unknown";
66-
}
51+
this.retryAttemptsRateLimitLeft = transloadit.getRetryAttemptsRateLimit();
52+
this.retryAttemptsRequestExceptionLeft = transloadit.getRetryAttemptsRequestException();
53+
this.qualifiedErrorsForRetry = transloadit.getQualifiedErrorsForRetry();
54+
this.retryDelay = transloadit.getRetryDelay();
55+
this.version = transloadit.getVersionInfo();
6756
}
6857

6958
/**
@@ -79,7 +68,7 @@ okhttp3.Response get(String url, Map<String, Object> params)
7968
String fullUrl = getFullUrl(url);
8069
okhttp3.Request request = new okhttp3.Request.Builder()
8170
.url(addUrlParams(fullUrl, toPayload(params)))
82-
.addHeader("Transloadit-Client", version)
71+
.addHeader("Transloadit-Client", this.version)
8372
.build();
8473

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

129118
okhttp3.Request request = new okhttp3.Request.Builder().url(getFullUrl(url))
130119
.post(getBody(payload, files, fileStreams))
131-
.addHeader("Transloadit-Client", version)
120+
.addHeader("Transloadit-Client", this.version)
132121
.build();
133122

134123
try {
@@ -176,7 +165,7 @@ okhttp3.Response delete(String url, Map<String, Object> params)
176165
okhttp3.Request request = new okhttp3.Request.Builder()
177166
.url(getFullUrl(url))
178167
.delete(getBody(toPayload(params), null))
179-
.addHeader("Transloadit-Client", version)
168+
.addHeader("Transloadit-Client", this.version)
180169
.build();
181170

182171
try {
@@ -206,7 +195,7 @@ okhttp3.Response put(String url, Map<String, Object> data)
206195
okhttp3.Request request = new okhttp3.Request.Builder()
207196
.url(getFullUrl(url))
208197
.put(getBody(toPayload(data), null))
209-
.addHeader("Transloadit-Client", version)
198+
.addHeader("Transloadit-Client", this.version)
210199
.build();
211200

212201
try {

‎src/main/java/com/transloadit/sdk/Transloadit.java

+38-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import com.transloadit.sdk.response.Response;
1111
import org.jetbrains.annotations.Nullable;
1212

13+
import java.io.IOException;
14+
import java.io.InputStream;
1315
import java.util.ArrayList;
1416
import java.util.Collections;
1517
import java.util.HashMap;
1618
import java.util.Map;
17-
19+
import java.util.Properties;
1820

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

3740
/**
3841
* A new instance to transloadit client.
@@ -47,8 +50,10 @@ public Transloadit(String key, @Nullable String secret, long duration, String ho
4750
this.secret = secret;
4851
this.duration = duration;
4952
this.hostUrl = hostUrl;
50-
shouldSignRequest = secret != null;
51-
qualifiedErrorsForRetry = new ArrayList<String>(Collections.singletonList("java.net.SocketTimeoutException"));
53+
this.shouldSignRequest = secret != null;
54+
this.qualifiedErrorsForRetry = new ArrayList<String>(
55+
Collections.singletonList("java.net.SocketTimeoutException"));
56+
this.versionInfo = loadVersionInfo();
5257
}
5358
/**
5459
* A new instance to transloadit client.
@@ -95,6 +100,36 @@ public void setRequestSigning(boolean flag) throws LocalOperationException {
95100
}
96101
}
97102

103+
/**
104+
* Loads the current version from the version.properties File and builds an Info String for the
105+
* Transloadit-Client header.
106+
* @return String with content: java-sdk:version
107+
*/
108+
protected String loadVersionInfo() {
109+
Properties prop = new Properties();
110+
String versionInfo;
111+
InputStream in = getClass().getClassLoader().getResourceAsStream("java-sdk-version/version.properties");
112+
try {
113+
prop.load(in);
114+
versionInfo = "java-sdk:" + prop.getProperty("versionNumber").replace("'", "");
115+
in.close();
116+
} catch (IOException e) {
117+
throw new RuntimeException(e);
118+
} catch (NullPointerException npe) {
119+
versionInfo = "java-sdk:unknown";
120+
}
121+
return versionInfo;
122+
}
123+
124+
/**
125+
* Returns a version Info String, which can be sent as header-value with a {@link Request}.
126+
* @return Version info as Header-String
127+
*/
128+
String getVersionInfo() {
129+
return this.versionInfo;
130+
}
131+
132+
98133
/**
99134
* Adjusts number of retry attempts that should be taken if a "RATE_LIMIT_REACHED" error appears
100135
* during assembly processing.
@@ -395,6 +430,4 @@ public void setRetryDelay(int delay) throws LocalOperationException {
395430
this.retryDelay = delay;
396431
}
397432
}
398-
399-
400433
}

‎src/main/resources/java-sdk-version.properties

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
versionNumber='0.4.2'

‎src/test/java/com/transloadit/sdk/RequestTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ public void setUp() throws Exception {
6161
public void get() throws Exception {
6262
request.get("/foo");
6363

64+
6465
mockServerClient.verify(HttpRequest.request()
6566
.withPath("/foo")
6667
.withMethod("GET")
67-
.withHeader("Transloadit-Client", "java-sdk:0.4.1"));
68+
.withHeader("Transloadit-Client", "java-sdk:0.4.2"));
6869

6970
}
7071

‎src/test/java/com/transloadit/sdk/TransloaditTest.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
//CHECKSTYLE:OFF
1919
import java.util.Map; // Suppress warning as the Map import is needed for the JavaDoc Comments
2020
import java.util.ArrayList;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
2123
//CHECKSTYLE:ON
22-
2324
import static org.junit.Assert.assertEquals;
2425
import static org.junit.Assert.assertTrue;
2526

27+
2628
/**
2729
* Unit test for {@link Transloadit} class. Api-Responses are simulated by mocking the server's response.
2830
*/
@@ -235,7 +237,7 @@ public void getBill() throws LocalOperationException, RequestException, IOExcept
235237
* {@link Transloadit#setQualifiedErrorsForRetry(ArrayList)} are working as supposed.
236238
*/
237239
@Test
238-
public void getAndsetqualifiedErrorsForRetry() {
240+
public void getAndSetqualifiedErrorsForRetry() {
239241
ArrayList<String> exceptionTemplate = new ArrayList<String>();
240242
exceptionTemplate.add("java.net.SocketTimeoutException");
241243
exceptionTemplate.add("Socket.blah.Exception");
@@ -258,7 +260,7 @@ public void getAndsetqualifiedErrorsForRetry() {
258260
* @throws LocalOperationException
259261
*/
260262
@Test
261-
public void getAndsetTimeoutRetry() throws LocalOperationException {
263+
public void getAndSetTimeoutRetry() throws LocalOperationException {
262264
assertEquals(0, transloadit.getRetryDelay());
263265
transloadit.setRetryDelay(5);
264266
assertEquals(5, transloadit.getRetryDelay());
@@ -271,5 +273,17 @@ public void getAndsetTimeoutRetry() throws LocalOperationException {
271273
assertTrue(exception instanceof LocalOperationException);
272274

273275
}
276+
277+
/**
278+
* Tests if the version Info is obtained correctly with {@link Transloadit#loadVersionInfo()}.
279+
*/
280+
@Test
281+
public void loadVersionInfo() {
282+
String info = transloadit.loadVersionInfo();
283+
Pattern versionPattern = Pattern.compile(
284+
"^[a-z-]*[:]([0-9]+)\\.([0-9]+)\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
285+
Matcher matcher = versionPattern.matcher(info);
286+
assertTrue(matcher.find());
287+
}
274288
}
275289

0 commit comments

Comments
 (0)
Please sign in to comment.