@@ -18,6 +18,36 @@ 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+ if parent is None :
29+ # No parent to upgrade, return the kwargs as is
30+ return kwargs
31+
32+ if not isinstance (parent , Tag ):
33+ raise TypeError (
34+ "`parent=` must be a Tag instance. If using a string, please use `parent_id=`"
35+ )
36+
37+ parent_id = kwargs .get ("parent_id" , None )
38+ if parent_id :
39+ raise ValueError ("Cannot provide both `parent=` and `parent_id=`" )
40+
41+ ret_kwargs = {** kwargs }
42+
43+ # Remove `parent` from ret_kwargs
44+ # and store the `parent_id` in the ret_kwargs below
45+ del ret_kwargs ["parent" ]
46+
47+ ret_kwargs ["parent_id" ] = parent ["id" ]
48+ return ret_kwargs
49+
50+
2151class Tag (Active ):
2252 """Tag resource."""
2353
@@ -135,6 +165,63 @@ def destroy(self) -> None:
135165 url = self ._ctx .url + self ._path
136166 self ._ctx .session .delete (url )
137167
168+ # Allow for every combination of `name` and (`parent` or `parent_id`)
169+ @overload
170+ def update (self , / , * , name : str = ..., parent : Tag | None = ...) -> Tag : ...
171+ @overload
172+ def update (self , / , * , name : str = ..., parent_id : str | None = ...) -> Tag : ...
173+
174+ def update ( # pyright: ignore[reportIncompatibleMethodOverride] ; This method returns `Tag`. Parent method returns `None`
175+ self ,
176+ ** kwargs ,
177+ ) -> Tag :
178+ """
179+ Update the tag.
180+
181+ Parameters
182+ ----------
183+ name : str
184+ The name of the tag.
185+ parent : Tag | None, optional
186+ The parent `Tag` object. If there is no parent, the tag is a top-level tag. To remove
187+ the parent tag, set the value to `None`. Only one of `parent` or `parent_id` can be
188+ provided.
189+ parent_id : str | None, optional
190+ The identifier for the parent tag. If there is no parent, the tag is a top-level tag.
191+ To remove the parent tag, set the value to `None`.
192+
193+ Returns
194+ -------
195+ Tag
196+ Updated tag object.
197+
198+ Examples
199+ --------
200+ ```python
201+ import posit
202+
203+ client = posit.connect.Client()
204+ last_tag = client.tags.find()[-1]
205+
206+ # Update the tag's name
207+ updated_tag = last_tag.update(name="new_name")
208+
209+ # Remove the tag's parent
210+ updated_tag = last_tag.update(parent=None)
211+ updated_tag = last_tag.update(parent_id=None)
212+
213+ # Update the tag's parent
214+ parent_tag = client.tags.find()[0]
215+ updated_tag = last_tag.update(parent=parent_tag)
216+ updated_tag = last_tag.update(parent_id=parent_tag["id"])
217+ ```
218+ """
219+ updated_kwargs = _update_parent_kwargs (kwargs )
220+ url = self ._ctx .url + self ._path
221+ response = self ._ctx .session .patch (url , json = updated_kwargs )
222+ result = response .json ()
223+ return Tag (self ._ctx , self ._path , ** result )
224+
138225
139226class TagContentItems (ContextManager ):
140227 def __init__ (self , ctx : Context , path : str ) -> None :
@@ -303,35 +390,6 @@ def get(self, tag_id: str) -> Tag:
303390 response = self ._ctx .session .get (url )
304391 return Tag (self ._ctx , path , ** response .json ())
305392
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-
335393 # Allow for every combination of `name` and (`parent` or `parent_id`)
336394 @overload
337395 def find (self , / , * , name : str = ..., parent : Tag = ...) -> list [Tag ]: ...
@@ -379,7 +437,7 @@ def find(self, /, **kwargs) -> list[Tag]:
379437 subtags = client.tags.find(name="sub_name", parent=mytag["id"])
380438 ```
381439 """
382- updated_kwargs = self . _update_parent_kwargs (
440+ updated_kwargs = _update_parent_kwargs (
383441 kwargs , # pyright: ignore[reportArgumentType]
384442 )
385443 url = self ._ctx .url + self ._path
@@ -425,7 +483,7 @@ def create(self, /, **kwargs) -> Tag:
425483 tag = client.tags.create(name="tag_name", parent=category_tag)
426484 ```
427485 """
428- updated_kwargs = self . _update_parent_kwargs (
486+ updated_kwargs = _update_parent_kwargs (
429487 kwargs , # pyright: ignore[reportArgumentType]
430488 )
431489
0 commit comments