1+ import base64
12import datetime
23import logging
34import os
3536_TB_HOST = "https://taskbadger.net"
3637
3738
39+ def _parse_token (token ):
40+ """Try to decode a project API key.
41+
42+ Project keys are base64-encoded strings in the format ``org/project/key``.
43+
44+ Returns:
45+ A tuple of ``(organization_slug, project_slug, api_key)`` if *token*
46+ is a valid project key, otherwise ``None``.
47+ """
48+ try :
49+ decoded = base64 .b64decode (token , validate = True ).decode ("utf-8" )
50+ except Exception :
51+ return None
52+
53+ parts = decoded .split ("/" )
54+ if len (parts ) == 3 and all (parts ):
55+ return tuple (parts )
56+ return None
57+
58+
3859def init (
3960 organization_slug : str = None ,
4061 project_slug : str = None ,
@@ -43,9 +64,16 @@ def init(
4364 tags : dict [str , str ] = None ,
4465 before_create : Callback = None ,
4566):
46- """Initialize Task Badger client
67+ """Initialize Task Badger client.
68+
69+ If *token* is a project API key (base64-encoded ``org/project/key``),
70+ the organization and project slugs are extracted automatically and
71+ *organization_slug* / *project_slug* are ignored.
4772
48- Call this function once per thread
73+ For legacy API keys, *organization_slug* and *project_slug* are
74+ required and a deprecation warning is emitted.
75+
76+ Call this function once per thread.
4977 """
5078 _init (_TB_HOST , organization_slug , project_slug , token , systems , tags , before_create )
5179
@@ -64,6 +92,17 @@ def _init(
6492 project_slug = project_slug or os .environ .get ("TASKBADGER_PROJECT" )
6593 token = token or os .environ .get ("TASKBADGER_API_KEY" )
6694
95+ if token :
96+ parsed = _parse_token (token )
97+ if parsed :
98+ organization_slug , project_slug , token = parsed
99+ else :
100+ warnings .warn (
101+ "Legacy API keys are deprecated. Please switch to a project API key." ,
102+ DeprecationWarning ,
103+ stacklevel = 3 ,
104+ )
105+
67106 if before_create and isinstance (before_create , str ):
68107 try :
69108 before_create = import_string (before_create )
0 commit comments