Skip to content

Commit

Permalink
use requests.structures.CaseInsensitiveDict directly (pycontribs#1084)
Browse files Browse the repository at this point in the history
* use requests.structures.CaseInsensitiveDict directly

* add tests for attachment using filename
  • Loading branch information
adehad authored and svermeulen committed Oct 31, 2021
1 parent 1cfe8e1 commit b9ca82c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
("py:class", "Request"),
("py:class", "requests.models.Response"),
("py:class", "requests.sessions.Session"),
("py:class", "requests.structures.CaseInsensitiveDict"),
("py:class", "Response"),
("py:mod", "requests-kerberos"),
("py:mod", "requests-oauthlib"),
Expand Down
3 changes: 2 additions & 1 deletion jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from pkg_resources import parse_version
from requests import Response
from requests.auth import AuthBase
from requests.structures import CaseInsensitiveDict
from requests.utils import get_netrc_auth

from jira import __version__
Expand Down Expand Up @@ -86,7 +87,7 @@
Watchers,
Worklog,
)
from jira.utils import CaseInsensitiveDict, json_loads, threaded_requests
from jira.utils import json_loads, threaded_requests

try:
# noinspection PyUnresolvedReferences
Expand Down
3 changes: 2 additions & 1 deletion jira/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union, cast

from requests import Response
from requests.structures import CaseInsensitiveDict

from jira.resilientsession import ResilientSession
from jira.utils import CaseInsensitiveDict, json_loads, threaded_requests
from jira.utils import json_loads, threaded_requests

if TYPE_CHECKING:
from jira.client import JIRA
Expand Down
37 changes: 10 additions & 27 deletions jira/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# -*- coding: utf-8 -*-
"""Jira utils used internally."""
import threading
import warnings
from typing import Any, Optional, cast

from requests import Response
from requests.structures import CaseInsensitiveDict as _CaseInsensitiveDict

from jira.resilientsession import raise_on_error


class CaseInsensitiveDict(dict):
class CaseInsensitiveDict(_CaseInsensitiveDict):
"""A case-insensitive ``dict``-like object.
DEPRECATED: use requests.structures.CaseInsensitiveDict directly.
Implements all methods and operations of
``collections.MutableMapping`` as well as dict's ``copy``. Also
provides ``lower_items``.
Expand All @@ -36,32 +40,11 @@ class CaseInsensitiveDict(dict):
"""

def __init__(self, *args, **kw):
super(CaseInsensitiveDict, self).__init__(*args, **kw)

self.itemlist = {}
for key, value in super(CaseInsensitiveDict, self).copy().items():
if key != key.lower():
self[key.lower()] = value
self.pop(key, None)

# self.itemlist[key.lower()] = value

def __setitem__(self, key, value):
"""Overwrite [] implementation."""
super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)

# def __iter__(self):
# return iter(self.itemlist)

# def keys(self):
# return self.itemlist

# def values(self):
# return [self[key] for key in self]

# def itervalues(self):
# return (self[key] for key in self)
def __init__(self, *args, **kwargs) -> None:
warnings.warn(
"Use requests.structures.CaseInsensitiveDict directly", DeprecationWarning
)
super().__init__(*args, **kwargs)


def threaded_requests(requests):
Expand Down
16 changes: 15 additions & 1 deletion tests/resources/test_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_0_attachment_meta(self):
# we have no control over server side upload limit
self.assertIn("uploadLimit", meta)

def test_1_add_remove_attachment(self):
def test_1_add_remove_attachment_using_filestream(self):
issue = self.jira.issue(self.issue_1)
with open(TEST_ATTACH_PATH, "rb") as f:
attachment = self.jira.add_attachment(issue, f, "new test attachment")
Expand All @@ -27,3 +27,17 @@ def test_1_add_remove_attachment(self):
)
# JIRA returns a HTTP 204 upon successful deletion
self.assertEqual(attachment.delete().status_code, 204)

def test_2_add_remove_attachment_using_filename(self):
issue = self.jira.issue(self.issue_1)
attachment = self.jira.add_attachment(
issue, TEST_ATTACH_PATH, "new test attachment"
)
new_attachment = self.jira.attachment(attachment.id)
msg = "attachment %s of issue %s" % (new_attachment.__dict__, issue)
self.assertEqual(new_attachment.filename, "new test attachment", msg=msg)
self.assertEqual(
new_attachment.size, os.path.getsize(TEST_ATTACH_PATH), msg=msg
)
# JIRA returns a HTTP 204 upon successful deletion
self.assertEqual(attachment.delete().status_code, 204)

0 comments on commit b9ca82c

Please sign in to comment.