Skip to content

Commit a76282f

Browse files
authored
Merge pull request #14 from detectlanguage/v2
V2 SDK
2 parents 638066c + 4f8d461 commit a76282f

20 files changed

+157
-204
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ jobs:
55
runs-on: ubuntu-latest
66
strategy:
77
matrix:
8-
java-version: [ 11, 17 ]
8+
java-version: [ 11, 17, 21, 24 ]
99
steps:
10-
- uses: actions/checkout@v3
10+
- uses: actions/checkout@v4
1111
- name: Set up JDK ${{ matrix.java-version }}
12-
uses: actions/setup-java@v3
12+
uses: actions/setup-java@v4
1313
with:
1414
java-version: ${{ matrix.java-version }}
15-
distribution: 'adopt'
15+
distribution: 'temurin'
1616
cache: 'maven'
1717
- name: Build with Maven
1818
env:
@@ -23,9 +23,9 @@ jobs:
2323
runs-on: ubuntu-latest
2424
strategy:
2525
matrix:
26-
java-release: [ 17 ]
26+
java-release: [ 21, 24 ]
2727
steps:
28-
- uses: actions/checkout@v3
28+
- uses: actions/checkout@v4
2929
- name: Set up JDK ${{ matrix.java-release }}
3030
uses: oracle-actions/setup-java@v1
3131
with:

.github/workflows/publish.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# https://medium.com/@jtbsorensen/publish-your-artifact-to-the-maven-central-repository-using-github-actions-15d3b5d9ce88
2+
name: Publish package to the Maven Central Repository
3+
on:
4+
release:
5+
types: [published]
6+
jobs:
7+
publish:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Set up Maven Central Repository
12+
uses: actions/setup-java@v4
13+
with:
14+
java-version: '11'
15+
distribution: 'temurin'
16+
server-id: ossrh
17+
server-username: MAVEN_USERNAME
18+
server-password: MAVEN_PASSWORD
19+
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
20+
gpg-passphrase: GPG_PASSPHRASE
21+
- name: Publish package
22+
run: mvn -P release --batch-mode deploy -DskipTests
23+
env:
24+
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
25+
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
26+
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# linux
22
*~
33

4+
.env.local
5+
46
# java
57
*.class
8+
.gradle
69

710
# packages
811
*.jar
@@ -24,4 +27,4 @@ target
2427

2528
# idea
2629
.idea
27-
*.iml
30+
*.iml

README.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Add this dependency to your `pom.xml`:
1616
<dependency>
1717
<groupId>com.detectlanguage</groupId>
1818
<artifactId>detectlanguage</artifactId>
19-
<version>1.1.0</version>
19+
<version>2.0.0</version>
2020
</dependency>
2121
```
2222

@@ -30,7 +30,7 @@ repositories {
3030
}
3131
3232
dependencies {
33-
compile 'com.detectlanguage:detectlanguage:1.1.0'
33+
compile 'com.detectlanguage:detectlanguage:2.0.0'
3434
}
3535
```
3636

@@ -46,9 +46,6 @@ Before using Detect Language API client you have to setup your personal **API ke
4646

4747
```java
4848
DetectLanguage.apiKey = "YOURAPIKEY";
49-
50-
// Enable secure mode (SSL) if passing sensitive information
51-
// DetectLanguage.ssl = true;
5249
```
5350

5451
### Language detection
@@ -59,14 +56,13 @@ List<Result> results = DetectLanguage.detect("Hello world");
5956
Result result = results.get(0);
6057

6158
System.out.println("Language: " + result.language);
62-
System.out.println("Is reliable: " + result.isReliable);
63-
System.out.println("Confidence: " + result.confidence);
59+
System.out.println("Score: " + result.score);
6460
```
6561

66-
### Simple detection
62+
### Language code detection
6763

6864
```java
69-
String language = DetectLanguage.simpleDetect("Hello world");
65+
String language = DetectLanguage.detectCode("Hello world");
7066
```
7167

7268
### Batch detection
@@ -80,6 +76,18 @@ String[] texts = {
8076
List<List<Result>> results = DetectLanguage.detect(texts);
8177
```
8278

79+
### Getting your account status
80+
81+
```java
82+
AccountStatusResponse accountStatus = DetectLanguage.getAccountStatus();
83+
```
84+
85+
### Getting list supported languages
86+
87+
```java
88+
LanguageInfo[] languages = DetectLanguage.getLanguages();
89+
```
90+
8391
## Requirements
8492

