diff --git a/src/gradient_labs/_article_set_status.py b/src/gradient_labs/_article_set_status.py index bc674ea..c2e0241 100644 --- a/src/gradient_labs/_article_set_status.py +++ b/src/gradient_labs/_article_set_status.py @@ -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 diff --git a/src/gradient_labs/_article_topic_upsert.py b/src/gradient_labs/_article_topic_upsert.py index 1165e6b..35c9650 100644 --- a/src/gradient_labs/_article_topic_upsert.py +++ b/src/gradient_labs/_article_topic_upsert.py @@ -7,7 +7,6 @@ from marshmallow import fields from .article import Visibility, PublicationStatus - from ._http_client import HttpClient @@ -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 @@ -51,6 +44,12 @@ 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)) @@ -58,4 +57,15 @@ class ArticleTopicUpsertParams: 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, + ) diff --git a/src/gradient_labs/_article_upsert.py b/src/gradient_labs/_article_upsert.py index 60d4666..2d397eb 100644 --- a/src/gradient_labs/_article_upsert.py +++ b/src/gradient_labs/_article_upsert.py @@ -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 @@ -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, ) diff --git a/src/gradient_labs/errors.py b/src/gradient_labs/errors.py index 7b7cefe..497e567 100644 --- a/src/gradient_labs/errors.py +++ b/src/gradient_labs/errors.py @@ -1,4 +1,5 @@ -from json import JSONDecodeError +from requests.exceptions import JSONDecodeError + class ResponseError(Exception): """