@@ -18,6 +18,32 @@ def find(self) -> list[Tag]:
1818 pass
1919
2020
21+ def _update_parent_kwargs (kwargs : dict ) -> dict :
22+ """
23+ Sets the `parent_id` key in the kwargs if `parent` is provided.
24+
25+ Asserts that the `parent=` and `parent_id=` keys are not both provided.
26+ """
27+ parent = kwargs .get ("parent" , None )
28+ parent_id = kwargs .get ("parent_id" , None )
29+ if parent is None :
30+ # No parent to upgrade, return the kwargs as is
31+ return kwargs
32+ if parent and parent_id :
33+ raise ValueError ("Cannot provide both `parent=` and `parent_id=`" )
34+ if not isinstance (parent , Tag ):
35+ raise TypeError (
36+ "`parent=` must be a Tag instance. If using a string, please use `parent_id=`"
37+ )
38+
39+ # Remove `parent` from a copy of `kwargs` and replace it with `parent_id`
40+ ret_kwargs = {** kwargs }
41+ del ret_kwargs ["parent" ]
42+ ret_kwargs ["parent_id" ] = parent ["id" ]
43+
44+ return ret_kwargs
45+
46+
2147class Tag (Active ):
2248 """Tag resource."""
2349
@@ -135,6 +161,63 @@ def destroy(self) -> None:
135161 url = self ._ctx .url + self ._path
136162 self ._ctx .session .delete (url )
137163
164+ # Allow for every combination of `name` and (`parent` or `parent_id`)
165+ @overload
166+ def update (self , / , * , name : str = ..., parent : Tag | None = ...) -> Tag : ...
167+ @overload
168+ def update (self , / , * , name : str = ..., parent_id : str | None = ...) -> Tag : ...
169+
170+ def update ( # pyright: ignore[reportIncompatibleMethodOverride] ; This method returns `Tag`. Parent method returns `None`
171+ self ,
172+ ** kwargs ,
173+ ) -> Tag :
174+ """
175+ Update the tag.
176+
177+ Parameters
178+ ----------
179+ name : str
180+ The name of the tag.
181+ parent : Tag | None, optional
182+ The parent `Tag` object. If there is no parent, the tag is a top-level tag. To remove
183+ the parent tag, set the value to `None`. Only one of `parent` or `parent_id` can be
184+ provided.
185+ parent_id : str | None, optional
186+ The identifier for the parent tag. If there is no parent, the tag is a top-level tag.
187+ To remove the parent tag, set the value to `None`.
188+
189+ Returns
190+ -------
191+ Tag
192+ Updated tag object.
193+
194+ Examples
195+ --------
196+ ```python
197+ import posit
198+
199+ client = posit.connect.Client()
200+ last_tag = client.tags.find()[-1]
201+
202+ # Update the tag's name
203+ updated_tag = last_tag.update(name="new_name")
204+
205+ # Remove the tag's parent
206+ updated_tag = last_tag.update(parent=None)
207+ updated_tag = last_tag.update(parent_id=None)
208+
209+ # Update the tag's parent
210+ parent_tag = client.tags.find()[0]
211+ updated_tag = last_tag.update(parent=parent_tag)
212+ updated_tag = last_tag.update(parent_id=parent_tag["id"])
213+ ```
214+ """
215+ updated_kwargs = _update_parent_kwargs (kwargs )
216+ url = self ._ctx .url + self ._path
217+ response = self ._ctx .session .patch (url , json = updated_kwargs )
218+ result = response .json ()
219+ return Tag (self ._ctx , self ._path , ** result )
220+
138221
139222class TagContentItems (ContextManager ):
140223 def __init__ (self , ctx : Context , path : str ) -> None :
@@ -303,35 +386,6 @@ def get(self, tag_id: str) -> Tag:
303386 response = self ._ctx .session .get (url )
304387 return Tag (self ._ctx , path , ** response .json ())
305388
306- def _update_parent_kwargs (self , kwargs : dict ) -> dict :
307- """
308- Sets the `parent_id` key in the kwargs if `parent` is provided.
309-
310- Asserts that the `parent=` and `parent_id=` keys are not both provided.
311- """
312- parent = kwargs .get ("parent" , None )
313- if parent is None :
314- # No parent to upgrade, return the kwargs as is
315- return kwargs
316-
317- if not isinstance (parent , Tag ):
318- raise TypeError (
319- "`parent=` must be a Tag instance. If using a string, please use `parent_id=`"
320- )
321-
322- parent_id = kwargs .get ("parent_id" , None )
323- if parent_id :
324- raise ValueError ("Cannot provide both `parent=` and `parent_id=`" )
325-
326- ret_kwargs = {** kwargs }
327-
328- # Remove `parent` from ret_kwargs
329- # and store the `parent_id` in the ret_kwargs below
330- del ret_kwargs ["parent" ]
331-
332- ret_kwargs ["parent_id" ] = parent ["id" ]
333- return ret_kwargs
334-
335389 # Allow for every combination of `name` and (`parent` or `parent_id`)
336390 @overload
337391 def find (self , / , * , name : str = ..., parent : Tag = ...) -> list [Tag ]: ...
@@ -379,7 +433,7 @@ def find(self, /, **kwargs) -> list[Tag]:
379433 subtags = client.tags.find(name="sub_name", parent=mytag["id"])
380434 ```
381435 """
382- updated_kwargs = self . _update_parent_kwargs (
436+ updated_kwargs = _update_parent_kwargs (
383437 kwargs , # pyright: ignore[reportArgumentType]
384438 )
385439 url = self ._ctx .url + self ._path
@@ -425,7 +479,7 @@ def create(self, /, **kwargs) -> Tag:
425479 tag = client.tags.create(name="tag_name", parent=category_tag)
426480 ```
427481 """
428- updated_kwargs = self . _update_parent_kwargs (
482+ updated_kwargs = _update_parent_kwargs (
429483 kwargs , # pyright: ignore[reportArgumentType]
430484 )
431485
0 commit comments