From b9d496fe0404171cca71074dc4cf815d429b8a78 Mon Sep 17 00:00:00 2001 From: Mitchell Kotler Date: Wed, 15 Nov 2023 11:15:59 -0500 Subject: [PATCH 1/2] add charge_credits --- documentcloud/addon.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/documentcloud/addon.py b/documentcloud/addon.py index 7d8d91f..4798ea5 100644 --- a/documentcloud/addon.py +++ b/documentcloud/addon.py @@ -42,6 +42,8 @@ def __init__(self): self.org_id = args.pop("organization", None) # add on specific data self.data = args.pop("data", None) + # title of the addon + self.title = args.pop("title", None) def _create_client(self, args): client_kwargs = { @@ -120,6 +122,8 @@ def _parse_arguments(self): with open("config.yaml") as config: schema = yaml.safe_load(config) args["data"] = fastjsonschema.validate(schema, args["data"]) + # add title in case the add-on wants to reference its own title + args["title"] = schema.get("title") except FileNotFoundError: pass except fastjsonschema.JsonSchemaException as exc: @@ -214,6 +218,29 @@ def get_documents(self): yield from documents + def charge_credits(self, amount): + """Charge the organization a certain amount of premium credits""" + + if not self.id: + print(f"Charge credits: {amount}") + return None + elif not self.org_id: + self.set_message("No organization to charge.") + raise ValueError + + resp = self.client.post( + f"organizations/{self.org_id}/ai_credits/", + json={ + "ai_credits": amount, + "addonrun_id": self.id, + "note": f"AddOn run: {self.title} - {self.id}", + }, + ) + if resp.status_code != 200: + self.set_message("Error charging AI credits.") + raise ValueError + return resp + class CronAddOn(BaseAddOn): """DEPREACTED""" From 2cc59f186631efcbfaeabf6c00b801597fe93e35 Mon Sep 17 00:00:00 2001 From: Mitchell Kotler Date: Wed, 29 Nov 2023 10:43:58 -0500 Subject: [PATCH 2/2] bump version --- docs/changelog.rst | 7 ++++++- setup.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 422e406..2939d48 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,9 +1,14 @@ Changelog --------- +3.8.0 +~~~~~ +* Add `charge_credits` to `AddOn` + + 3.7.1 ~~~~~ -* Fixes an indentation bug present in 3.7.1 +* Fixes an indentation bug present in 3.7.0 3.7.0 diff --git a/setup.py b/setup.py index 153562a..db1590c 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name="python-documentcloud", - version="3.7.0", + version="3.8.0", description="A simple Python wrapper for the DocumentCloud API", author="Mitchell Kotler", author_email="mitch@muckrock.com",