Skip to content

Commit 866435a

Browse files
committed
Add support for authorization headers, closes #26
1 parent 0151f4e commit 866435a

13 files changed

+228
-46
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Jitpack is the recommended way to integrate txtai with Java. See [this link](htt
3232
The following is an example adding txtai to a project's `build.gradle` file. The same attributes can be ported to other build systems per the JitPack link above.
3333

3434
```gradle
35-
implementation 'com.github.neuml:txtai.java:v6.2.0'
35+
implementation 'com.github.neuml:txtai.java:v6.3.0'
3636
```
3737

3838
txtai can also be manually built from GitHub.

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ java {
1717

1818
jar {
1919
archiveBaseName = "txtai"
20-
archiveVersion = "6.2.0"
20+
archiveVersion = "6.3.0"
2121
}

src/main/java/txtai/API.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,21 @@ public IndexResult(int id, double score) {
3030
/**
3131
* Creates a new Retrofit API instance.
3232
*
33-
* @param url base url
33+
* @param url API url
34+
* @param token API token
3435
* @param type service interface class
3536
* @return instance of type
3637
*/
37-
public static <T> T create(String url, Class<T> type) {
38+
public static <T> T create(String url, String token, Class<T> type) {
39+
// Default url and token to environment variables, if empty
40+
url = url != null ? url : System.getenv("TXTAI_API_URL");
41+
token = token != null ? token : System.getenv("TXTAI_API_TOKEN");
42+
3843
// Configure API instance
3944
Retrofit retrofit =
4045
new Retrofit.Builder()
4146
.baseUrl(url)
42-
.client(API.client())
47+
.client(API.client(token))
4348
.addConverterFactory(GsonConverterFactory.create())
4449
.build();
4550

@@ -51,14 +56,21 @@ public static <T> T create(String url, Class<T> type) {
5156
* Builds a custom http client that raises an Exception when calls are not
5257
* successful.
5358
*
59+
* @param token API token
5460
* @return client instance
5561
*/
56-
public static OkHttpClient client() {
62+
public static OkHttpClient client(String token) {
5763
return new OkHttpClient.Builder()
5864
.addInterceptor(new Interceptor() {
5965
@Override
6066
public Response intercept(Chain chain) throws IOException {
6167
Request request = chain.request();
68+
69+
// Add authorization header
70+
if (token != null) {
71+
request = request.newBuilder().addHeader("Authorization", "Bearer " + token).build();
72+
}
73+
6274
Response response = chain.proceed(request);
6375

6476
if (!response.isSuccessful()) {

src/main/java/txtai/Embeddings.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
@SuppressWarnings("rawtypes")
1919
public class Embeddings {
20-
private String url;
2120
private Remote api;
2221

2322
/**
@@ -94,15 +93,33 @@ public SearchResult(String id, double score) {
9493
}
9594
}
9695

96+
/**
97+
* Creates an Embeddings instance.
98+
*/
99+
public Embeddings() {
100+
// Create API instance
101+
this.api = API.create(null, null, Remote.class);
102+
}
103+
97104
/**
98105
* Creates an Embeddings instance.
99106
*
100-
* @param url base url of txtai API
107+
* @param url API url
101108
*/
102109
public Embeddings(String url) {
103110
// Create API instance
104-
this.url = url;
105-
this.api = API.create(this.url, Remote.class);
111+
this.api = API.create(url, null, Remote.class);
112+
}
113+
114+
/**
115+
* Creates an Embeddings instance.
116+
*
117+
* @param url API url
118+
* @param token API token
119+
*/
120+
public Embeddings(String url, String token) {
121+
// Create API instance
122+
this.api = API.create(url, token, Remote.class);
106123
}
107124

108125
/**

src/main/java/txtai/Extractor.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
@SuppressWarnings("rawtypes")
1515
public class Extractor {
16-
private String url;
1716
private Remote api;
1817

1918
/**
@@ -75,15 +74,33 @@ public String toString() {
7574
}
7675
}
7776

77+
/**
78+
* Creates an Extractor instance.
79+
*/
80+
public Extractor() {
81+
// Create API instance
82+
this.api = API.create(null, null, Remote.class);
83+
}
84+
7885
/**
7986
* Creates an Extractor instance.
8087
*
81-
* @param url base url of txtai API
88+
* @param url API url
8289
*/
8390
public Extractor(String url) {
8491
// Create API instance
85-
this.url = url;
86-
this.api = API.create(this.url, Remote.class);
92+
this.api = API.create(url, null, Remote.class);
93+
}
94+
95+
/**
96+
* Creates an Extractor instance.
97+
*
98+
* @param url API url
99+
* @param token API token
100+
*/
101+
public Extractor(String url, String token) {
102+
// Create API instance
103+
this.api = API.create(url, token, Remote.class);
87104
}
88105

89106
/**

src/main/java/txtai/Labels.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
@SuppressWarnings("rawtypes")
1717
public class Labels {
18-
private String url;
1918
private Remote api;
2019

2120
/**
@@ -29,15 +28,33 @@ public interface Remote {
2928
Call<List<List<IndexResult>>> batchlabel(@Body HashMap params);
3029
}
3130

31+
/**
32+
* Creates a Labels instance.
33+
*/
34+
public Labels() {
35+
// Create API instance
36+
this.api = API.create(null, null, Remote.class);
37+
}
38+
3239
/**
3340
* Creates a Labels instance.
3441
*
35-
* @param url base url of txtai API
42+
* @param url API url
3643
*/
3744
public Labels(String url) {
3845
// Create API instance
39-
this.url = url;
40-
this.api = API.create(this.url, Remote.class);
46+
this.api = API.create(url, null, Remote.class);
47+
}
48+
49+
/**
50+
* Creates a Labels instance.
51+
*
52+
* @param url API url
53+
* @param token API token
54+
*/
55+
public Labels(String url, String token) {
56+
// Create API instance
57+
this.api = API.create(url, token, Remote.class);
4158
}
4259

4360
/**

src/main/java/txtai/Segmentation.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
@SuppressWarnings("rawtypes")
1717
public class Segmentation {
18-
private String url;
1918
private Remote api;
2019

2120
/**
@@ -29,15 +28,33 @@ public interface Remote {
2928
Call<List<Object>> batchsegment(@Body HashMap params);
3029
}
3130

31+
/**
32+
* Creates a Segmentation instance.
33+
*/
34+
public Segmentation() {
35+
// Create API instance
36+
this.api = API.create(null, null, Remote.class);
37+
}
38+
3239
/**
3340
* Creates a Segmentation instance.
3441
*
35-
* @param url base url of txtai API
42+
* @param url API url
3643
*/
3744
public Segmentation(String url) {
3845
// Create API instance
39-
this.url = url;
40-
this.api = API.create(this.url, Remote.class);
46+
this.api = API.create(url, null, Remote.class);
47+
}
48+
49+
/**
50+
* Creates a Segmentation instance.
51+
*
52+
* @param url API url
53+
* @param token API token
54+
*/
55+
public Segmentation(String url, String token) {
56+
// Create API instance
57+
this.api = API.create(url, token, Remote.class);
4158
}
4259

4360
/**

src/main/java/txtai/Similarity.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
@SuppressWarnings("rawtypes")
1717
public class Similarity {
18-
private String url;
1918
private Remote api;
2019

2120
/**
@@ -29,15 +28,33 @@ public interface Remote {
2928
Call<List<List<IndexResult>>> batchsimilarity(@Body HashMap params);
3029
}
3130

31+
/**
32+
* Creates a Similarity instance.
33+
*/
34+
public Similarity() {
35+
// Create API instance
36+
this.api = API.create(null, null, Remote.class);
37+
}
38+
3239
/**
3340
* Creates a Similarity instance.
3441
*
35-
* @param url base url of txtai API
42+
* @param url API url
3643
*/
3744
public Similarity(String url) {
3845
// Create API instance
39-
this.url = url;
40-
this.api = API.create(this.url, Remote.class);
46+
this.api = API.create(url, null, Remote.class);
47+
}
48+
49+
/**
50+
* Creates a Similarity instance.
51+
*
52+
* @param url API url
53+
* @param token API token
54+
*/
55+
public Similarity(String url, String token) {
56+
// Create API instance
57+
this.api = API.create(url, token, Remote.class);
4158
}
4259

4360
/**

src/main/java/txtai/Summary.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
@SuppressWarnings("rawtypes")
1717
public class Summary {
18-
private String url;
1918
private Remote api;
2019

2120
/**
@@ -29,15 +28,33 @@ public interface Remote {
2928
Call<List<String>> batchsummary(@Body HashMap params);
3029
}
3130

31+
/**
32+
* Creates a Summary instance.
33+
*/
34+
public Summary() {
35+
// Create API instance
36+
this.api = API.create(null, null, Remote.class);
37+
}
38+
3239
/**
3340
* Creates a Summary instance.
3441
*
35-
* @param url base url of txtai API
42+
* @param url API url
3643
*/
3744
public Summary(String url) {
3845
// Create API instance
39-
this.url = url;
40-
this.api = API.create(this.url, Remote.class);
46+
this.api = API.create(url, null, Remote.class);
47+
}
48+
49+
/**
50+
* Creates a Summary instance.
51+
*
52+
* @param url API url
53+
* @param token API token
54+
*/
55+
public Summary(String url, String token) {
56+
// Create API instance
57+
this.api = API.create(url, token, Remote.class);
4158
}
4259

4360
/**

src/main/java/txtai/Textractor.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
@SuppressWarnings("rawtypes")
1717
public class Textractor {
18-
private String url;
1918
private Remote api;
2019

2120
/**
@@ -29,15 +28,33 @@ public interface Remote {
2928
Call<List<Object>> batchtextract(@Body HashMap params);
3029
}
3130

31+
/**
32+
* Creates a Textractor instance.
33+
*/
34+
public Textractor() {
35+
// Create API instance
36+
this.api = API.create(null, null, Remote.class);
37+
}
38+
3239
/**
3340
* Creates a Textractor instance.
3441
*
35-
* @param url base url of txtai API
42+
* @param url API url
3643
*/
3744
public Textractor(String url) {
3845
// Create API instance
39-
this.url = url;
40-
this.api = API.create(this.url, Remote.class);
46+
this.api = API.create(url, null, Remote.class);
47+
}
48+
49+
/**
50+
* Creates a Textractor instance.
51+
*
52+
* @param url API url
53+
* @param token API token
54+
*/
55+
public Textractor(String url, String token) {
56+
// Create API instance
57+
this.api = API.create(url, token, Remote.class);
4158
}
4259

4360
/**

0 commit comments

Comments
 (0)