diff --git a/CHANGELOG.md b/CHANGELOG.md index 01bb2ff..414c34e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ All notable changes to this project are documented here. The format follows ### Added +- `designs()` (`DesignsResource`) for the create suite: `list`, `create`, `get`, `update`, + `delete`, `versions`, `restoreVersion`, `duplicate`, `export`, `bulk`, `posts`, `textToSlides` + and `fromPost`, with `DesignParams` and the `Design`, `DesignVersion`, `DesignSaved`, + `DesignDeleted`, `DesignExport`, `DesignPost` and `TextSlides` records. The design document is + a `Map` passed through unchanged. `textToSlides` also needs `ai`. +- `fonts()` (`FontsResource`): `list`, `upload` from a `Path`, bytes or a `FileInput`, and + `delete`, with `FontList`, `LibraryFont` and `UploadedFont`. +- `videoEdits()` (`VideoEditsResource`): `ops`, `list`, `create` and `get`, with `VideoEdit`, + `VideoEditOp` and `VideoEditOperation`. A step that uses AI also needs `ai`. +- `media().generateAltText(mediaId)` (`AltText`) writes and saves alt text for one image. Needs + `ai`. - `posts().validateImport` and `posts().commitImport` gain overloads taking a `List` of account ids or platform names, sent comma-separated and applied to every CSV row whose own `accounts` cell is empty. diff --git a/CLAUDE.md b/CLAUDE.md index f1d5a46..ddbd675 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -82,7 +82,7 @@ calls `ApiClient.unwrap(...)` and `convert(...)`/`convertList(...)` into a recor - `FoPost` instances are immutable and safe to share across threads. **Resources wired today:** `posts`, `accounts` (with `accounts().communities()`), `workspaces`, -`labels`, `webhooks`, `analytics`, `automations`, `media`, `ai`, `inbox`, `contacts`, `broadcasts`, `sequences`, `ads`, `googleAds`, `validate`. This is +`labels`, `webhooks`, `analytics`, `automations`, `media`, `ai`, `inbox`, `contacts`, `broadcasts`, `sequences`, `ads`, `googleAds`, `validate`, `designs`, `fonts`, `videoEdits`. This is the most complete of the FoPost SDKs — do not narrow it. `FoPost.request(...)` is the escape hatch for anything unwrapped. diff --git a/README.md b/README.md index 622f5c0..97f6c53 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ long failed = client.posts().stream(PostListParams.create().workspaceId(workspac | `webhooks()` | `list`, `create`, `update`, `delete`, `test`, `deliveries`, `delivery`, `redeliver`, `verify` | | `analytics()` | `overview`, `timeSeries`, `topPosts`, `labels`, `postsTable`, `postingStreak`, `demographics`, `collect`, `decay`, `frequency`, `timeline`, `changes`, `collectPost`, `nativePosts`, `inbox` | | `automations()` | `list`, `get`, `create`, `update`, `delete`, `toggle`, `runs`, `run`, `trigger`, `stats`, `versions`, `version`, `restoreVersion` | -| `media()` | `list`, `limits`, `upload`, `presign`, `complete`, `uploadDirect`, `delete` | +| `media()` | `list`, `limits`, `upload`, `presign`, `complete`, `uploadDirect`, `generateAltText`, `delete` | | `ai()` | `credits`, `generateCaption`, `rewrite`, `repurposeUrl` | | `inbox()` | `list`, `threads`, `conversations`, `unreadCount`, `accounts`, `platforms`, `markThreadRead`, `markConversationRead`, `refresh`, `update`, `editComment`, `reply`, `hide`, `unhide`, `delete`, `like`, `unlike`, `repost`, `unrepost`, `bookmark`, `unbookmark`, `vote`, `pin`, `unpin`, `react`, `startConversation`, `setTyping`, `passThreadControl`, `takeThreadControl`, `handover`, `listApprovals`, `approveReply`, `rejectReply` | | `contacts()` | `list`, `get`, `create`, `update`, `delete`, `conversations`, `importCsv`, `listFields`, `createField`, `updateField`, `updateFieldOptions`, `deleteField`, `conversationAnalytics` | @@ -121,6 +121,9 @@ long failed = client.posts().stream(PostListParams.create().workspaceId(workspac | `googleBusiness()` | `getLocation`, `updateLocation`, `getAttributes`, `updateAttributes`, `getMenus`, `replaceMenus`, `getServices`, `replaceServices`, `listMedia`, `addMedia`, `deleteMedia`, `listPlaceActions`, `createPlaceAction`, `updatePlaceAction`, `deletePlaceAction`, `getVerificationOptions`, `startVerification`, `completeVerification`, `getPerformance`, `getSearchKeywords`, `assign` | | `blogs()` | `listBlogs`, `listArticles`, `getArticle`, `createArticle`, `updateArticle`, `deleteArticle`, `listProducts`, `updateProduct` | | `validate()` | `post`, `length`, `media`, `subreddit` | +| `designs()` | `list`, `create`, `get`, `update`, `delete`, `versions`, `restoreVersion`, `duplicate`, `export`, `bulk`, `posts`, `textToSlides`, `fromPost` | +| `fonts()` | `list`, `upload`, `delete` | +| `videoEdits()` | `ops`, `list`, `create`, `get` | | `queue()` | `listSlots`, `createSlot`, `updateSlot`, `deleteSlot`, `next`, `preview` | | `connectSessions()` | `create`, `get` | | `logs()` | `list` | @@ -248,6 +251,53 @@ files under `files` (at most five). Otherwise it uploads each file directly, the JSON request with the results attached as media items. An update without new content reads the post first and appends to what its first block already holds. +## Designs and video + +`designs()` holds the create suite's designs. The design document is a plain +`Map` sent and returned unchanged, keys and nulls included: + +```java +Design design = client.designs().create( + DesignParams.Create.of(workspace.id(), "post", document).name("Launch card")); + +DesignSaved saved = client.designs().update(design.id(), + DesignParams.Update.create().document(edited).createVersion(true)); + +// Each file lands in the media library, ready to attach to a post. +DesignExport export = client.designs().export(design.id(), DesignParams.Export.of("png")); +String mediaId = export.files().get(0).mediaId(); + +// One design per row, filling text and image layers by layer id or name. +List cards = client.designs().bulk(design.id(), + List.of(Map.of("headline", "Spring sale"), Map.of("headline", "Summer sale")), "headline"); +``` + +`fonts()` lists the font library and the workspace's uploaded fonts, and uploads one TTF, OTF, +WOFF or WOFF2 file of up to 5 MB: + +```java +UploadedFont font = client.fonts().upload(workspace.id(), + FileInput.of(Path.of("BrandSans-Bold.woff2")), "Brand Sans", 700, "normal"); +``` + +`videoEdits()` runs steps such as `trim`, `subtitles` or `reframe` over a library video as a +background job. `ops()` lists every step and whether it can run; a job comes back `queued`, so +read it again with `get` until it is `succeeded` or `failed`: + +```java +VideoEdit job = client.videoEdits().create(mediaId, List.of( + VideoEditOperation.of("trim", Map.of("startSec", 2, "endSec", 12)), + VideoEditOperation.of("reframe", Map.of("aspect", "9:16")))); + +job = client.videoEdits().get(job.id()); +``` + +`media().generateAltText(mediaId)` writes alt text for one image and saves it on the item. + +All of these need the `posts` scope and create access on the workspace. `textToSlides`, +`generateAltText` and any video step whose `usesAi()` is true spend AI, so they also need `ai`; an +empty balance answers `402`. + ## Webhooks The signing secret is returned by the create call and never shown again — store it then. diff --git a/src/main/java/com/fopost/sdk/FoPost.java b/src/main/java/com/fopost/sdk/FoPost.java index 8e6e57a..7b71c02 100644 --- a/src/main/java/com/fopost/sdk/FoPost.java +++ b/src/main/java/com/fopost/sdk/FoPost.java @@ -16,6 +16,8 @@ import com.fopost.sdk.resource.BroadcastsResource; import com.fopost.sdk.resource.ConnectSessionsResource; import com.fopost.sdk.resource.ContactsResource; +import com.fopost.sdk.resource.DesignsResource; +import com.fopost.sdk.resource.FontsResource; import com.fopost.sdk.resource.BlogsResource; import com.fopost.sdk.resource.InboxResource; import com.fopost.sdk.resource.KnowledgeResource; @@ -28,6 +30,7 @@ import com.fopost.sdk.resource.QueueResource; import com.fopost.sdk.resource.SequencesResource; import com.fopost.sdk.resource.ValidateResource; +import com.fopost.sdk.resource.VideoEditsResource; import com.fopost.sdk.resource.WebhooksResource; import com.fopost.sdk.resource.WhatsappResource; import com.fopost.sdk.resource.WorkspacesResource; @@ -80,6 +83,9 @@ public final class FoPost { private final AnalyticsResource analytics; private final AutomationsResource automations; private final MediaResource media; + private final DesignsResource designs; + private final FontsResource fonts; + private final VideoEditsResource videoEdits; private final AiResource ai; private final ContactsResource contacts; private final BroadcastsResource broadcasts; @@ -111,6 +117,9 @@ private FoPost(ApiClient http) { this.analytics = new AnalyticsResource(http); this.automations = new AutomationsResource(http); this.media = new MediaResource(http); + this.designs = new DesignsResource(http); + this.fonts = new FontsResource(http); + this.videoEdits = new VideoEditsResource(http); this.ai = new AiResource(http); this.whatsapp = new WhatsappResource(http); this.inbox = new InboxResource(http); @@ -192,6 +201,21 @@ public MediaResource media() { return media; } + /** Designs from the create suite: versions, exports, bulk and text-to-slides. */ + public DesignsResource designs() { + return designs; + } + + /** The font library and the workspace's uploaded fonts. */ + public FontsResource fonts() { + return fonts; + } + + /** Video edit jobs over videos in the media library. */ + public VideoEditsResource videoEdits() { + return videoEdits; + } + public ActivityResource activity() { return activity; } diff --git a/src/main/java/com/fopost/sdk/internal/Json.java b/src/main/java/com/fopost/sdk/internal/Json.java index 69eb2af..dfc253c 100644 --- a/src/main/java/com/fopost/sdk/internal/Json.java +++ b/src/main/java/com/fopost/sdk/internal/Json.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyName; import com.fasterxml.jackson.databind.PropertyNamingStrategies; @@ -21,6 +22,13 @@ private Json() {} public static final ObjectMapper MAPPER = create(); + private static final ObjectMapper VERBATIM = new ObjectMapper().registerModule(new JavaTimeModule()); + + /** A caller-shaped payload as a tree, keys and nulls exactly as given. */ + public static JsonNode verbatim(Object value) { + return VERBATIM.valueToTree(value); + } + private static ObjectMapper create() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); diff --git a/src/main/java/com/fopost/sdk/model/AltText.java b/src/main/java/com/fopost/sdk/model/AltText.java new file mode 100644 index 0000000..00f5ff5 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/AltText.java @@ -0,0 +1,11 @@ +package com.fopost.sdk.model; + +import java.util.List; + +/** + * Alt text generated for an image, now saved on the media item. + * + * @param altTextSource {@code ai} for generated text + * @param tags subject tags for searching the library + */ +public record AltText(String altText, String altTextSource, List tags) {} diff --git a/src/main/java/com/fopost/sdk/model/Design.java b/src/main/java/com/fopost/sdk/model/Design.java new file mode 100644 index 0000000..b4b7804 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/Design.java @@ -0,0 +1,26 @@ +package com.fopost.sdk.model; + +import java.time.Instant; +import java.util.Map; + +/** + * A design from the create suite: a story, post, carousel, thumbnail, card or collage. + * + * @param kind {@code story}, {@code post}, {@code carousel}, {@code thumbnail}, {@code card} or + * {@code collage} + * @param document the design document as the API stores it (pages, layers, size), passed through + * unchanged; null on a list, which leaves it out + * @param thumbnailMediaId the media library item holding the first-page preview, once rendered + * @param thumbnailUrl a membership-checked URL for that preview + */ +public record Design( + String id, + String name, + String kind, + Integer width, + Integer height, + Map document, + String thumbnailMediaId, + String thumbnailUrl, + Instant createdAt, + Instant updatedAt) {} diff --git a/src/main/java/com/fopost/sdk/model/DesignDeleted.java b/src/main/java/com/fopost/sdk/model/DesignDeleted.java new file mode 100644 index 0000000..cd34b8f --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/DesignDeleted.java @@ -0,0 +1,3 @@ +package com.fopost.sdk.model; + +public record DesignDeleted(String id, boolean deleted) {} diff --git a/src/main/java/com/fopost/sdk/model/DesignExport.java b/src/main/java/com/fopost/sdk/model/DesignExport.java new file mode 100644 index 0000000..8d8bd9b --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/DesignExport.java @@ -0,0 +1,16 @@ +package com.fopost.sdk.model; + +import java.util.List; + +/** + * The files an export rendered, each already in the media library. + * + * @param format {@code png}, {@code jpg}, {@code pdf}, {@code mp4} or {@code gif} + */ +public record DesignExport(String format, List files) { + + /** + * @param pageIndex the page this file shows; null for a file covering the whole design + */ + public record File(String mediaId, Integer pageIndex, String mimeType, String previewUrl) {} +} diff --git a/src/main/java/com/fopost/sdk/model/DesignPost.java b/src/main/java/com/fopost/sdk/model/DesignPost.java new file mode 100644 index 0000000..53a52f0 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/DesignPost.java @@ -0,0 +1,22 @@ +package com.fopost.sdk.model; + +import java.time.Instant; +import java.util.List; + +/** + * A post that uses an export of a design. + * + * @param text the start of the post's first block, at most 280 characters + * @param engagement summed across the post's networks; null until something was measured + * @param views summed the same way + */ +public record DesignPost( + String id, + String status, + String title, + String text, + Instant scheduleAt, + Instant publishedAt, + List platforms, + Long engagement, + Long views) {} diff --git a/src/main/java/com/fopost/sdk/model/DesignSaved.java b/src/main/java/com/fopost/sdk/model/DesignSaved.java new file mode 100644 index 0000000..4a2ee50 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/DesignSaved.java @@ -0,0 +1,8 @@ +package com.fopost.sdk.model; + +/** + * The result of an update or a version restore. + * + * @param version the version row this save wrote, or null when it did not write one + */ +public record DesignSaved(Design design, DesignVersion version) {} diff --git a/src/main/java/com/fopost/sdk/model/DesignVersion.java b/src/main/java/com/fopost/sdk/model/DesignVersion.java new file mode 100644 index 0000000..f25c6af --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/DesignVersion.java @@ -0,0 +1,11 @@ +package com.fopost.sdk.model; + +import java.time.Instant; +import java.util.Map; + +/** + * A saved version of a design. + * + * @param document set only when the versions were listed with their documents + */ +public record DesignVersion(String id, Integer version, Map document, Instant createdAt) {} diff --git a/src/main/java/com/fopost/sdk/model/FontList.java b/src/main/java/com/fopost/sdk/model/FontList.java new file mode 100644 index 0000000..fe86f68 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/FontList.java @@ -0,0 +1,11 @@ +package com.fopost.sdk.model; + +import java.util.List; + +/** + * The fonts a design can use. + * + * @param library the fonts FoPost ships + * @param uploaded the workspace's own fonts; empty when no workspace was named + */ +public record FontList(List library, List uploaded) {} diff --git a/src/main/java/com/fopost/sdk/model/LibraryFont.java b/src/main/java/com/fopost/sdk/model/LibraryFont.java new file mode 100644 index 0000000..47c1c58 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/LibraryFont.java @@ -0,0 +1,17 @@ +package com.fopost.sdk.model; + +import java.util.List; + +/** + * A font family FoPost ships. + * + * @param category {@code sans}, {@code serif}, {@code display}, {@code handwriting} or + * {@code monospace} + * @param scripts {@code latin}, {@code arabic} + */ +public record LibraryFont( + String slug, String family, String category, List scripts, List weights, List files) { + + /** One font file of the family, covering one subset. */ + public record File(String path, String subset, String weight, String style, String unicodeRange) {} +} diff --git a/src/main/java/com/fopost/sdk/model/TextSlides.java b/src/main/java/com/fopost/sdk/model/TextSlides.java new file mode 100644 index 0000000..8a001b5 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/TextSlides.java @@ -0,0 +1,20 @@ +package com.fopost.sdk.model; + +import java.util.List; + +/** + * Text split into carousel slides: a cover, the slides, a closing call to action, and the brand + * the slides should wear. + */ +public record TextSlides(Cover cover, List slides, Cta cta, Brand brand) { + + public record Cover(String title, String subtitle) {} + + public record Slide(String heading, String body) {} + + public record Cta(String heading, String body) {} + + public record Brand(List colors, Fonts fonts, String logoMediaId) {} + + public record Fonts(String heading, String body) {} +} diff --git a/src/main/java/com/fopost/sdk/model/UploadedFont.java b/src/main/java/com/fopost/sdk/model/UploadedFont.java new file mode 100644 index 0000000..4617a81 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/UploadedFont.java @@ -0,0 +1,13 @@ +package com.fopost.sdk.model; + +import java.time.Instant; + +/** + * A font uploaded into a workspace. + * + * @param style {@code normal} or {@code italic} + * @param format the stored file format, such as {@code woff2} or {@code ttf} + * @param fileUrl where the file is served, after a membership check + */ +public record UploadedFont( + String id, String family, String style, Integer weight, String format, String fileUrl, Instant createdAt) {} diff --git a/src/main/java/com/fopost/sdk/model/VideoEdit.java b/src/main/java/com/fopost/sdk/model/VideoEdit.java new file mode 100644 index 0000000..0577ba4 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/VideoEdit.java @@ -0,0 +1,35 @@ +package com.fopost.sdk.model; + +import java.time.Instant; +import java.util.List; +import java.util.Map; + +/** + * A video edit job: the source video, the steps, and what they produced. + * + * @param status {@code queued}, {@code running}, {@code succeeded} or {@code failed} + * @param progress 0 to 100 + * @param resultMediaId the media library item holding the edited video, once it succeeded + * @param extraOutputs further files a step produced, such as clips or a subtitle file + * @param result step-specific output, such as a transcript; null until there is one + * @param error why it failed, in plain words + */ +public record VideoEdit( + String id, + String status, + Integer progress, + String sourceMediaId, + List operations, + String resultMediaId, + ResultMedia resultMedia, + List extraOutputs, + Map result, + String error, + Instant createdAt, + Instant updatedAt, + Instant completedAt) { + + public record ResultMedia(String id, String previewUrl, String mimeType) {} + + public record ExtraOutput(String mediaId, String label, String mime, String previewUrl) {} +} diff --git a/src/main/java/com/fopost/sdk/model/VideoEditOp.java b/src/main/java/com/fopost/sdk/model/VideoEditOp.java new file mode 100644 index 0000000..9c9e0a4 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/VideoEditOp.java @@ -0,0 +1,9 @@ +package com.fopost.sdk.model; + +/** + * A video edit step the API knows. + * + * @param available false when this deployment cannot run it right now + * @param usesAi true when it spends AI, so an API key also needs the {@code ai} scope + */ +public record VideoEditOp(String name, boolean available, boolean usesAi) {} diff --git a/src/main/java/com/fopost/sdk/model/VideoEditOperation.java b/src/main/java/com/fopost/sdk/model/VideoEditOperation.java new file mode 100644 index 0000000..500d287 --- /dev/null +++ b/src/main/java/com/fopost/sdk/model/VideoEditOperation.java @@ -0,0 +1,20 @@ +package com.fopost.sdk.model; + +import java.util.Map; + +/** + * One step of a video edit, such as {@code trim}, {@code subtitles} or {@code reframe}. + * + * @param params the step's parameters exactly as the API names them, for example + * {@code Map.of("startSec", 2, "endSec", 12)} for {@code trim}; sent unchanged + */ +public record VideoEditOperation(String op, Map params) { + + public static VideoEditOperation of(String op, Map params) { + return new VideoEditOperation(op, params); + } + + public static VideoEditOperation of(String op) { + return new VideoEditOperation(op, Map.of()); + } +} diff --git a/src/main/java/com/fopost/sdk/param/DesignParams.java b/src/main/java/com/fopost/sdk/param/DesignParams.java new file mode 100644 index 0000000..fcac062 --- /dev/null +++ b/src/main/java/com/fopost/sdk/param/DesignParams.java @@ -0,0 +1,244 @@ +package com.fopost.sdk.param; + +import com.fopost.sdk.internal.Json; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Filters and bodies for {@code designs()}. + * + *

