Skip to content

Commit e7b23b7

Browse files
authored
Add set headers to lara http client (#3)
1 parent 4f8803c commit e7b23b7

6 files changed

Lines changed: 146 additions & 110 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.translated.lara.Version;
88
import com.translated.lara.errors.LaraApiConnectionException;
99
import com.translated.lara.errors.LaraException;
10-
import com.translated.lara.net.json.TextResultTypeAdapter;
10+
import com.translated.lara.net.json.TextResultValueTypeAdapter;
1111
import com.translated.lara.translator.TextResult;
1212

1313
import javax.crypto.Mac;
@@ -36,7 +36,7 @@ public class LaraClient {
3636
}
3737

3838
private final Gson gson = new GsonBuilder()
39-
.registerTypeAdapter(TextResult.class, new TextResultTypeAdapter())
39+
.registerTypeAdapter(TextResult.Value.class, new TextResultValueTypeAdapter())
4040
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
4141
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
4242
.create();
@@ -46,6 +46,7 @@ public class LaraClient {
4646
private final int readTimeout;
4747
private final String accessKeyId;
4848
private final Key signingKey;
49+
private final Map<String, String> extraHeaders = new HashMap<>();
4950

5051
public LaraClient(Credentials credentials) {
5152
this(credentials, new ClientOptions());
@@ -60,6 +61,10 @@ public LaraClient(Credentials credentials, ClientOptions options) {
6061
this.readTimeout = (int) options.getReadTimeoutMs();
6162
}
6263

64+
public void setExtraHeader(String name, String value) {
65+
extraHeaders.put(name, value);
66+
}
67+
6368
public ClientResponse get(String path) throws LaraApiConnectionException {
6469
return get(path, null);
6570
}
@@ -133,6 +138,10 @@ private ClientResponse request(String method, String path, Map<String, Object> p
133138
}
134139
connection.setRequestProperty("Authorization", "Lara " + accessKeyId + ":" + sign(method, path, connection));
135140

141+
// extra headers
142+
for (Map.Entry<String, String> header : extraHeaders.entrySet())
143+
connection.setRequestProperty(header.getKey(), header.getValue());
144+
136145
// http method
137146
connection.setRequestMethod("POST");
138147

src/main/java/com/translated/lara/net/json/TextResultTypeAdapter.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.translated.lara.net.json;
2+
3+
import com.google.gson.*;
4+
import com.translated.lara.translator.TextBlock;
5+
import com.translated.lara.translator.TextResult;
6+
7+
import java.lang.reflect.Type;
8+
9+
public class TextResultValueTypeAdapter implements JsonDeserializer<TextResult.Value> {
10+
11+
@Override
12+
public TextResult.Value deserialize(JsonElement jElement, Type type, JsonDeserializationContext context) throws JsonParseException {
13+
if (jElement != null && !jElement.isJsonNull()) {
14+
if (jElement.isJsonArray()) {
15+
JsonArray jTranslationArray = jElement.getAsJsonArray();
16+
17+
if (jTranslationArray.isEmpty()) {
18+
return new TextResult.Value(new String[0]);
19+
} else {
20+
if (jTranslationArray.get(0).isJsonObject()) {
21+
TextBlock[] blocks = context.deserialize(jTranslationArray, TextBlock[].class);
22+
return new TextResult.Value(blocks);
23+
} else {
24+
String[] translations = context.deserialize(jTranslationArray, String[].class);
25+
return new TextResult.Value(translations);
26+
}
27+
}
28+
} else {
29+
return new TextResult.Value(jElement.getAsString());
30+
}
31+
} else {
32+
return new TextResult.Value((String) null);
33+
}
34+
35+
}
36+
}

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

Lines changed: 96 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,122 @@
77

88
public class TextResult {
99

10+
public static class Value {
11+
12+
private final String translation;
13+
private final List<String> translations;
14+
private final List<TextBlock> translationBlocks;
15+
16+
public Value(String translation) {
17+
this.translation = translation;
18+
this.translations = null;
19+
this.translationBlocks = null;
20+
}
21+
22+
public Value(String[] translation) {
23+
this.translation = null;
24+
this.translations = Collections.unmodifiableList(Arrays.asList(translation));
25+
this.translationBlocks = null;
26+
}
27+
28+
public Value(TextBlock[] translation) {
29+
this.translation = null;
30+
this.translations = null;
31+
this.translationBlocks = Collections.unmodifiableList(Arrays.asList(translation));
32+
}
33+
34+
public String getTranslation() {
35+
if (translation != null)
36+
return translation;
37+
38+
if (translations != null && !translations.isEmpty()) {
39+
if (translations.size() != 1)
40+
throw new IllegalStateException("Cannot get translation from multiple elements (" + translations.size() + ")");
41+
return translations.get(0);
42+
}
43+
44+
if (translationBlocks != null && !translationBlocks.isEmpty()) {
45+
if (translationBlocks.size() != 1)
46+
throw new IllegalStateException("Cannot get translation from multiple elements (" + translationBlocks.size() + ")");
47+
return translationBlocks.get(0).getText();
48+
}
49+
50+
return null;
51+
}
52+
53+
public List<String> getTranslations() {
54+
if (translations != null)
55+
return translations;
56+
57+
if (translation != null)
58+
return Collections.singletonList(translation);
59+
60+
if (translationBlocks != null && !translationBlocks.isEmpty()) {
61+
List<String> result = new ArrayList<>(translationBlocks.size());
62+
for (TextBlock block : translationBlocks)
63+
result.add(block.getText());
64+
65+
return result;
66+
}
67+
68+
return null;
69+
}
70+
71+
public List<TextBlock> getTranslationBlocks() {
72+
if (translationBlocks != null)
73+
return translationBlocks;
74+
75+
if (translation != null)
76+
return Collections.singletonList(new TextBlock(translation));
77+
78+
if (translations != null && !translations.isEmpty()) {
79+
List<TextBlock> result = new ArrayList<>(translations.size());
80+
for (String translation : translations)
81+
result.add(new TextBlock(translation));
82+
83+
return result;
84+
}
85+
86+
return null;
87+
}
88+
89+
@Override
90+
public String toString() {
91+
if (translation != null) return translation;
92+
if (translations != null) return translations.toString();
93+
if (translationBlocks != null) return translationBlocks.toString();
94+
return null;
95+
}
96+
}
97+
1098
private final String contentType;
1199
private final String sourceLanguage;
12100
private final List<String> adaptedTo;
13101

14-
private final String translation;
15-
private final List<String> translations;
16-
private final List<TextBlock> translationBlocks;
102+
private final Value translation;
17103

18104
public TextResult(String contentType, String sourceLanguage, String translation, String[] adaptedTo) {
19105
this.contentType = contentType;
20106
this.sourceLanguage = sourceLanguage;
21107
this.adaptedTo = adaptedTo == null ? null : Collections.unmodifiableList(Arrays.asList(adaptedTo));
22108

23-
this.translation = translation;
24-
this.translations = null;
25-
this.translationBlocks = null;
109+
this.translation = new Value(translation);
26110
}
27111

28112
public TextResult(String contentType, String sourceLanguage, String[] translation, String[] adaptedTo) {
29113
this.contentType = contentType;
30114
this.sourceLanguage = sourceLanguage;
31115
this.adaptedTo = adaptedTo == null ? null : Collections.unmodifiableList(Arrays.asList(adaptedTo));
32116

33-
this.translation = null;
34-
this.translations = Collections.unmodifiableList(Arrays.asList(translation));
35-
this.translationBlocks = null;
117+
this.translation = new Value(translation);
36118
}
37119

38120
public TextResult(String contentType, String sourceLanguage, TextBlock[] translation, String[] adaptedTo) {
39121
this.contentType = contentType;
40122
this.sourceLanguage = sourceLanguage;
41123
this.adaptedTo = adaptedTo == null ? null : Collections.unmodifiableList(Arrays.asList(adaptedTo));
42124

43-
this.translation = null;
44-
this.translations = null;
45-
this.translationBlocks = Collections.unmodifiableList(Arrays.asList(translation));
125+
this.translation = new Value(translation);
46126
}
47127

48128
public String getContentType() {
@@ -58,66 +138,20 @@ public List<String> getAdaptedTo() {
58138
}
59139

60140
public String getTranslation() {
61-
if (translation != null)
62-
return translation;
63-
64-
if (translations != null && !translations.isEmpty()) {
65-
if (translations.size() != 1)
66-
throw new IllegalStateException("Cannot get translation from multiple elements (" + translations.size() + ")");
67-
return translations.get(0);
68-
}
69-
70-
if (translationBlocks != null && !translationBlocks.isEmpty()) {
71-
if (translationBlocks.size() != 1)
72-
throw new IllegalStateException("Cannot get translation from multiple elements (" + translationBlocks.size() + ")");
73-
return translationBlocks.get(0).getText();
74-
}
75-
76-
return null;
141+
return translation.getTranslation();
77142
}
78143

79144
public List<String> getTranslations() {
80-
if (translations != null)
81-
return translations;
82-
83-
if (translation != null)
84-
return Collections.singletonList(translation);
85-
86-
if (translationBlocks != null && !translationBlocks.isEmpty()) {
87-
List<String> result = new ArrayList<>(translationBlocks.size());
88-
for (TextBlock block : translationBlocks)
89-
result.add(block.getText());
90-
91-
return result;
92-
}
93-
94-
return null;
145+
return translation.getTranslations();
95146
}
96147

97148
public List<TextBlock> getTranslationBlocks() {
98-
if (translationBlocks != null)
99-
return translationBlocks;
100-
101-
if (translation != null)
102-
return Collections.singletonList(new TextBlock(translation));
103-
104-
if (translations != null && !translations.isEmpty()) {
105-
List<TextBlock> result = new ArrayList<>(translations.size());
106-
for (String translation : translations)
107-
result.add(new TextBlock(translation));
108-
109-
return result;
110-
}
111-
112-
return null;
149+
return translation.getTranslationBlocks();
113150
}
114151

115152
@Override
116153
public String toString() {
117-
if (translation != null) return translation;
118-
if (translations != null) return translations.toString();
119-
if (translationBlocks != null) return translationBlocks.toString();
120-
return null;
154+
return this.translation.toString();
121155
}
122156

123157
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public TranslateOptions setCacheTTL(Integer cacheTTL) {
115115
return this;
116116
}
117117

118-
HttpParams<Object> toParams() {
118+
public HttpParams<Object> toParams() {
119119
HttpParams<Object> params = new HttpParams<>();
120120
params.set("source_hint", sourceHint);
121121
params.set("adapt_to", adaptTo);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class Translator {
1212

13-
private final LaraClient client;
13+
protected final LaraClient client;
1414
public final Memories memories;
1515

1616
public Translator(Credentials credentials) {
@@ -66,7 +66,7 @@ public TextResult translateBlocks(TextBlock[] text, String source, String target
6666
return translateAny(text, source, target, options);
6767
}
6868

69-
private TextResult translateAny(Object text, String source, String target, TranslateOptions options) throws LaraException {
69+
protected TextResult translateAny(Object text, String source, String target, TranslateOptions options) throws LaraException {
7070
HttpParams<Object> params = options == null ? new HttpParams<>() : options.toParams();
7171

7272
return client.post("/translate", params

0 commit comments

Comments
 (0)