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
11 changes: 3 additions & 8 deletions src/gradient_labs/_article_set_status.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from typing import Optional, Any
from collections import defaultdict
from datetime import datetime
from dataclasses import dataclass
from dataclasses_json import dataclass_json

from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from marshmallow import fields

from .article import Visibility, PublicationStatus, ArticleUsageStatus
from .article import ArticleUsageStatus
from ._http_client import HttpClient


Expand Down
26 changes: 18 additions & 8 deletions src/gradient_labs/_article_topic_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from marshmallow import fields

from .article import Visibility, PublicationStatus

from ._http_client import HttpClient


Expand All @@ -17,15 +16,9 @@ class ArticleTopicUpsertParams:
# id is your identifier for this topic
id: str

# parent_id is the identifier for this topic's parent topic (if any).
parent_id: str

# name is the topic's name. This cannot be empty.
name: str

# description is an topic's tagline. It may be empty.
description: str

# visibility describes who can see this topic, ranging from the
# whole world (public) through to employees only (internal).
visibility: Visibility
Expand All @@ -51,11 +44,28 @@ class ArticleTopicUpsertParams:
)
)

# description is an topic's tagline. It may be empty.
description: Optional[str] = None

# parent_id is the identifier for this topic's parent topic (if any).
parent_id: Optional[str] = None

# data optionally gives additional meta-data about the topic.
data: Optional[dict] = field(default_factory=lambda: defaultdict(dict))


def upsert_article_topic(
*, client: HttpClient, params: ArticleTopicUpsertParams
) -> None:
_ = client.post(path="topics", body=params.to_dict())
body = params.to_dict()
if params.parent_id is None:
body.pop("parent_id")
if params.description is None:
body.pop("description")

body["created"] = HttpClient.localize(params.created)
body["last_edited"] = HttpClient.localize(params.last_edited)
_ = client.post(
path="topics",
body=body,
)
49 changes: 32 additions & 17 deletions src/gradient_labs/_article_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,14 @@
@dataclass_json
@dataclass(frozen=True)
class UpsertArticleParams:
# author_id optionally identifies the user who last edited the article
author_id: str

# id is your identifier of choice for this article.
id: str

# title is the article's title. It may be empty if the article is a draft.
title: str

# description is an article's tagline. It may be empty.
description: str

# body is the main contents of an article. It may be empty if the article is a draft.
body: str

# visibility describes who can access this article, ranging from the
# whole world (public) through to employees only (internal).
visibility: Visibility

# topic_id optionally identifies the topic that this
# article is associated with. If given, you must have created
# the topic first (see: UpsertArticleTopic)
topic_id: str

# status describes whether this article is published or not.
status: PublicationStatus

Expand All @@ -58,12 +42,43 @@ class UpsertArticleParams:
)
)

# topic_id optionally identifies the topic that this
# article is associated with. If given, you must have created
# the topic first (see: UpsertArticleTopic)
topic_id: Optional[str] = None

# author_id optionally identifies the user who last edited the article
author_id: Optional[str] = None

# title is the article's title. It may be empty if the article is a draft.
title: Optional[str] = None

# description is an article's tagline. It may be empty.
description: Optional[str] = None

# body is the main contents of an article. It may be empty if the article is a draft.
body: Optional[str] = None

# data optionally gives additional meta-data about the article.
data: Optional[Any] = field(default_factory=lambda: defaultdict(dict))


def upsert_article(*, client: HttpClient, params: UpsertArticleParams) -> None:
body = params.to_dict()
if params.title is None:
body.pop("title")
if params.description is None:
body.pop("description")
if params.topic_id is None:
body.pop("topic_id")
if params.author_id is None:
body.pop("author_id")
if params.body is None:
body.pop("body")

body["created"] = HttpClient.localize(params.created)
body["last_edited"] = HttpClient.localize(params.last_edited)
_ = client.post(
path="articles",
body=params.to_dict(),
body=body,
)
3 changes: 2 additions & 1 deletion src/gradient_labs/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from json import JSONDecodeError
from requests.exceptions import JSONDecodeError


class ResponseError(Exception):
"""
Expand Down