A design document travels exactly as given: its keys and nulls are not renamed or dropped. + * + *

{@code
+ * DesignParams.Create.of(workspaceId, "post", document).name("Launch card");
+ * DesignParams.Update.create().document(document).createVersion(true);
+ * DesignParams.Export.of("png").pageIndexes(List.of(0, 1));
+ * }
+ */ +public final class DesignParams { + + private DesignParams() {} + + /** Filters for the designs listing. */ + public static final class Filter { + + private final Map query = new LinkedHashMap<>(); + + public static Filter of(String workspaceId) { + Filter filter = new Filter(); + filter.query.put("workspace_id", workspaceId); + return filter; + } + + public Filter kind(String kind) { + Params.put(query, "kind", kind); + return this; + } + + /** Defaults to 50, at most 100. */ + public Filter limit(int limit) { + Params.put(query, "limit", limit); + return this; + } + + public Filter offset(int offset) { + Params.put(query, "offset", offset); + return this; + } + + public Map toQuery() { + return new LinkedHashMap<>(query); + } + } + + /** The body of a design create. */ + public static final class Create { + + private final Map body = new LinkedHashMap<>(); + + public static Create of(String workspaceId, String kind, Map document) { + Create create = new Create(); + create.body.put("workspace_id", workspaceId); + create.body.put("kind", kind); + create.body.put("document", Json.verbatim(document)); + return create; + } + + /** Defaults to {@code Untitled Design}. */ + public Create name(String name) { + Params.put(body, "name", name); + return this; + } + + public Create brandVoiceId(String brandVoiceId) { + Params.put(body, "brand_voice_id", brandVoiceId); + return this; + } + + public Map toMap() { + return new LinkedHashMap<>(body); + } + } + + /** A partial design update: only what was set is sent. */ + public static final class Update { + + private final Map body = new LinkedHashMap<>(); + + public static Update create() { + return new Update(); + } + + public Update name(String name) { + Params.put(body, "name", name); + return this; + } + + public Update kind(String kind) { + Params.put(body, "kind", kind); + return this; + } + + public Update document(Map document) { + body.put("document", Json.verbatim(document)); + return this; + } + + /** True writes a version row now; otherwise only every few saves do. */ + public Update createVersion(boolean createVersion) { + body.put("create_version", createVersion); + return this; + } + + public Map toMap() { + return new LinkedHashMap<>(body); + } + } + + /** What to render a design to. */ + public static final class Export { + + private final Map body = new LinkedHashMap<>(); + + /** {@code png}, {@code jpg}, {@code pdf}, {@code mp4} or {@code gif}. */ + public static Export of(String format) { + Export export = new Export(); + export.body.put("format", format); + return export; + } + + /** Zero-based pages to render; every page when not set. */ + public Export pageIndexes(List pageIndexes) { + Params.put(body, "page_indexes", pageIndexes); + return this; + } + + /** 5, 10 or 15 seconds, for {@code mp4} and {@code gif}. */ + public Export durationSec(int durationSec) { + body.put("duration_sec", durationSec); + return this; + } + + public Map toMap() { + return new LinkedHashMap<>(body); + } + } + + /** Text to split into carousel slides. Needs {@code text} or {@code postId}. */ + public static final class TextToSlides { + + private final Map body = new LinkedHashMap<>(); + + public static TextToSlides of(String workspaceId) { + TextToSlides params = new TextToSlides(); + params.body.put("workspace_id", workspaceId); + return params; + } + + public TextToSlides text(String text) { + Params.put(body, "text", text); + return this; + } + + /** Read the text from a post in the workspace instead. */ + public TextToSlides postId(String postId) { + Params.put(body, "post_id", postId); + return this; + } + + public TextToSlides maxSlides(int maxSlides) { + body.put("max_slides", maxSlides); + return this; + } + + public TextToSlides brandVoiceId(String brandVoiceId) { + Params.put(body, "brand_voice_id", brandVoiceId); + return this; + } + + public Map toMap() { + return new LinkedHashMap<>(body); + } + } + + /** A text post or carousel built from post text. Needs {@code text} or {@code postId}. */ + public static final class FromPost { + + private final Map body = new LinkedHashMap<>(); + + public static FromPost of(String workspaceId) { + FromPost params = new FromPost(); + params.body.put("workspace_id", workspaceId); + return params; + } + + public FromPost text(String text) { + Params.put(body, "text", text); + return this; + } + + public FromPost postId(String postId) { + Params.put(body, "post_id", postId); + return this; + } + + /** Defaults to {@code quote}. */ + public FromPost style(String style) { + Params.put(body, "style", style); + return this; + } + + /** A size preset id, for a single text post. */ + public FromPost preset(String preset) { + Params.put(body, "preset", preset); + return this; + } + + /** The connected account whose name and handle the design shows. */ + public FromPost accountId(String accountId) { + Params.put(body, "account_id", accountId); + return this; + } + + /** Unset, a long text becomes a carousel and a short one a single post. */ + public FromPost carousel(boolean carousel) { + body.put("carousel", carousel); + return this; + } + + public FromPost name(String name) { + Params.put(body, "name", name); + return this; + } + + public FromPost brandVoiceId(String brandVoiceId) { + Params.put(body, "brand_voice_id", brandVoiceId); + return this; + } + + public Map toMap() { + return new LinkedHashMap<>(body); + } + } +} diff --git a/src/main/java/com/fopost/sdk/resource/DesignsResource.java b/src/main/java/com/fopost/sdk/resource/DesignsResource.java new file mode 100644 index 0000000..80acde7 --- /dev/null +++ b/src/main/java/com/fopost/sdk/resource/DesignsResource.java @@ -0,0 +1,145 @@ +package com.fopost.sdk.resource; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fopost.sdk.internal.ApiClient; +import com.fopost.sdk.model.Design; +import com.fopost.sdk.model.DesignDeleted; +import com.fopost.sdk.model.DesignExport; +import com.fopost.sdk.model.DesignPost; +import com.fopost.sdk.model.DesignSaved; +import com.fopost.sdk.model.DesignVersion; +import com.fopost.sdk.model.TextSlides; +import com.fopost.sdk.param.DesignParams; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Designs from the create suite, their versions and exports. + * + *
{@code
+ * Design design = client.designs().create(
+ *         DesignParams.Create.of(workspaceId, "post", document).name("Launch card"));
+ * DesignExport export = client.designs().export(design.id(), DesignParams.Export.of("png"));
+ * }
+ * + *

