org.yamcs.client.storage.StorageClient Maven / Gradle / Ivy
package org.yamcs.client.storage;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.yamcs.api.HttpBody;
import org.yamcs.api.MethodHandler;
import org.yamcs.client.base.ResponseObserver;
import org.yamcs.protobuf.BucketInfo;
import org.yamcs.protobuf.BucketsApiClient;
import org.yamcs.protobuf.CreateBucketRequest;
import org.yamcs.protobuf.DeleteBucketRequest;
import org.yamcs.protobuf.GetObjectRequest;
import org.yamcs.protobuf.ListBucketsRequest;
import org.yamcs.protobuf.ListBucketsResponse;
import org.yamcs.protobuf.UploadObjectRequest;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
public class StorageClient {
private BucketsApiClient bucketService;
public StorageClient(MethodHandler handler) {
bucketService = new BucketsApiClient(handler);
}
public CompletableFuture createBucket(String bucketName) {
var request = CreateBucketRequest.newBuilder()
.setName(bucketName)
.build();
var f = new CompletableFuture();
bucketService.createBucket(null, request, new ResponseObserver<>(f));
return f.thenApply(response -> getBucket(response.getName()));
}
public CompletableFuture deleteBucket(String bucketName) {
var request = DeleteBucketRequest.newBuilder()
.setBucketName(bucketName)
.build();
var f = new CompletableFuture();
bucketService.deleteBucket(null, request, new ResponseObserver<>(f));
return f.thenApply(response -> null);
}
public CompletableFuture uploadObject(ObjectId target, byte[] bytes) {
var request = UploadObjectRequest.newBuilder()
.setBucketName(target.getBucket())
.setObjectName(target.getObjectName())
.setData(HttpBody.newBuilder().setData(ByteString.copyFrom(bytes)))
.build();
var f = new CompletableFuture();
bucketService.uploadObject(null, request, new ResponseObserver<>(f));
return f.thenApply(response -> null);
}
public CompletableFuture downloadObject(ObjectId source) {
var request = GetObjectRequest.newBuilder()
.setBucketName(source.getBucket())
.setObjectName(source.getObjectName())
.build();
var f = new CompletableFuture();
bucketService.getObject(null, request, new ResponseObserver<>(f));
return f.thenApply(response -> response.getData().toByteArray());
}
public CompletableFuture> listBuckets() {
var request = ListBucketsRequest.newBuilder().build();
var f = new CompletableFuture();
bucketService.listBuckets(null, request, new ResponseObserver<>(f));
return f.thenApply(response -> response.getBucketsList());
}
public Bucket getBucket(String bucket) {
return new Bucket(bucketService, bucket);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy