Java SDK
The Telesoft Healthcare AI Java SDK provides a convenient way to integrate with our API from your Java applications. This SDK handles authentication, request formatting, and response parsing so you can focus on building your healthcare application.
Installation
Add the Telesoft SDK to your Java project using Maven or Gradle:
Maven
<dependency>
<groupId>us.telesoft</groupId>
<artifactId>healthcare-ai</artifactId>
<version>1.2.0</version>
</dependency>
Gradle
implementation 'us.telesoft:healthcare-ai:1.2.0'
Authentication
Initialize the SDK with your API key:
import us.telesoft.healthcareai.TelesoftClient;
// Create a client instance with your API key
TelesoftClient client = new TelesoftClient.Builder()
.apiKey("YOUR_API_KEY")
.build();
⚠️ API Key Security
Never hardcode your API key in your source code. Instead, load it from environment variables or a secure configuration management system.
Basic Usage
Here's a simple example of using the SDK to analyze patient symptoms:
import us.telesoft.healthcareai.TelesoftClient;
import us.telesoft.healthcareai.diagnostics.DiagnosticsRequest;
import us.telesoft.healthcareai.diagnostics.DiagnosticsResponse;
import us.telesoft.healthcareai.models.Patient;
import us.telesoft.healthcareai.models.Symptom;
import java.util.Arrays;
import java.util.List;
public class DiagnosticsExample {
public static void main(String[] args) {
// Initialize the client
TelesoftClient client = new TelesoftClient.Builder()
.apiKey(System.getenv("TELESOFT_API_KEY"))
.build();
// Create a patient with symptoms
Patient patient = new Patient.Builder()
.age(45)
.sex("female")
.build();
List<Symptom> symptoms = Arrays.asList(
new Symptom("cough", "5 days"),
new Symptom("fever", "3 days"),
new Symptom("shortness of breath", "2 days")
);
// Create the diagnostics request
DiagnosticsRequest request = new DiagnosticsRequest.Builder()
.patient(patient)
.symptoms(symptoms)
.includeConfidenceScores(true)
.includeDifferentialDiagnosis(true)
.build();
// Send the request
try {
DiagnosticsResponse response = client.diagnostics().analyze(request);
// Process the response
System.out.println("Primary diagnosis: " + response.getPrimaryDiagnosis().getCondition());
System.out.println("Confidence: " + response.getPrimaryDiagnosis().getConfidenceScore());
System.out.println("\nDifferential diagnoses:");
response.getDifferentialDiagnoses().forEach(diagnosis -> {
System.out.println("- " + diagnosis.getCondition() + " (" +
diagnosis.getConfidenceScore() * 100 + "%)");
});
System.out.println("\nRecommended tests:");
response.getRecommendations().getDiagnosticTests().forEach(test -> {
System.out.println("- " + test.getName() + ": " + test.getRationale());
});
} catch (Exception e) {
System.err.println("Error analyzing symptoms: " + e.getMessage());
}
}
}
Advanced Features
Analyzing Medical Images
Use the SDK to analyze medical images:
import java.io.File;
import us.telesoft.healthcareai.imaging.ImagingRequest;
import us.telesoft.healthcareai.imaging.ImagingResponse;
// Create an imaging request with a chest X-ray
File xrayImage = new File("path/to/chest_xray.dcm");
ImagingRequest request = new ImagingRequest.Builder()
.imagingType("chest-xray")
.image(xrayImage)
.includeAnnotations(true)
.build();
// Send the request
ImagingResponse response = client.imaging().analyze(request);
// Process the findings
response.getFindings().forEach(finding -> {
System.out.println("Finding: " + finding.getDescription());
System.out.println("Confidence: " + finding.getConfidence());
System.out.println("Location: " + finding.getLocation());
});
Custom Configurations
Configure the SDK with advanced options:
import us.telesoft.healthcareai.TelesoftClient;
import us.telesoft.healthcareai.config.ClientConfig;
import us.telesoft.healthcareai.config.RetryPolicy;
// Create a custom configuration
ClientConfig config = new ClientConfig.Builder()
.connectTimeout(30) // 30 seconds connection timeout
.readTimeout(60) // 60 seconds read timeout
.retryPolicy(new RetryPolicy( // Custom retry behavior
3, // Max 3 retries
500, // Start with 500ms delay
2.0, // Double the delay each retry
Arrays.asList(429, 503) // Only retry on these status codes
))
.build();
// Create the client with custom configuration
TelesoftClient client = new TelesoftClient.Builder()
.apiKey(System.getenv("TELESOFT_API_KEY"))
.config(config)
.build();
Asynchronous Requests
Process long-running requests asynchronously:
import java.util.concurrent.CompletableFuture;
import us.telesoft.healthcareai.async.AsyncCallback;
// Create a request as usual
DiagnosticsRequest request = new DiagnosticsRequest.Builder()
// ... request parameters ...
.build();
// Option 1: Use CompletableFuture
CompletableFuture<DiagnosticsResponse> future = client.diagnostics().analyzeAsync(request);
future.thenAccept(response -> {
System.out.println("Analysis complete: " + response.getPrimaryDiagnosis().getCondition());
}).exceptionally(ex -> {
System.err.println("Error in analysis: " + ex.getMessage());
return null;
});
// Option 2: Use callbacks
client.diagnostics().analyzeAsync(request, new AsyncCallback<DiagnosticsResponse>() {
@Override
public void onSuccess(DiagnosticsResponse response) {
System.out.println("Analysis complete: " + response.getPrimaryDiagnosis().getCondition());
}
@Override
public void onFailure(Exception e) {
System.err.println("Error in analysis: " + e.getMessage());
}
});
Handling Errors
The SDK provides structured error handling:
import us.telesoft.healthcareai.exceptions.*;
try {
DiagnosticsResponse response = client.diagnostics().analyze(request);
// Process successful response
} catch (AuthenticationException e) {
// Handle authentication issues
System.err.println("Authentication failed: " + e.getMessage());
} catch (RateLimitException e) {
// Handle rate limit issues
System.err.println("Rate limit exceeded. Retry after " + e.getRetryAfter() + " seconds");
} catch (ValidationException e) {
// Handle invalid input
System.err.println("Input validation failed: " + e.getMessage());
e.getErrors().forEach(error -> {
System.err.println("- " + error.getField() + ": " + error.getMessage());
});
} catch (ServiceException e) {
// Handle server errors
System.err.println("Service error: " + e.getMessage());
} catch (TelesoftException e) {
// Handle any other SDK exceptions
System.err.println("Error: " + e.getMessage());
}
ℹ️ Error Handling Best Practices
For production applications, consider implementing exponential backoff for retries on rate limit errors and timeouts. The SDK's built-in RetryPolicy can handle this automatically.
Using with Spring Boot
Integrating the SDK with Spring Boot:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;
import us.telesoft.healthcareai.TelesoftClient;
@Configuration
public class TelesoftConfig {
@Value("${telesoft.api.key}")
private String apiKey;
@Bean
public TelesoftClient telesoftClient() {
return new TelesoftClient.Builder()
.apiKey(apiKey)
.build();
}
}
// In your service class
@Service
public class DiagnosticsService {
private final TelesoftClient telesoftClient;
@Autowired
public DiagnosticsService(TelesoftClient telesoftClient) {
this.telesoftClient = telesoftClient;
}
public DiagnosticResult analyzeSymptoms(PatientData patientData) {
// Convert your domain model to SDK model
Patient patient = mapToSdkPatient(patientData);
// Create and send request
DiagnosticsRequest request = new DiagnosticsRequest.Builder()
.patient(patient)
.symptoms(mapToSdkSymptoms(patientData.getSymptoms()))
.build();
DiagnosticsResponse response = telesoftClient.diagnostics().analyze(request);
// Map SDK response back to your domain model
return mapToDomainResult(response);
}
// Mapping methods...
}
💡 Pro Tip
For Spring Boot applications, store your API key in the application.yml
file and reference it via @Value
annotations. Be sure to use environment-specific configuration for different deployment environments.