Skip to content

Commit 4baa4ec

Browse files
authored
Feature: support incognito mode (#3)
* feature: add optional headers parameter to HTTP client methods * feature: support noTrace option for text and document translation
1 parent dbab481 commit 4baa4ec

6 files changed

Lines changed: 82 additions & 15 deletions

File tree

src/main/java/com/translated/lara/net/LaraClient.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,46 +69,62 @@ public void setExtraHeader(String name, String value) {
6969
}
7070

7171
public ClientResponse get(String path) throws LaraApiConnectionException {
72-
return get(path, null);
72+
return get(path, null, null);
7373
}
7474

7575
public ClientResponse get(String path, Map<String, Object> params) throws LaraApiConnectionException {
76-
return request("GET", path, params, null);
76+
return get(path, params, null);
77+
}
78+
79+
public ClientResponse get(String path, Map<String, Object> params, Map<String, String> headers) throws LaraApiConnectionException {
80+
return request("GET", path, params, null, headers);
7781
}
7882

7983
public ClientResponse delete(String path) throws LaraException {
80-
return delete(path, null);
84+
return delete(path, null, null);
85+
}
86+
87+
public ClientResponse delete(String path, Map<String, Object> params) throws LaraException {
88+
return delete(path, params, null);
8189
}
8290

83-
public ClientResponse delete(String path, Map<String, Object> params) throws LaraApiConnectionException {
84-
return request("DELETE", path, params, null);
91+
public ClientResponse delete(String path, Map<String, Object> params, Map<String, String> headers) throws LaraApiConnectionException {
92+
return request("DELETE", path, params, null, headers);
8593
}
8694

8795
public ClientResponse post(String path) throws LaraException {
88-
return post(path, null, null);
96+
return post(path, null, null, null);
8997
}
9098

91-
public ClientResponse post(String path, Map<String, Object> params) throws LaraApiConnectionException {
92-
return post(path, params, null);
99+
public ClientResponse post(String path, Map<String, Object> params) throws LaraException {
100+
return post(path, params, null, null);
93101
}
94102

95103
public ClientResponse post(String path, Map<String, Object> params, Map<String, File> files) throws LaraApiConnectionException {
96-
return request("POST", path, params, files);
104+
return post(path, params, files, null);
105+
}
106+
107+
public ClientResponse post(String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraApiConnectionException {
108+
return request("POST", path, params, files, headers);
97109
}
98110

99111
public ClientResponse put(String path) throws LaraException {
100-
return put(path, null, null);
112+
return put(path, null, null, null);
101113
}
102114

103115
public ClientResponse put(String path, Map<String, Object> params) throws LaraApiConnectionException {
104-
return put(path, params, null);
116+
return put(path, params, null, null);
105117
}
106118

107119
public ClientResponse put(String path, Map<String, Object> params, Map<String, File> files) throws LaraApiConnectionException {
108-
return request("PUT", path, params, files);
120+
return put(path, params, files, null);
109121
}
110122

111-
private ClientResponse request(String method, String path, Map<String, Object> params, Map<String, File> files) throws LaraApiConnectionException {
123+
public ClientResponse put(String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraApiConnectionException {
124+
return request("PUT", path, params, files, headers);
125+
}
126+
127+
private ClientResponse request(String method, String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraApiConnectionException {
112128
path = normalizePath(path);
113129
params = prune(params);
114130
files = prune(files);
@@ -145,6 +161,12 @@ private ClientResponse request(String method, String path, Map<String, Object> p
145161
for (Map.Entry<String, String> header : extraHeaders.entrySet())
146162
connection.setRequestProperty(header.getKey(), header.getValue());
147163

164+
// headers
165+
if (headers != null) {
166+
for (Map.Entry<String, String> header : headers.entrySet())
167+
connection.setRequestProperty(header.getKey(), header.getValue());
168+
}
169+
148170
// http method
149171
connection.setRequestMethod("POST");
150172

src/main/java/com/translated/lara/translator/DocumentTranslateOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class DocumentTranslateOptions {
88

99
private String[] adaptTo = null;
1010
private String outputFormat = null;
11+
private Boolean noTrace = null;
1112

1213
public String[] getAdaptTo() {
1314
return adaptTo;
@@ -32,6 +33,15 @@ public DocumentTranslateOptions setOutputFormat(String outputFormat) {
3233
return this;
3334
}
3435

36+
public Boolean getNoTrace() {
37+
return noTrace;
38+
}
39+
40+
public DocumentTranslateOptions setNoTrace(Boolean noTrace) {
41+
this.noTrace = noTrace;
42+
return this;
43+
}
44+
3545
public HttpParams<Object> toParams() {
3646
HttpParams<Object> params = new HttpParams<>();
3747
params.set("adapt_to", adaptTo);

src/main/java/com/translated/lara/translator/DocumentUploadOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class DocumentUploadOptions {
88

99
private String[] adaptTo = null;
10+
private Boolean noTrace = null;
1011

1112
public String[] getAdaptTo() {
1213
return adaptTo;
@@ -22,6 +23,15 @@ public DocumentUploadOptions setAdaptTo(String... adaptTo) {
2223
return this;
2324
}
2425

26+
public Boolean getNoTrace() {
27+
return noTrace;
28+
}
29+
30+
public DocumentUploadOptions setNoTrace(Boolean noTrace) {
31+
this.noTrace = noTrace;
32+
return this;
33+
}
34+
2535
public HttpParams<Object> toParams() {
2636
HttpParams<Object> params = new HttpParams<>();
2737
params.set("adapt_to", adaptTo);

src/main/java/com/translated/lara/translator/Documents.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public Document upload(File file, String source, String target, DocumentUploadOp
5555
.set("source", source)
5656
.set("s3key", s3key);
5757

58-
return client.post("/documents", params.build()).as(Document.class);
58+
Map<String, String> headers = new HashMap<>();
59+
if (options != null && options.getNoTrace()) {
60+
headers.put("X-No-Trace", "true");
61+
}
62+
63+
return client.post("/documents", params.build(), null, headers).as(Document.class);
5964
}
6065

6166
/***
@@ -112,6 +117,7 @@ public InputStream translate(File input, String source, String target) throws S3
112117
public InputStream translate(File input, String source, String target, DocumentTranslateOptions options) throws S3Exception, LaraException, InterruptedException {
113118
DocumentUploadOptions uploadOptions = options != null ? new DocumentUploadOptions() : null;
114119
if (options != null && options.getAdaptTo() != null) uploadOptions.setAdaptTo(options.getAdaptTo());
120+
if (options != null && options.getNoTrace()) uploadOptions.setNoTrace(true);
115121

116122
Document document = upload(input, source, target, uploadOptions);
117123
document = pollDocumentUntilCompleted(document);

src/main/java/com/translated/lara/translator/TranslateOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum UseCache {
2323
private Priority priority = null;
2424
private UseCache useCache = null;
2525
private Integer cacheTTL = null;
26+
private Boolean noTrace = null;
2627

2728
public String getSourceHint() {
2829
return sourceHint;
@@ -115,6 +116,15 @@ public TranslateOptions setCacheTTL(Integer cacheTTL) {
115116
return this;
116117
}
117118

119+
public Boolean getNoTrace() {
120+
return noTrace;
121+
}
122+
123+
public TranslateOptions setNoTrace(boolean noTrace) {
124+
this.noTrace = noTrace;
125+
return this;
126+
}
127+
118128
public HttpParams<Object> toParams() {
119129
HttpParams<Object> params = new HttpParams<>();
120130
params.set("source_hint", sourceHint);

src/main/java/com/translated/lara/translator/Translator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.translated.lara.net.HttpParams;
77
import com.translated.lara.net.LaraClient;
88

9+
import java.util.HashMap;
910
import java.util.List;
11+
import java.util.Map;
1012

1113
public class Translator {
1214

@@ -71,11 +73,18 @@ public TextResult translateBlocks(TextBlock[] text, String source, String target
7173
protected TextResult translateAny(Object text, String source, String target, TranslateOptions options) throws LaraException {
7274
HttpParams<Object> params = options == null ? new HttpParams<>() : options.toParams();
7375

76+
Map<String, String> headers = new HashMap<>();
77+
if (options != null && options.getNoTrace()) {
78+
headers.put("X-No-Trace", "true");
79+
}
80+
7481
return client.post("/translate", params
7582
.set("source", source)
7683
.set("target", target)
7784
.set("q", text)
78-
.build()
85+
.build(),
86+
null,
87+
headers
7988
).as(TextResult.class);
8089
}
8190

0 commit comments

Comments
 (0)