8593
- [gson](http://code.google.com/p/google-gson/)
@@ -113,13 +121,3 @@ Please use appropriately tagged github [issues](https://github.com/detectlanguag
113121
### Release
114122

115123
Done using the [Sonatype Nexus UI](https://oss.sonatype.org/).
116-
117-
## Contributing
118-
119-
1. Fork it
120-
2. Create your feature branch (`git checkout -b my-new-feature`)
121-
3. Write your code **and tests**
122-
4. Ensure all [tests](#testing) still pass
123-
5. Commit your changes (`git commit -am 'Add some feature'`)
124-
6. Push to the branch (`git push origin my-new-feature`)
125-
7. Create new pull request

build.gradle

Lines changed: 0 additions & 32 deletions
This file was deleted.

mise.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tools]
2+
java = "openjdk-21"
3+
maven = "3"
4+
5+
[env]
6+
_.file = ".env.local"
7+
8+
[tasks]
9+
test = "mvn test"

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@
9696
<plugin>
9797
<groupId>org.apache.maven.plugins</groupId>
9898
<artifactId>maven-compiler-plugin</artifactId>
99-
<version>2.3.2</version>
99+
<version>3.11.0</version>
100100
<configuration>
101-
<source>1.7</source>
102-
<target>1.7</target>
101+
<source>8</source>
102+
<target>8</target>
103103
</configuration>
104104
</plugin>
105105
<plugin>

src/main/java/com/detectlanguage/Client.java

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,45 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020
import java.util.Scanner;
21+
import java.lang.reflect.Type;
2122

2223
public class Client {
23-
24-
public static final String CHARSET = "UTF-8";
25-
2624
private static final String AGENT = "detectlanguage-java";
25+
private static final String CHARSET = "UTF-8";
2726

2827
public Client() {
2928
}
3029

31-
public <T> T execute(String method, Map<String, Object> params,
32-
Class<T> responseClass) throws APIError {
33-
URL url = buildUrl(method);
34-
String query = buildQuery(params);
30+
public <T> T get(String path, Type responseType) throws APIError {
31+
return execute("GET", path, null, null, responseType);
32+
}
33+
34+
public <T> T post(String path, String payload, Type responseType) throws APIError {
35+
return execute("POST", path, null, payload, responseType);
36+
}
37+
38+
private <T> T execute(String method, String path, Map<String, Object> params,
39+
String payload, Type responseType) throws APIError {
40+
URL url = buildUrl(path, params);
3541

3642
try {
37-
HttpURLConnection conn = createPostConnection(url, query);
43+
HttpURLConnection conn = createConnection(url);
44+
45+
conn.setDoOutput(true);
46+
conn.setRequestMethod(method);
47+
conn.setRequestProperty("Content-Type", "application/json");
48+
49+
if (payload != null) {
50+
OutputStream output = null;
51+
try {
52+
output = conn.getOutputStream();
53+
output.write(payload.getBytes(CHARSET));
54+
} finally {
55+
if (output != null) {
56+
output.close();
57+
}
58+
}
59+
}
3860

3961
try {
4062
// trigger the request
@@ -47,7 +69,7 @@ public <T> T execute(String method, Map<String, Object> params,
4769
body = getResponseBody(conn.getErrorStream());
4870
}
4971

50-
return processResponse(responseClass, body);
72+
return processResponse(responseType, body);
5173
} finally {
5274
conn.disconnect();
5375
}
@@ -56,7 +78,7 @@ public <T> T execute(String method, Map<String, Object> params,
5678
}
5779
}
5880

59-
private <T> T processResponse(Class<T> responseClass, String body)
81+
private <T> T processResponse(Type responseType, String body)
6082
throws APIError {
6183

6284
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
@@ -69,20 +91,15 @@ private <T> T processResponse(Class<T> responseClass, String body)
6991
}
7092

7193
try {
72-
return gson.fromJson(body, responseClass);
94+
return gson.fromJson(body, responseType);
7395
} catch (JsonSyntaxException e) {
7496
throw new APIError("Server error. Invalid response format.", 9999);
7597
}
7698
}
7799

78-
private String getProtocol() {
79-
return DetectLanguage.ssl ? "https" : "http";
80-
}
81-
82100
private URL buildUrl(String path, Map<String, Object> params) {
83101
String url = String.format(
84-
"%s://%s/%s/%s",
85-
getProtocol(),
102+
"https://%s/%s/%s",
86103
DetectLanguage.apiHost,
87104
DetectLanguage.apiVersion,
88105
path);
@@ -98,31 +115,6 @@ private URL buildUrl(String path, Map<String, Object> params) {
98115
}
99116
}
100117

101-
private URL buildUrl(String path) {
102-
return buildUrl(path, null);
103-
}
104-
105-
private HttpURLConnection createPostConnection(
106-
URL url, String query) throws IOException {
107-
HttpURLConnection conn = createConnection(url);
108-
109-
conn.setDoOutput(true);
110-
conn.setRequestMethod("POST");
111-
conn.setRequestProperty("Content-Type", String.format(
112-
"application/x-www-form-urlencoded;charset=%s", CHARSET));
113-
114-
OutputStream output = null;
115-
try {
116-
output = conn.getOutputStream();
117-
output.write(query.getBytes(CHARSET));
118-
} finally {
119-
if (output != null) {
120-
output.close();
121-
}
122-
}
123-
return conn;
124-
}
125-
126118
private HttpURLConnection createConnection(URL url) throws IOException {
127119
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
128120
conn.setConnectTimeout(DetectLanguage.timeout);
@@ -133,7 +125,6 @@ private HttpURLConnection createConnection(URL url) throws IOException {
133125

134126
conn.setRequestProperty("User-Agent", AGENT + '/' + version);
135127
conn.setRequestProperty("Accept", "application/json");
136-
conn.setRequestProperty("Accept-Charset", CHARSET);
137128
conn.setRequestProperty("Authorization", "Bearer " + DetectLanguage.apiKey);
138129

139130
return conn;

0 commit comments

Comments
 (0)