GhostAdminClient

Documentation for eth_defi.vault_report.ghost.GhostAdminClient Python class.

class GhostAdminClient

Bases: object

Upload images and manage draft posts with the Ghost Admin API.

Create an Admin API client after checking the key format.

The key is validated before any request, so a Content API key or a malformed key fails early without being sent or printed.

Parameters
  • api_url – Ghost site API URL, e.g. https://example.ghost.io.

  • admin_api_key – Admin API key or staff access token in {id}:{secret} format, see check_ghost_admin_api_key().

  • timeout – HTTP timeout in seconds.

Raises

GhostAPIError – The key cannot authenticate to the Admin API.

Methods summary

__init__(api_url, admin_api_key[, timeout])

Create an Admin API client after checking the key format.

create_or_update_draft(title, slug, html[, ...])

Create a draft post, or replace the body of an existing draft with the same slug.

delete_post(post_id)

Delete a post.

fetch_post_by_slug(slug)

Fetch a post in any status by its slug.

fetch_writable_draft(slug, *[, ...])

Fetch the existing post for a slug and check a draft can be written there without losing work.

get_editor_url(post)

Get the Ghost Admin editor link for a post.

upload_image(path)

Upload an image to the Ghost media library.

__init__(api_url, admin_api_key, timeout=60.0)

Create an Admin API client after checking the key format.

The key is validated before any request, so a Content API key or a malformed key fails early without being sent or printed.

Parameters
  • api_url (str) – Ghost site API URL, e.g. https://example.ghost.io.

  • admin_api_key (str) – Admin API key or staff access token in {id}:{secret} format, see check_ghost_admin_api_key().

  • timeout (float) – HTTP timeout in seconds.

Raises

GhostAPIError – The key cannot authenticate to the Admin API.

Return type

None

upload_image(path)

Upload an image to the Ghost media library.

The report bundle references charts and podcast images by relative paths, which only work in the local preview. Before the draft is created, publish_report_draft() uploads each image here and rebuilds the post with the returned storage.ghost.io URLs, so the draft is self-contained in Ghost. See the Admin API images endpoint.

Each run uploads new copies, because the media library has no de-duplication by content; replacing a draft therefore leaves the previous run’s images in the library.

Parameters

path (pathlib.Path) – Local image file. Ghost resizes uploads, so it must be a real image: a hand-made 1×1 PNG is rejected with “Unable to manipulate image”.

Returns

Public URL of the uploaded image.

Return type

str

fetch_post_by_slug(slug)

Fetch a post in any status by its slug.

Parameters

slug (str) – Post slug.

Returns

The post, or None if it does not exist.

Return type

Optional[eth_defi.vault_report.ghost.GhostPost]

delete_post(post_id)

Delete a post.

Parameters

post_id (str) – Ghost post id.

Return type

None

fetch_writable_draft(slug, *, overwrite_draft=False, last_write=None, force=False, existing_slug=None)

Fetch the existing post for a slug and check a draft can be written there without losing work.

An existing draft is replaced only with overwrite_draft, and only when last_write shows nobody has edited it since the pipeline wrote it, see DraftRecord.matches(). force skips that comparison.

Parameters
  • slug (str) – Post slug.

  • overwrite_draft (bool) – Allow replacing an existing draft.

  • last_write (Optional[eth_defi.vault_report.ghost.DraftRecord]) – What the pipeline last wrote to this draft.

  • force (bool) – Replace an existing draft without comparing it with last_write.

  • existing_slug (Optional[str]) – Explicit draft to update and rename, e.g. when moving a waiting September draft to October. Must exist; the new slug must be free.

Returns

The existing draft to replace, or None if the slug is free.

Raises

GhostAPIError – A post with the slug has been published or scheduled, or a draft exists and may have been edited.

Return type

Optional[eth_defi.vault_report.ghost.GhostPost]

create_or_update_draft(title, slug, html, custom_excerpt=None, tags=None, *, overwrite_draft=False, last_write=None, force=False, existing_slug=None)

Create a draft post, or replace the body of an existing draft with the same slug.

See fetch_writable_draft() for when an existing post is replaced.

The body is sent with ?source=html and Ghost converts it to its editor format. Content wrapped in <!--kg-card-begin: html--> / <!--kg-card-end: html--> comments becomes a raw HTML card; the previous report posts store their tables the same way.

Parameters
  • title (str) – Post title.

  • slug (str) – Post URL slug.

  • html (str) – Post body.

  • custom_excerpt (Optional[str]) – Post excerpt.

  • tags (Optional[list[str]]) – Tag names.

  • overwrite_draft (bool) – Replace the body of an existing draft with the same slug.

  • last_write (Optional[eth_defi.vault_report.ghost.DraftRecord]) – What the pipeline last wrote to the draft, see fetch_writable_draft().

  • force (bool) – Replace an existing draft without comparing it with last_write.

  • existing_slug (Optional[str]) – Explicit existing draft slug to update in place while changing its slug to slug. Preserves the post id and editor link.

Returns

The created or updated draft.

Return type

eth_defi.vault_report.ghost.GhostPost

get_editor_url(post)

Get the Ghost Admin editor link for a post.

Parameters

post (eth_defi.vault_report.ghost.GhostPost) – Post.

Returns

Editor URL.

Return type

str