Exemplo n.º 1
0
def _case_insensitive_dict_init_hot_patch(
    obj: CaseInsensitiveDict,
    *args: Any,
    **kw: Any,
) -> None:
    super(CaseInsensitiveDict, obj).__init__(*args,
                                             *kw)  # noqa: WPS608, WPS613
    obj_copy = super(CaseInsensitiveDict, obj).copy()  # noqa: WPS608, WPS613
    obj.itemlist = {}
    for key, value in obj_copy.items():
        if key != key.lower():
            obj[key.lower()] = value
            obj.pop(key, None)
Exemplo n.º 2
0
    def _load(
        self,
        url: str,
        headers=CaseInsensitiveDict(),
        params: Optional[Dict[str, str]] = None,
        path: Optional[str] = None,
    ):
        """Load a resource.

        Args:
            url (str): url
            headers (Optional[CaseInsensitiveDict]): headers. Defaults to CaseInsensitiveDict().
            params (Optional[Dict[str,str]]): params to get request. Defaults to None.
            path (Optional[str]): field to get. Defaults to None.

        Raises:
            ValueError: If json cannot be loaded
        """
        r = self._session.get(url, headers=headers, params=params)
        try:
            j = json_loads(r)
        except ValueError as e:
            logging.error(f"{e}:\n{r.text}")
            raise e
        if path:
            j = j[path]
        self._parse_raw(j)
Exemplo n.º 3
0
 def _load(self, url, headers=CaseInsensitiveDict(), params=None, path=None):
     r = self._session.get(url, headers=headers, params=params)
     try:
         j = json_loads(r)
     except ValueError as e:
         logging.error("%s:\n%s" % (e, r.text))
         raise e
     if path:
         j = j[path]
     self._parse_raw(j)
Exemplo n.º 4
0
    def _load(self, url, headers=CaseInsensitiveDict(), params=None, path=None):
        """ Load a resource.

        :type url: str
        :type headers: CaseInsensitiveDict
        :type params: Optional[Dict[str,str]]
        :type path: Optional[str]

        """
        r = self._session.get(url, headers=headers, params=params)
        try:
            j = json_loads(r)
        except ValueError as e:
            logging.error("%s:\n%s" % (e, r.text))
            raise e
        if path:
            j = j[path]
        self._parse_raw(j)
Exemplo n.º 5
0
class OrganizationAdapter(Component):
    _name = 'jira.organization.adapter'
    _inherit = ['jira.webservice.adapter']
    _apply_on = ['jira.organization']

    # The Service Desk REST API returns an error if this header
    # is not used. The API may change so they want an agreement for
    # the client about this.
    _desk_headers = CaseInsensitiveDict({'X-ExperimentalApi': 'opt-in'
                                         }) if (CaseInsensitiveDict) else None

    _desk_api_path_base = '{server}/rest/servicedeskapi/{path}'

    def __init__(self, work_context):
        super().__init__(work_context)
        self.client._session.headers.update(self._desk_headers)

    def read(self, id_):
        organization = Organization(self.client._options, self.client._session)
        with self.handle_404():
            organization.find(id_)
        return organization.raw

    def search(self):
        # A GET on the REST API returns only one page with the
        # first 50 rows. Fetch all pages.
        orgs = []
        start = 0
        while True:
            result = self.client._get_json(
                'organization',
                params={
                    'start': start,
                    # 50 items per page is the maximum allowed by Jira
                    'limit': start + 50,
                },
                base=self._desk_api_path_base,
            )
            start += 50
            orgs += result['values']
            if result['isLastPage']:
                break

        return orgs
def _add_attachment(issue: Issue, attachment: IO, filename: str):
    """
    JIRA.add_attachment is broken, because of improper use of CaseInsensitiveDict. Therefore, reimplement it.
    The fix is to only use lower-cased keys with CaseInsensitiveDict.
    """

    from jira.utils import CaseInsensitiveDict
    from requests_toolbelt import MultipartEncoder

    self = issue

    url = self._get_url('issue/' + str(self) + '/attachments')

    def file_stream():
        return MultipartEncoder(
            fields={
                'file': (filename, attachment, 'application/octet-stream')})

    m = file_stream()
    r = self._session.post(
        url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token'.lower(): 'nocheck'}), retry_data=file_stream)
Exemplo n.º 7
0
class OrganizationAdapter(Component):
    _name = 'jira.organization.adapter'
    _inherit = ['jira.webservice.adapter']
    _apply_on = ['jira.organization']

    # The Service Desk REST API returns an error if this header
    # is not used. The API may change so they want an agreement for
    # the client about this.
    _desk_headers = CaseInsensitiveDict({'X-ExperimentalApi': 'opt-in'})

    def __init__(self, work_context):
        super().__init__(work_context)
        self.client._session.headers.update(self._desk_headers)

    def read(self, id_):
        organization = Organization(
            self.client._options,
            self.client._session
        )
        organization.find(id_)
        return organization.raw

    def search(self):
        base = (self.client._options['server'] +
                '/rest/servicedeskapi/organization')
        # By default, a GET on the REST API returns only one page with the
        # first 50 rows. Here, client is an instance of the jira library's JIRA
        # class, which provides a _fetch_pages method to fetch pages.
        # maxResults=False means it will try to get all pages.
        orgs = self.client._fetch_pages(
            Organization,
            'values',
            'organization',
            # limit to False will get them in batch
            maxResults=False,
            base=base
        )
        return [org.id for org in orgs]
Exemplo n.º 8
0
 def _default_headers(self, user_headers):
     # result = dict(user_headers)
     # result['accept'] = 'application/json'
     return CaseInsensitiveDict(self._options['headers'].items() +
                                user_headers.items())
Exemplo n.º 9
0
    def find(self, id, params=None, headers=CaseInsensitiveDict()):
>>>>>>> pr388