import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.HashMap; // Use HashMap for mutable map
import java.util.stream.Collectors;
public class ListProcessTasks {
public static void main(String[] args) {
String accessToken = System.getenv().getOrDefault("TALLYFY_ACCESS_TOKEN", "YOUR_PERSONAL_ACCESS_TOKEN");
String orgId = System.getenv().getOrDefault("TALLYFY_ORG_ID", "YOUR_ORGANIZATION_ID");
String runId = "PROCESS_RUN_ID"; // ID of the specific process run
String baseUrl = "https://go.tallyfy.com/api/organizations/" + orgId + "/runs/" + runId + "/tasks";
// Optional: Add query parameters
Map<String, String> queryParamsMap = new HashMap<>();
// queryParamsMap.put("status", "incomplete");
queryParamsMap.put("with", "step");
queryParamsMap.put("sort", "-deadline");
String queryParamsString = queryParamsMap.entrySet().stream()
.map(entry -> URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8) + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
.collect(Collectors.joining("&", "?", ""));
String apiUrl = baseUrl + (queryParamsMap.isEmpty() ? "" : queryParamsString);
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
HttpRequest request = HttpRequest.newBuilder()
.header("Authorization", "Bearer " + accessToken)
.header("Accept", "application/json")
.header("X-Tallyfy-Client", "APIClient")
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Successfully listed tasks for process " + runId + ":");
System.out.println(response.body());
// TODO: Consider parsing JSON response using Jackson/Gson
System.err.println("Failed to list tasks for process " + runId + ". Status: " + response.statusCode());
System.err.println("Response Body: " + response.body());
} catch (IOException | InterruptedException e) {
System.err.println("Request failed: " + e.getMessage());
Thread.currentThread().interrupt();
System.err.println("An unexpected error occurred: " + e.getMessage());