un-inception/clients/java/sync/examples/HttpRequestClient.java
russell@unturf.com 331cba42aa feat: Complete 6 additional SDK implementations with fixes and examples
Go Async SDK (clients/go/async/):
- Fixed case-sensitive language detection bug (.R for R language)
- Created go.mod for module management
- Added comprehensive test suite
- Created 3 examples with expected output comments
- Added README with full documentation

Java Sync SDK (clients/java/sync/):
- RENAMED: Unsandbox.java -> Un.java (matches naming convention)
- Updated class name from Unsandbox to Un
- Created pom.xml for Maven build
- Added 6 examples (simple + SDK client versions)
- Created test suite with JUnit 5
- Added README with API documentation

JavaScript Async SDK (clients/javascript/async/):
- Fixed unused import
- Created package.json with ES module support
- Added 5 examples covering all async patterns
- Created 71 tests (all passing)
- Added comprehensive README

PHP Sync SDK (clients/php/sync/):
- RENAMED: Unsandbox.php -> un.php (matches naming convention)
- Created composer.json with PSR-4 autoloading
- Created phpunit.xml for testing
- Added 4 examples with expected output comments
- Created 54 tests across 4 test files
- Added README with full documentation

Ruby Sync SDK (clients/ruby/sync/):
- Created Gemfile and un.gemspec
- Created Rakefile with test task
- Updated examples to actually use the SDK
- Added 4 examples (hello_world, async_job, language_detection, snapshots)
- Created comprehensive test suite with 30+ tests
- Added README with documentation

Rust Sync SDK (clients/rust/sync/):
- Updated Cargo.toml with example declarations
- Created 4 examples (hello_world, fibonacci, multi_language, async_polling)
- Added comprehensive README with API reference
- All dependencies verified correct

All SDKs verified:
- HMAC-SHA256 authentication implemented
- 4-tier credential system (args > env > ~/.unsandbox > ./accounts.csv)
- Expected output comments for pipeline validation
- Proper error handling
- Language detection support
2026-01-15 17:32:24 -05:00

92 lines
3.3 KiB
Java

/**
* HTTP Request Client example for unsandbox Java SDK - Synchronous Version
*
* This example demonstrates making HTTP requests from within a sandboxed environment.
* Uses semitrusted mode which provides internet access through an egress proxy.
*
* To compile:
* javac -cp ../src HttpRequestClient.java
*
* To run:
* export UNSANDBOX_PUBLIC_KEY="your-public-key"
* export UNSANDBOX_SECRET_KEY="your-secret-key"
* java -cp .:../src HttpRequestClient
*
* Expected output:
* Executing HTTP request in sandbox...
*
* === STDOUT ===
* Status Code: 200
* Response: {"origin": "..."}
*/
import java.util.Map;
public class HttpRequestClient {
public static void main(String[] args) {
// The code to execute - uses requests library (pre-installed in sandbox)
String code = """
import requests
import json
try:
# Make HTTP request to httpbin.org
response = requests.get('https://httpbin.org/ip', timeout=10)
print(f"Status Code: {response.status_code}")
# Parse and display response
data = response.json()
print(f"Response: {json.dumps(data)}")
except requests.RequestException as e:
print(f"Request failed: {e}")
except Exception as e:
print(f"Error: {e}")
""";
try {
// Resolve credentials from environment
String publicKey = System.getenv("UNSANDBOX_PUBLIC_KEY");
String secretKey = System.getenv("UNSANDBOX_SECRET_KEY");
if (publicKey == null || publicKey.isEmpty() ||
secretKey == null || secretKey.isEmpty()) {
System.err.println("Error: UNSANDBOX_PUBLIC_KEY and UNSANDBOX_SECRET_KEY environment variables required");
System.err.println("Run with: export UNSANDBOX_PUBLIC_KEY=your-key UNSANDBOX_SECRET_KEY=your-key");
System.exit(1);
}
// Execute the code
System.out.println("Executing HTTP request in sandbox...");
Map<String, Object> result = Un.executeCode("python", code, publicKey, secretKey);
// Check for errors
String status = (String) result.get("status");
if ("completed".equals(status)) {
System.out.println("\n=== STDOUT ===");
String stdout = (String) result.get("stdout");
if (stdout != null) {
System.out.println(stdout);
}
String stderr = (String) result.get("stderr");
if (stderr != null && !stderr.isEmpty()) {
System.out.println("\n=== STDERR ===");
System.out.println(stderr);
}
} else {
System.out.println("Execution failed with status: " + status);
System.out.println("Error: " + result.getOrDefault("error", "Unknown error"));
System.exit(1);
}
} catch (Un.CredentialsException e) {
System.err.println("Credentials error: " + e.getMessage());
System.exit(1);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}