Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .spec-sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
06a7c92e23df7c44f8898a2b18abec43aa5c14b8f12ea10e1ce65a94a14a0fb4
e8f81ea407d93977cf0612e9d7e4bbda4b86c4d25de84bf44152b2ff868cde03
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ client.conversations().list(null, null, null, null, null, null, "BUYER_ID", null
// Writes (API key with items:write / conversations:write); the last argument, optional, is the Idempotency-Key.
client.items().create(new ItemCreateRequest().name("Jacket")); // items().update(42, new ItemUpdateRequest()...) patches
client.items().publish(42, new PublishRequest().accountIds(List.of(accountId)));
FileUpload photo = client.files().upload(Files.readAllBytes(Path.of("jacket.jpg")), "jacket.jpg", "image/jpeg"); // registers the photo and PUTs its bytes; files().register only registers it, files().get reads it (status is PENDING until its bytes are stored)
client.items().create(new ItemCreateRequest().name("Jacket").photoIds(List.of(photo.getId())));
client.items().publish(42, new PublishRequest().accountIds(List.of(accountId)).scheduledAt(OffsetDateTime.parse("2026-11-02T10:00:00Z"))); // publish, priceDrop and republish take an optional scheduledAt, at most 30 days ahead; without it they run now
client.conversations().sendMessage(conversationId, new SendMessageRequest().text("Hello!"));

Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/controlresell/sdk/ControlResell.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.controlresell.sdk.generated.api.ConversationsApi;
import com.controlresell.sdk.generated.api.CustomersApi;
import com.controlresell.sdk.generated.api.FavoritesApi;
import com.controlresell.sdk.generated.api.FilesApi;
import com.controlresell.sdk.generated.api.ItemsApi;
import com.controlresell.sdk.generated.api.MeApi;
import com.controlresell.sdk.generated.api.OrdersApi;
Expand All @@ -28,6 +29,9 @@
import com.controlresell.sdk.generated.model.Customer;
import com.controlresell.sdk.generated.model.CustomerPage;
import com.controlresell.sdk.generated.model.Favorite;
import com.controlresell.sdk.generated.model.FileUpload;
import com.controlresell.sdk.generated.model.FileUploadRequest;
import com.controlresell.sdk.generated.model.ModelFile;
import com.controlresell.sdk.generated.model.FavoritePage;
import com.controlresell.sdk.generated.model.Item;
import com.controlresell.sdk.generated.model.ItemCreateRequest;
Expand Down Expand Up @@ -62,7 +66,11 @@
import com.controlresell.sdk.generated.model.WebhookSecretRotateRequest;
import com.controlresell.sdk.generated.model.WebhookTestResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.List;
Expand Down Expand Up @@ -101,6 +109,8 @@ public interface HttpCall<T> {

private final ApiClient apiClient;
private final Retrier retrier;
private final HttpClient storage;
private final Duration timeout;

/** A client for {@code apiKey}, on the production API. */
public ControlResell(String apiKey) {
Expand All @@ -114,6 +124,9 @@ private ControlResell(Builder b) {
}
ObjectMapper mapper = ApiClient.createDefaultObjectMapper();
HttpClient.Builder http = b.httpClient != null ? b.httpClient : ApiClient.createDefaultHttpClientBuilder();
// The bytes of a file go to the signed storage address, without the API key.
this.storage = http.build();
this.timeout = b.timeout;
this.apiClient = new ApiClient(http, mapper, b.baseUrl);
this.apiClient.setReadTimeout(b.timeout);
this.apiClient.setRequestInterceptor(request -> request
Expand Down Expand Up @@ -183,6 +196,9 @@ private <T> T callOnce(HttpCall<T> call) {
/** The other parties of the seller's sales and purchases (needs the {@code customers:read} scope). */
public Customers customers() { return new Customers(); }

/** The photos of the seller's library: register one, send its bytes, read it (needs {@code items:write} / {@code items:read}). */
public Files files() { return new Files(); }

/** The buyers who put the seller's listings in their favorites (needs the {@code items:read} scope). */
public Favorites favorites() { return new Favorites(); }

Expand All @@ -192,6 +208,64 @@ private <T> T callOnce(HttpCall<T> call) {
/** The seller's webhook endpoints, their secrets and deliveries, and the recent events (webhooks:read, webhooks:write). */
public Webhooks webhooks() { return new Webhooks(); }

/** The photos of the seller's library. Put the identifier of a registered file in {@code photoIds} of an item. */
public final class Files {
private final FilesApi api = new FilesApi(apiClient);

private Files() {}

/**
* Registers a file (jpeg, png, webp, heic or heif, at most 20 MiB): its identifier and the address where to
* {@code PUT} its bytes within 30 minutes. {@code idempotencyKey} may be {@code null}.
*/
public FileUpload register(String filename, String contentType, long size, String idempotencyKey) {
String key = key(idempotencyKey);
FileUploadRequest request = new FileUploadRequest().filename(filename).contentType(contentType).size(size);
return call(false, h -> api.uploadFile(key, request));
}

/** One file of the library, with a signed link to read it; its status is {@code PENDING} until its bytes are stored. */
public ModelFile get(UUID id) { return call(false, h -> api.getFile(id, h)); }

/** Registers a file and sends its bytes, with a random {@code Idempotency-Key}; see {@link #upload(byte[], String, String, String)}. */
public FileUpload upload(byte[] data, String filename, String contentType) {
return upload(data, filename, contentType, null);
}

/**
* Registers a file and sends its bytes (an HTTP {@code PUT} with the same {@code Content-Type}, no API key), and
* returns the registration: put its id in {@code photoIds}. The {@code PUT} is retried like any call; a refused
* one is thrown as a {@link ControlResellError} (the file stays {@code PENDING}).
*/
public FileUpload upload(byte[] data, String filename, String contentType, String idempotencyKey) {
FileUpload upload = register(filename, contentType, data.length, idempotencyKey);
if (upload.getUploadUrl() == null || upload.getUploadUrl().isBlank()) {
throw new IllegalStateException("The API returned no upload address for file " + upload.getId());
}
HttpRequest request = HttpRequest.newBuilder(URI.create(upload.getUploadUrl()))
.timeout(timeout)
.header("Content-Type", contentType)
.header("User-Agent", "controlresell-java/" + VERSION)
.PUT(HttpRequest.BodyPublishers.ofByteArray(data))
.build();
call(false, h -> {
try {
HttpResponse<byte[]> response = storage.send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() / 100 != 2) {
throw new ApiException(response.statusCode(), "HTTP " + response.statusCode(), response.headers(), null);
}
return response.statusCode();
} catch (IOException e) {
throw new ApiException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ApiException(e);
}
});
return upload;
}
}

/** The seller the API key acts for. */
public final class MeResource {
private final MeApi api = new MeApi(apiClient);
Expand Down
Loading
Loading