Needs the {@code posts} scope and create access on the workspace. {@link #textToSlides} spends + * AI, so it also needs {@code ai}; an empty balance answers {@code 402}. + */ +public final class DesignsResource { + + private final ApiClient http; + + public DesignsResource(ApiClient http) { + this.http = http; + } + + /** Newest first, without their documents. */ + public List list(String workspaceId) { + return list(DesignParams.Filter.of(workspaceId)); + } + + public List list(DesignParams.Filter filter) { + return http.convertList(ApiClient.unwrap(http.get("/v1/designs", filter.toQuery())), Design.class); + } + + public Design create(DesignParams.Create params) { + return http.convert(ApiClient.unwrap(http.post("/v1/designs", params.toMap())), Design.class); + } + + public Design get(String designId) { + return http.convert(ApiClient.unwrap(http.get("/v1/designs/" + designId, null)), Design.class); + } + + public DesignSaved update(String designId, DesignParams.Update params) { + return saved(http.patch("/v1/designs/" + designId, params.toMap())); + } + + public DesignDeleted delete(String designId) { + return http.convert(ApiClient.unwrap(http.delete("/v1/designs/" + designId)), DesignDeleted.class); + } + + public List versions(String designId) { + return versions(designId, false); + } + + /** {@code includeDocument} returns each version's full document too. */ + public List versions(String designId, boolean includeDocument) { + Map query = new LinkedHashMap<>(); + if (includeDocument) { + query.put("include", "document"); + } + return http.convertList( + ApiClient.unwrap(http.get("/v1/designs/" + designId + "/versions", query)), + DesignVersion.class); + } + + /** Puts an earlier version's document back and records it as a new version. */ + public DesignSaved restoreVersion(String designId, String versionId) { + return saved(http.post( + "/v1/designs/" + designId + "/versions/" + versionId + "/restore", + new LinkedHashMap())); + } + + public Design duplicate(String designId) { + return duplicate(designId, null); + } + + /** {@code name} may be null, which names the copy {@code (Copy)}. */ + public Design duplicate(String designId, String name) { + Map body = new LinkedHashMap<>(); + if (name != null) { + body.put("name", name); + } + return http.convert( + ApiClient.unwrap(http.post("/v1/designs/" + designId + "/duplicate", body)), Design.class); + } + + /** Renders the design and adds each file to the media library. */ + public DesignExport export(String designId, DesignParams.Export params) { + return http.convert( + ApiClient.unwrap(http.post("/v1/designs/" + designId + "/export", params.toMap())), + DesignExport.class); + } + + public List bulk(String designId, List> rows) { + return bulk(designId, rows, null); + } + + /** + * One new design per row, filling text and image layers matched by layer id or name. At most 50 + * rows. {@code nameColumn} names the column each new design is named after; null numbers them. + */ + public List bulk(String designId, List> rows, String nameColumn) { + Map body = new LinkedHashMap<>(); + body.put("rows", rows); + if (nameColumn != null) { + body.put("name_column", nameColumn); + } + return http.convertList( + ApiClient.unwrap(http.post("/v1/designs/" + designId + "/bulk", body)), Design.class); + } + + /** Posts that use an export of this design, newest first. */ + public List posts(String designId) { + return http.convertList( + ApiClient.unwrap(http.get("/v1/designs/" + designId + "/posts", null)), DesignPost.class); + } + + /** Splits text into carousel slides. Creates nothing; build the design from the result. */ + public TextSlides textToSlides(DesignParams.TextToSlides params) { + return http.convert( + ApiClient.unwrap(http.post("/v1/designs/text-to-slides", params.toMap())), TextSlides.class); + } + + /** Creates a text post or carousel design from post text. */ + public Design fromPost(DesignParams.FromPost params) { + return http.convert( + ApiClient.unwrap(http.post("/v1/designs/from-post", params.toMap())), Design.class); + } + + private DesignSaved saved(JsonNode body) { + return new DesignSaved( + http.convert(body.get("data"), Design.class), http.convert(body.get("version"), DesignVersion.class)); + } +} diff --git a/src/main/java/com/fopost/sdk/resource/FontsResource.java b/src/main/java/com/fopost/sdk/resource/FontsResource.java new file mode 100644 index 0000000..05a1dd8 --- /dev/null +++ b/src/main/java/com/fopost/sdk/resource/FontsResource.java @@ -0,0 +1,73 @@ +package com.fopost.sdk.resource; + +import com.fopost.sdk.internal.ApiClient; +import com.fopost.sdk.internal.Multipart; +import com.fopost.sdk.model.FontList; +import com.fopost.sdk.model.UploadedFont; +import com.fopost.sdk.param.FileInput; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * The font library and a workspace's uploaded fonts, for designs. Needs the {@code posts} scope and + * create access on the workspace. + * + *

A font file is at most 5 MB; a larger one answers {@code 413} with {@code file_too_large}, and + * a workspace at its font cap answers {@code 409} with {@code font_limit_reached}. + */ +public final class FontsResource { + + private final ApiClient http; + + public FontsResource(ApiClient http) { + this.http = http; + } + + /** The library only. */ + public FontList list() { + return list(null); + } + + /** The library plus the workspace's uploaded fonts. */ + public FontList list(String workspaceId) { + Map query = new LinkedHashMap<>(); + if (workspaceId != null) { + query.put("workspace_id", workspaceId); + } + return http.convert(http.get("/v1/fonts", query), FontList.class); + } + + public UploadedFont upload(String workspaceId, Path file) { + return upload(workspaceId, FileInput.of(file), null, null, null); + } + + public UploadedFont upload(String workspaceId, String filename, byte[] content, String contentType) { + return upload(workspaceId, FileInput.of(filename, contentType, content), null, null, null); + } + + /** + * Uploads one TTF, OTF, WOFF or WOFF2 file. {@code family} defaults to the file name, + * {@code weight} (100 to 1000) to 400, and {@code style} ({@code normal} or {@code italic}) to + * {@code normal}; pass null for any of them. + */ + public UploadedFont upload(String workspaceId, FileInput file, String family, Integer weight, String style) { + Multipart body = new Multipart() + .file("file", file.filename(), file.mimeType(), file.source(), file.size()) + .field("workspace_id", workspaceId); + if (family != null) { + body.field("family", family); + } + if (weight != null) { + body.field("weight", String.valueOf(weight)); + } + if (style != null) { + body.field("style", style); + } + return http.convert(ApiClient.unwrap(http.request("POST", "/v1/fonts", body, null)), UploadedFont.class); + } + + public void delete(String fontId) { + http.delete("/v1/fonts/" + fontId); + } +} diff --git a/src/main/java/com/fopost/sdk/resource/MediaResource.java b/src/main/java/com/fopost/sdk/resource/MediaResource.java index b21727a..1ab2353 100644 --- a/src/main/java/com/fopost/sdk/resource/MediaResource.java +++ b/src/main/java/com/fopost/sdk/resource/MediaResource.java @@ -2,6 +2,7 @@ import com.fopost.sdk.internal.ApiClient; import com.fopost.sdk.internal.Multipart; +import com.fopost.sdk.model.AltText; import com.fopost.sdk.model.MediaLibraryItem; import com.fopost.sdk.model.PresignedUpload; import com.fopost.sdk.model.UploadLimits; @@ -93,6 +94,17 @@ public UploadedMedia uploadDirect(PresignUploadParams params, FileInput file) { return complete(presigned.uploadId()); } + /** + * Writes alt text for one image in the library and saves it on the item. Spends AI, so it needs + * the {@code ai} scope; an empty balance answers {@code 402}, and a file that is not an image + * answers {@code 400}. + */ + public AltText generateAltText(String mediaId) { + return http.convert( + ApiClient.unwrap(http.post("/v1/media/" + mediaId + "/alt-text", new LinkedHashMap())), + AltText.class); + } + /** Removes the file from the library. Posts already published keep the copy on the platform. */ public void delete(String mediaId) { http.delete("/v1/media/" + mediaId); diff --git a/src/main/java/com/fopost/sdk/resource/VideoEditsResource.java b/src/main/java/com/fopost/sdk/resource/VideoEditsResource.java new file mode 100644 index 0000000..a891e09 --- /dev/null +++ b/src/main/java/com/fopost/sdk/resource/VideoEditsResource.java @@ -0,0 +1,87 @@ +package com.fopost.sdk.resource; + +import com.fopost.sdk.internal.ApiClient; +import com.fopost.sdk.internal.Json; +import com.fopost.sdk.model.VideoEdit; +import com.fopost.sdk.model.VideoEditOp; +import com.fopost.sdk.model.VideoEditOperation; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Video edits: trim, cut, subtitles, reframe and the other steps from {@link #ops()}, run as a job + * over a video in the media library. + * + *

{@code
+ * VideoEdit job = client.videoEdits().create(mediaId, List.of(
+ *         VideoEditOperation.of("trim", Map.of("startSec", 2, "endSec", 12)),
+ *         VideoEditOperation.of("reframe", Map.of("aspect", "9:16"))));
+ * // later
+ * job = client.videoEdits().get(job.id());
+ * }
+ * + *

Needs the {@code posts} scope and create access on the workspace. A step whose {@code usesAi} + * is true also needs {@code ai}, and an empty balance answers {@code 402}. A job runs in the + * background, so {@link #create} returns it {@code queued}; read it again with {@link #get}. + */ +public final class VideoEditsResource { + + private final ApiClient http; + + public VideoEditsResource(ApiClient http) { + this.http = http; + } + + /** Every step the API knows, and whether it can run now. */ + public List ops() { + return http.convertList(ApiClient.unwrap(http.get("/v1/video-edits/ops", null)), VideoEditOp.class); + } + + /** The workspace's jobs, newest first. */ + public List list(String workspaceId) { + return list(workspaceId, null, null); + } + + /** + * Needs {@code workspaceId} or {@code mediaId}; with only {@code mediaId}, the workspace is the + * one owning that video. {@code limit} defaults to 20. + */ + public List list(String workspaceId, String mediaId, Integer limit) { + Map query = new LinkedHashMap<>(); + putIfPresent(query, "workspace_id", workspaceId); + putIfPresent(query, "media_id", mediaId); + putIfPresent(query, "limit", limit); + return http.convertList(ApiClient.unwrap(http.get("/v1/video-edits", query)), VideoEdit.class); + } + + /** Queues a job in the workspace that owns the video. At most 10 steps. */ + public VideoEdit create(String mediaId, List operations) { + return create(null, mediaId, operations); + } + + /** As above, naming the workspace; null resolves it from the video. */ + public VideoEdit create(String workspaceId, String mediaId, List operations) { + Map body = new LinkedHashMap<>(); + putIfPresent(body, "workspace_id", workspaceId); + body.put("media_id", mediaId); + body.put("operations", Json.verbatim(operations)); + return http.convert(ApiClient.unwrap(http.post("/v1/video-edits", body)), VideoEdit.class); + } + + public VideoEdit get(String jobId) { + return get(jobId, null); + } + + public VideoEdit get(String jobId, String workspaceId) { + Map query = new LinkedHashMap<>(); + putIfPresent(query, "workspace_id", workspaceId); + return http.convert(ApiClient.unwrap(http.get("/v1/video-edits/" + jobId, query)), VideoEdit.class); + } + + private static void putIfPresent(Map target, String key, Object value) { + if (value != null) { + target.put(key, value); + } + } +} diff --git a/src/test/java/com/fopost/sdk/DesignsTest.java b/src/test/java/com/fopost/sdk/DesignsTest.java new file mode 100644 index 0000000..692f61b --- /dev/null +++ b/src/test/java/com/fopost/sdk/DesignsTest.java @@ -0,0 +1,254 @@ +package com.fopost.sdk; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fopost.sdk.param.DesignParams; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; + +/** Designs: paths, verbs, snake_case bodies, the document passed through verbatim, and the records. */ +class DesignsTest { + + private static final String DESIGN = "{\"id\":\"des_1\",\"name\":\"Launch card\",\"kind\":\"post\"," + + "\"width\":1080,\"height\":1080,\"document\":{\"version\":1,\"pages\":[{\"layers\":[]}]}," + + "\"thumbnail_media_id\":\"med_9\",\"thumbnail_url\":\"https://api.fopost.test/v1/media/med_9/file\"," + + "\"created_at\":\"2026-11-19T10:00:00.000Z\",\"updated_at\":\"2026-11-19T11:00:00.000Z\"}"; + + private static final String VERSION = + "{\"id\":\"ver_1\",\"version\":3,\"created_at\":\"2026-11-19T11:00:00.000Z\"}"; + + @Test + void listSendsTheFiltersAndReadsDesigns() { + FakeTransport transport = new FakeTransport().enqueue(200, "{\"data\":[" + DESIGN + "]}"); + + var designs = TestSupport.client(transport) + .designs() + .list(DesignParams.Filter.of("w1").kind("story").limit(10).offset(20)); + + assertEquals("GET", transport.last().method()); + assertEquals( + "https://api.fopost.test/v1/designs?workspace_id=w1&kind=story&limit=10&offset=20", + transport.last().url()); + assertEquals(1, designs.size()); + assertEquals("des_1", designs.get(0).id()); + assertEquals(1080, designs.get(0).width()); + assertEquals("med_9", designs.get(0).thumbnailMediaId()); + } + + @Test + void createSendsTheDocumentVerbatimWithItsNulls() { + FakeTransport transport = new FakeTransport().enqueue(201, "{\"data\":" + DESIGN + "}"); + Map layer = new LinkedHashMap<>(); + layer.put("id", "l1"); + layer.put("mediaId", null); + Map document = Map.of("pages", List.of(Map.of("layers", List.of(layer)))); + + var design = TestSupport.client(transport) + .designs() + .create(DesignParams.Create.of("w1", "post", document).name("Launch card").brandVoiceId("bv_1")); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs", transport.last().url()); + assertEquals( + "{\"workspace_id\":\"w1\",\"kind\":\"post\"," + + "\"document\":{\"pages\":[{\"layers\":[{\"id\":\"l1\",\"mediaId\":null}]}]}," + + "\"name\":\"Launch card\",\"brand_voice_id\":\"bv_1\"}", + transport.lastBody()); + assertEquals("Launch card", design.name()); + assertEquals(1, design.document().get("version")); + } + + @Test + void getReadsTheDesign() { + FakeTransport transport = new FakeTransport().enqueue(200, "{\"data\":" + DESIGN + "}"); + + var design = TestSupport.client(transport).designs().get("des_1"); + + assertEquals("GET", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/des_1", transport.last().url()); + assertEquals("post", design.kind()); + } + + @Test + void updateSendsOnlyWhatWasSetAndReadsTheVersion() { + FakeTransport transport = + new FakeTransport().enqueue(200, "{\"data\":" + DESIGN + ",\"version\":" + VERSION + "}"); + + var saved = TestSupport.client(transport) + .designs() + .update("des_1", DesignParams.Update.create().name("Renamed").createVersion(true)); + + assertEquals("PATCH", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/des_1", transport.last().url()); + assertEquals("{\"name\":\"Renamed\",\"create_version\":true}", transport.lastBody()); + assertEquals("des_1", saved.design().id()); + assertEquals(3, saved.version().version()); + } + + @Test + void updateWithoutAVersionLeavesItNull() { + FakeTransport transport = new FakeTransport().enqueue(200, "{\"data\":" + DESIGN + ",\"version\":null}"); + + var saved = TestSupport.client(transport).designs().update("des_1", DesignParams.Update.create().kind("story")); + + assertNull(saved.version()); + } + + @Test + void deleteAnswersTheDeletedId() { + FakeTransport transport = + new FakeTransport().enqueue(200, "{\"data\":{\"id\":\"des_1\",\"deleted\":true}}"); + + var deleted = TestSupport.client(transport).designs().delete("des_1"); + + assertEquals("DELETE", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/des_1", transport.last().url()); + assertTrue(deleted.deleted()); + } + + @Test + void versionsAsksForDocumentsOnlyWhenTold() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"data\":[" + VERSION + "]}") + .enqueue(200, "{\"data\":[{\"id\":\"ver_1\",\"version\":3,\"document\":{\"pages\":[]}," + + "\"created_at\":\"2026-11-19T11:00:00.000Z\"}]}"); + FoPost client = TestSupport.client(transport); + + var plain = client.designs().versions("des_1"); + assertEquals("https://api.fopost.test/v1/designs/des_1/versions", transport.last().url()); + assertNull(plain.get(0).document()); + + var full = client.designs().versions("des_1", true); + assertEquals("https://api.fopost.test/v1/designs/des_1/versions?include=document", transport.last().url()); + assertEquals(List.of(), full.get(0).document().get("pages")); + } + + @Test + void restoreVersionPostsToTheRestorePath() { + FakeTransport transport = + new FakeTransport().enqueue(200, "{\"data\":" + DESIGN + ",\"version\":" + VERSION + "}"); + + var saved = TestSupport.client(transport).designs().restoreVersion("des_1", "ver_1"); + + assertEquals("POST", transport.last().method()); + assertEquals( + "https://api.fopost.test/v1/designs/des_1/versions/ver_1/restore", transport.last().url()); + assertEquals("ver_1", saved.version().id()); + } + + @Test + void duplicateSendsTheNameOnlyWhenGiven() { + FakeTransport transport = new FakeTransport() + .enqueue(201, "{\"data\":" + DESIGN + "}") + .enqueue(201, "{\"data\":" + DESIGN + "}"); + FoPost client = TestSupport.client(transport); + + client.designs().duplicate("des_1"); + assertEquals("https://api.fopost.test/v1/designs/des_1/duplicate", transport.last().url()); + assertEquals("{}", transport.lastBody()); + + client.designs().duplicate("des_1", "Copy B"); + assertEquals("{\"name\":\"Copy B\"}", transport.lastBody()); + } + + @Test + void exportSendsFormatPagesAndDurationAndReadsTheFiles() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"data\":{\"format\":\"mp4\",\"files\":[{\"media_id\":\"med_1\"," + + "\"page_index\":0,\"mime_type\":\"video/mp4\"," + + "\"preview_url\":\"https://api.fopost.test/v1/media/med_1/file\"}]}}"); + + var export = TestSupport.client(transport) + .designs() + .export("des_1", DesignParams.Export.of("mp4").pageIndexes(List.of(0)).durationSec(10)); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/des_1/export", transport.last().url()); + assertEquals("{\"format\":\"mp4\",\"page_indexes\":[0],\"duration_sec\":10}", transport.lastBody()); + assertEquals("mp4", export.format()); + assertEquals("med_1", export.files().get(0).mediaId()); + assertEquals(0, export.files().get(0).pageIndex()); + assertEquals("video/mp4", export.files().get(0).mimeType()); + } + + @Test + void bulkSendsTheRowsAndTheNameColumn() { + FakeTransport transport = new FakeTransport().enqueue(201, "{\"data\":[" + DESIGN + "]}"); + + var created = TestSupport.client(transport) + .designs() + .bulk("des_1", List.of(Map.of("headline", "Hello")), "headline"); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/des_1/bulk", transport.last().url()); + assertEquals("{\"rows\":[{\"headline\":\"Hello\"}],\"name_column\":\"headline\"}", transport.lastBody()); + assertEquals(1, created.size()); + } + + @Test + void postsReadsThePostsUsingTheDesign() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"data\":[{\"id\":\"post_1\",\"status\":\"published\",\"title\":null," + + "\"text\":\"Launch day\",\"schedule_at\":null," + + "\"published_at\":\"2026-11-19T12:00:00.000Z\",\"platforms\":[\"instagram\"]," + + "\"engagement\":42,\"views\":null}]}"); + + var posts = TestSupport.client(transport).designs().posts("des_1"); + + assertEquals("GET", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/des_1/posts", transport.last().url()); + assertEquals("post_1", posts.get(0).id()); + assertEquals(List.of("instagram"), posts.get(0).platforms()); + assertEquals(42L, posts.get(0).engagement()); + assertNull(posts.get(0).views()); + } + + @Test + void textToSlidesSendsTheBodyAndReadsTheSlides() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"data\":{\"cover\":{\"title\":\"Five tips\",\"subtitle\":\"\"}," + + "\"slides\":[{\"heading\":\"One\",\"body\":\"First\"}]," + + "\"cta\":{\"heading\":\"Follow\",\"body\":\"More soon\"}," + + "\"brand\":{\"colors\":[\"#111111\"],\"fonts\":{\"heading\":\"Geist\",\"body\":\"Geist\"}," + + "\"logo_media_id\":null}}}"); + + var slides = TestSupport.client(transport) + .designs() + .textToSlides(DesignParams.TextToSlides.of("w1").text("Five tips for launch").maxSlides(5)); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/text-to-slides", transport.last().url()); + assertEquals( + "{\"workspace_id\":\"w1\",\"text\":\"Five tips for launch\",\"max_slides\":5}", + transport.lastBody()); + assertEquals("Five tips", slides.cover().title()); + assertEquals("One", slides.slides().get(0).heading()); + assertEquals("Follow", slides.cta().heading()); + assertEquals("Geist", slides.brand().fonts().heading()); + } + + @Test + void fromPostSendsTheBodyAndReadsTheDesign() { + FakeTransport transport = new FakeTransport().enqueue(201, "{\"data\":" + DESIGN + "}"); + + var design = TestSupport.client(transport) + .designs() + .fromPost(DesignParams.FromPost.of("w1").postId("post_1").style("post").accountId("acc_1") + .carousel(false)); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/designs/from-post", transport.last().url()); + String body = transport.lastBody(); + assertEquals( + "{\"workspace_id\":\"w1\",\"post_id\":\"post_1\",\"style\":\"post\",\"account_id\":\"acc_1\"," + + "\"carousel\":false}", + body); + assertFalse(body.contains("\"text\""), body); + assertEquals("des_1", design.id()); + } +} diff --git a/src/test/java/com/fopost/sdk/FontsTest.java b/src/test/java/com/fopost/sdk/FontsTest.java new file mode 100644 index 0000000..3d4630e --- /dev/null +++ b/src/test/java/com/fopost/sdk/FontsTest.java @@ -0,0 +1,92 @@ +package com.fopost.sdk; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fopost.sdk.param.FileInput; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** Fonts: the bare list envelope, the multipart upload fields, and delete. */ +class FontsTest { + + private static final String UPLOADED = "{\"id\":\"font_1\",\"family\":\"Brand Sans\",\"style\":\"italic\"," + + "\"weight\":700,\"format\":\"woff2\",\"file_url\":\"https://api.fopost.test/v1/fonts/font_1/file\"," + + "\"created_at\":\"2026-11-19T10:00:00.000Z\"}"; + + @Test + void listReadsTheLibraryAndTheWorkspaceFonts() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"library\":[{\"slug\":\"geist\",\"family\":\"Geist\",\"category\":\"sans\"," + + "\"scripts\":[\"latin\"],\"weights\":[\"100 900\"],\"files\":[{\"path\":\"/fonts/geist.woff2\"," + + "\"subset\":\"latin\",\"weight\":\"100 900\",\"style\":\"normal\"," + + "\"unicode_range\":\"U+0000-00FF\"}]}],\"uploaded\":[" + UPLOADED + "]}"); + + var fonts = TestSupport.client(transport).fonts().list("w1"); + + assertEquals("GET", transport.last().method()); + assertEquals("https://api.fopost.test/v1/fonts?workspace_id=w1", transport.last().url()); + assertEquals("Geist", fonts.library().get(0).family()); + assertEquals("U+0000-00FF", fonts.library().get(0).files().get(0).unicodeRange()); + assertEquals("font_1", fonts.uploaded().get(0).id()); + assertEquals(700, fonts.uploaded().get(0).weight()); + } + + @Test + void listWithoutAWorkspaceSendsNoQuery() { + FakeTransport transport = new FakeTransport().enqueue(200, "{\"library\":[],\"uploaded\":[]}"); + + var fonts = TestSupport.client(transport).fonts().list(); + + assertEquals("https://api.fopost.test/v1/fonts", transport.last().url()); + assertTrue(fonts.uploaded().isEmpty()); + } + + @Test + void uploadSendsTheFileAndTheOptionalFields() { + FakeTransport transport = new FakeTransport().enqueue(201, UPLOADED); + + var font = TestSupport.client(transport) + .fonts() + .upload("w1", FileInput.of("brand.woff2", "font/woff2", new byte[] {1, 2, 3}), "Brand Sans", 700, + "italic"); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/fonts", transport.last().url()); + assertTrue(transport.last().headers().get("Content-Type").startsWith("multipart/form-data; boundary=")); + String body = transport.lastBody(); + assertTrue(body.contains("name=\"file\"; filename=\"brand.woff2\""), body); + assertTrue(body.contains("name=\"workspace_id\"\r\n\r\nw1"), body); + assertTrue(body.contains("name=\"family\"\r\n\r\nBrand Sans"), body); + assertTrue(body.contains("name=\"weight\"\r\n\r\n700"), body); + assertTrue(body.contains("name=\"style\"\r\n\r\nitalic"), body); + assertEquals("Brand Sans", font.family()); + assertEquals("italic", font.style()); + } + + @Test + void uploadFromAPathLeavesTheDefaultsToTheServer(@TempDir Path dir) throws Exception { + Path file = Files.write(dir.resolve("brand.ttf"), new byte[] {0, 1, 0, 0}); + FakeTransport transport = new FakeTransport().enqueue(201, UPLOADED); + + TestSupport.client(transport).fonts().upload("w1", file); + + String body = transport.lastBody(); + assertTrue(body.contains("filename=\"brand.ttf\""), body); + assertFalse(body.contains("name=\"family\""), body); + assertFalse(body.contains("name=\"weight\""), body); + } + + @Test + void deleteSendsADeleteToTheFont() { + FakeTransport transport = new FakeTransport().enqueue(204, ""); + + TestSupport.client(transport).fonts().delete("font_1"); + + assertEquals("DELETE", transport.last().method()); + assertEquals("https://api.fopost.test/v1/fonts/font_1", transport.last().url()); + } +} diff --git a/src/test/java/com/fopost/sdk/MediaAltTextTest.java b/src/test/java/com/fopost/sdk/MediaAltTextTest.java new file mode 100644 index 0000000..fd5db21 --- /dev/null +++ b/src/test/java/com/fopost/sdk/MediaAltTextTest.java @@ -0,0 +1,39 @@ +package com.fopost.sdk; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; +import org.junit.jupiter.api.Test; + +/** Generated alt text: the path, the bare response, and an empty balance. */ +class MediaAltTextTest { + + @Test + void generateAltTextPostsToTheItemAndReadsTheText() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"alt_text\":\"A red bicycle against a white wall\",\"alt_text_source\":\"ai\"," + + "\"tags\":[\"bicycle\",\"red\"]}"); + + var alt = TestSupport.client(transport).media().generateAltText("med_1"); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/media/med_1/alt-text", transport.last().url()); + assertEquals("A red bicycle against a white wall", alt.altText()); + assertEquals("ai", alt.altTextSource()); + assertEquals(List.of("bicycle", "red"), alt.tags()); + } + + @Test + void anEmptyBalanceIsPaymentRequired() { + FakeTransport transport = new FakeTransport() + .enqueue(402, "{\"error\":\"insufficient_credits\",\"message\":\"Your AI balance is empty.\"," + + "\"upgrade_url\":\"/settings/billing\"}"); + + var e = assertThrows( + PaymentRequiredException.class, + () -> TestSupport.client(transport).media().generateAltText("med_1")); + + assertEquals("insufficient_credits", e.code()); + } +} diff --git a/src/test/java/com/fopost/sdk/VideoEditsTest.java b/src/test/java/com/fopost/sdk/VideoEditsTest.java new file mode 100644 index 0000000..740fe1d --- /dev/null +++ b/src/test/java/com/fopost/sdk/VideoEditsTest.java @@ -0,0 +1,111 @@ +package com.fopost.sdk; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.fopost.sdk.model.VideoEditOperation; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; + +/** Video edits: the bare job envelope, the operations passed through unchanged, and the queries. */ +class VideoEditsTest { + + private static final String JOB = "{\"id\":\"ve_1\",\"status\":\"succeeded\",\"progress\":100," + + "\"source_media_id\":\"med_1\",\"operations\":[{\"op\":\"trim\",\"params\":{\"startSec\":2,\"endSec\":12}}]," + + "\"result_media_id\":\"med_2\",\"result_media\":{\"id\":\"med_2\"," + + "\"preview_url\":\"https://api.fopost.test/v1/media/med_2/file\",\"mime_type\":\"video/mp4\"}," + + "\"extra_outputs\":[{\"media_id\":\"med_3\",\"label\":\"Subtitles\",\"mime\":\"text/vtt\",\"preview_url\":null}]," + + "\"result\":{\"language\":\"en\"},\"error\":null,\"created_at\":\"2026-11-19T10:00:00.000Z\"," + + "\"updated_at\":\"2026-11-19T10:01:00.000Z\",\"completed_at\":\"2026-11-19T10:01:00.000Z\"}"; + + @Test + void opsReadsWhatCanRun() { + FakeTransport transport = new FakeTransport() + .enqueue(200, "{\"data\":[{\"name\":\"trim\",\"available\":true,\"uses_ai\":false}," + + "{\"name\":\"subtitles\",\"available\":false,\"uses_ai\":true}]}"); + + var ops = TestSupport.client(transport).videoEdits().ops(); + + assertEquals("GET", transport.last().method()); + assertEquals("https://api.fopost.test/v1/video-edits/ops", transport.last().url()); + assertEquals("trim", ops.get(0).name()); + assertTrue(ops.get(0).available()); + assertTrue(ops.get(1).usesAi()); + assertFalse(ops.get(1).available()); + } + + @Test + void listSendsTheFiltersAndReadsJobs() { + FakeTransport transport = new FakeTransport().enqueue(200, "{\"data\":[" + JOB + "]}"); + + var jobs = TestSupport.client(transport).videoEdits().list("w1", "med_1", 5); + + assertEquals("GET", transport.last().method()); + assertEquals( + "https://api.fopost.test/v1/video-edits?workspace_id=w1&media_id=med_1&limit=5", + transport.last().url()); + assertEquals("ve_1", jobs.get(0).id()); + } + + @Test + void createSendsTheOperationsVerbatim() { + FakeTransport transport = new FakeTransport().enqueue(201, JOB); + Map subtitles = new LinkedHashMap<>(); + subtitles.put("language", null); + + var job = TestSupport.client(transport) + .videoEdits() + .create("med_1", List.of( + VideoEditOperation.of("trim", Map.of("startSec", 2)), + VideoEditOperation.of("subtitles", subtitles), + VideoEditOperation.of("denoise"))); + + assertEquals("POST", transport.last().method()); + assertEquals("https://api.fopost.test/v1/video-edits", transport.last().url()); + assertEquals( + "{\"media_id\":\"med_1\",\"operations\":[{\"op\":\"trim\",\"params\":{\"startSec\":2}}," + + "{\"op\":\"subtitles\",\"params\":{\"language\":null}},{\"op\":\"denoise\",\"params\":{}}]}", + transport.lastBody()); + assertEquals("succeeded", job.status()); + assertEquals(2, job.operations().get(0).params().get("startSec")); + } + + @Test + void createNamesTheWorkspaceWhenGiven() { + FakeTransport transport = new FakeTransport().enqueue(201, JOB); + + TestSupport.client(transport).videoEdits().create("w1", "med_1", List.of(VideoEditOperation.of("denoise"))); + + assertTrue(transport.lastBody().startsWith("{\"workspace_id\":\"w1\",\"media_id\":\"med_1\"")); + } + + @Test + void getReadsTheJobAndItsOutputs() { + FakeTransport transport = new FakeTransport().enqueue(200, JOB); + + var job = TestSupport.client(transport).videoEdits().get("ve_1"); + + assertEquals("GET", transport.last().method()); + assertEquals("https://api.fopost.test/v1/video-edits/ve_1", transport.last().url()); + assertEquals(100, job.progress()); + assertEquals("med_2", job.resultMediaId()); + assertEquals("video/mp4", job.resultMedia().mimeType()); + assertEquals("med_3", job.extraOutputs().get(0).mediaId()); + assertNull(job.extraOutputs().get(0).previewUrl()); + assertEquals("en", job.result().get("language")); + assertNull(job.error()); + } + + @Test + void getSendsTheWorkspaceWhenGiven() { + FakeTransport transport = new FakeTransport().enqueue(200, JOB); + + TestSupport.client(transport).videoEdits().get("ve_1", "w1"); + + assertEquals("https://api.fopost.test/v1/video-edits/ve_1?workspace_id=w1", transport.last().url()); + } +}