1010import random
1111import time
1212import typing as t
13+ import urllib .parse
1314
1415from polarion_rest_api_client import base_client
1516from polarion_rest_api_client import data_models as dm
1617from polarion_rest_api_client import errors
1718from polarion_rest_api_client .open_api_client import client as oa_client
1819from polarion_rest_api_client .open_api_client import models as api_models
1920from polarion_rest_api_client .open_api_client import types as oa_types
21+ from polarion_rest_api_client .open_api_client .api .documents import get_document
2022from polarion_rest_api_client .open_api_client .api .linked_work_items import (
2123 delete_linked_work_items ,
2224 get_linked_work_items ,
@@ -611,16 +613,20 @@ def get_work_items(
611613 self ._work_item (
612614 work_item_id ,
613615 unset_str_builder (work_item .attributes .title ),
614- unset_str_builder (
615- work_item .attributes .description .type
616- )
617- if work_item .attributes .description
618- else None ,
619- unset_str_builder (
620- work_item .attributes .description .value
621- )
622- if work_item .attributes .description
623- else None ,
616+ (
617+ unset_str_builder (
618+ work_item .attributes .description .type
619+ )
620+ if work_item .attributes .description
621+ else None
622+ ),
623+ (
624+ unset_str_builder (
625+ work_item .attributes .description .value
626+ )
627+ if work_item .attributes .description
628+ else None
629+ ),
624630 unset_str_builder (work_item .attributes .type ),
625631 unset_str_builder (work_item .attributes .status ),
626632 work_item .attributes .additional_properties ,
@@ -635,6 +641,89 @@ def get_work_items(
635641
636642 return work_items , next_page
637643
644+ def get_document (
645+ self ,
646+ space_id : str ,
647+ document_name : str ,
648+ fields : dict [str , str ] | None = None ,
649+ include : str | None = None ,
650+ revision : str | None = None ,
651+ retry : bool = True ,
652+ ) -> dm .Document :
653+ """Return the document with the given document_name and space_id."""
654+
655+ if " " in space_id or " " in document_name :
656+ space_id = urllib .parse .quote (
657+ space_id , safe = "/" , encoding = None , errors = None
658+ )
659+ document_name = urllib .parse .quote (
660+ document_name , safe = "/" , encoding = None , errors = None
661+ )
662+ if fields is None :
663+ fields = self .default_fields .documents
664+
665+ sparse_fields = _build_sparse_fields (fields )
666+ response = get_document .sync_detailed (
667+ self .project_id ,
668+ space_id ,
669+ document_name ,
670+ client = self .client ,
671+ fields = sparse_fields ,
672+ include = include ,
673+ revision = revision ,
674+ )
675+
676+ if not self ._check_response (response , not retry ) and retry :
677+ sleep_random_time ()
678+ return self .get_document (
679+ space_id , document_name , fields , include , revision , False
680+ )
681+
682+ document_response = response .parsed
683+
684+ if isinstance (
685+ document_response , api_models .DocumentsSingleGetResponse
686+ ) and (data := document_response .data ):
687+ if not getattr (data .meta , "errors" , []):
688+ assert (attributes := data .attributes )
689+ assert isinstance (data .id , str )
690+ home_page_content = self ._handle_home_page_content (
691+ attributes .home_page_content
692+ )
693+
694+ document : dm .Document = dm .Document (
695+ id = data .id ,
696+ module_folder = unset_str_builder (attributes .module_folder ),
697+ module_name = unset_str_builder (attributes .module_name ),
698+ type = unset_str_builder (attributes .type ),
699+ status = unset_str_builder (attributes .status ),
700+ home_page_content = home_page_content ,
701+ )
702+ return document
703+
704+ def _handle_home_page_content (
705+ self ,
706+ home_page_content : api_models .DocumentsSingleGetResponseDataAttributesHomePageContent
707+ | oa_types .Unset ,
708+ ) -> dm .TextContent | None :
709+ if isinstance (home_page_content , oa_types .Unset ):
710+ return None
711+
712+ home_page_content_type = None
713+ home_page_content_value = None
714+
715+ if isinstance (
716+ home_page_content .type ,
717+ api_models .DocumentsSingleGetResponseDataAttributesHomePageContentType ,
718+ ):
719+ home_page_content_type = str (home_page_content .type )
720+ if isinstance (home_page_content .value , str ):
721+ home_page_content_value = home_page_content .value
722+ return dm .TextContent (
723+ type = home_page_content_type ,
724+ value = home_page_content_value ,
725+ )
726+
638727 def create_work_items (self , work_items : list [base_client .WorkItemType ]):
639728 """Create the given list of work items."""
640729 current_batch = api_models .WorkitemsListPostRequest ([])
0 commit comments