Java API Client
Glean's Java API client provides enterprise-ready integration for Java applications, with support for Spring Boot and traditional enterprise patterns.
glean-api-client
Official Java client for Glean's Client API
Installation
- Maven
- Gradle
<dependency>
<groupId>com.glean.api-client</groupId>
<artifactId>glean-api-client</artifactId>
<version>0.x.x</version>
</dependency>
implementation 'com.glean.api-client:glean-api-client:0.x.x'
Quick Start
import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.*;
import java.util.List;
public class GleanExample {
public static void main(String[] args) {
Glean client = Glean.builder()
.apiToken(System.getenv("GLEAN_API_TOKEN"))
.instance(System.getenv("GLEAN_INSTANCE"))
.build();
var response = client.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text("What are our company values?")
.build()))
.build()))
.build())
.call();
if (response.chatResponse().isPresent()) {
System.out.println(response.chatResponse().get().text());
}
}
}
Core Features
Chat API
public class ChatService {
private final Glean client;
public ChatService(String apiToken, String instance) {
this.client = Glean.builder()
.apiToken(apiToken)
.instance(instance)
.build();
}
public String sendMessage(String message) {
var response = client.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text(message)
.build()))
.build()))
.build())
.call();
return response.chatResponse()
.map(ChatResponse::text)
.orElse("No response");
}
}
Search API
public class SearchService {
private final Glean client;
public SearchService(String apiToken, String instance) {
this.client = Glean.builder()
.apiToken(apiToken)
.instance(instance)
.build();
}
public List<SearchResult> search(String query) {
var response = client.client().search().search()
.searchRequest(SearchRequest.builder()
.query(query)
.pageSize(10)
.build())
.call();
return response.searchResponse()
.map(SearchResponse::results)
.orElse(List.of());
}
}
Spring Boot Integration
Configuration
@Configuration
@ConfigurationProperties(prefix = "glean")
public class GleanConfig {
private String apiToken;
private String instance;
// Getters and setters
public String getApiToken() { return apiToken; }
public void setApiToken(String apiToken) { this.apiToken = apiToken; }
public String getInstance() { return instance; }
public void setInstance(String instance) { this.instance = instance; }
}
@Configuration
@EnableConfigurationProperties(GleanConfig.class)
public class GleanAutoConfiguration {
@Bean
public Glean gleanClient(GleanConfig config) {
return Glean.builder()
.apiToken(config.getApiToken())
.instance(config.getInstance())
.build();
}
}
REST Controller
@RestController
@RequestMapping("/api")
public class GleanController {
private final Glean gleanClient;
public GleanController(Glean gleanClient) {
this.gleanClient = gleanClient;
}
@PostMapping("/chat")
public ResponseEntity<String> chat(@RequestBody Map<String, String> request) {
try {
String message = request.get("message");
var response = gleanClient.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text(message)
.build()))
.build()))
.build())
.call();
String responseText = response.chatResponse()
.map(ChatResponse::text)
.orElse("No response");
return ResponseEntity.ok(responseText);
} catch (Exception e) {
return ResponseEntity.status(500).body("Error: " + e.getMessage());
}
}
}
Authentication
User-Scoped Tokens (Recommended)
Glean client = Glean.builder()
.apiToken("your-user-token")
.instance("your-company")
.build();
Configuration Properties
# application.yml
glean:
api-token: ${GLEAN_API_TOKEN}
instance: ${GLEAN_INSTANCE}
Error Handling
public String safeChat(Glean client, String message) {
try {
var response = client.client().chat().create()
.chatRequest(ChatRequest.builder()
.messages(List.of(
ChatMessage.builder()
.fragments(List.of(
ChatMessageFragment.builder()
.text(message)
.build()))
.build()))
.build())
.call();
return response.chatResponse()
.map(ChatResponse::text)
.orElse("No response");
} catch (Exception e) {
System.err.println("API error: " + e.getMessage());
return "Sorry, I couldn't process your request.";
}
}
Testing
JUnit 5 with Mockito
@ExtendWith(MockitoExtension.class)
class ChatServiceTest {
@Mock
private Glean gleanClient;
@InjectMocks
private ChatService chatService;
@Test
void shouldSendMessageSuccessfully() {
String message = "Test message";
String expectedResponse = "Test response";
// Mock setup and test implementation
assertNotNull(expectedResponse);
}
}