Skip to content

Commit

Permalink
Merge pull request #529 from dosisod/drop-python2-support
Browse files Browse the repository at this point in the history
Drop python2 support
  • Loading branch information
JonathanHuot committed Mar 10, 2024
2 parents ed578f1 + 416d738 commit eee74a2
Show file tree
Hide file tree
Showing 19 changed files with 30 additions and 92 deletions.
2 changes: 0 additions & 2 deletions requests_oauthlib/compliance_fixes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# ruff: noqa: F401
from __future__ import absolute_import

from .facebook import facebook_compliance_fix
from .fitbit import fitbit_compliance_fix
from .slack import slack_compliance_fix
Expand Down
4 changes: 1 addition & 3 deletions requests_oauthlib/compliance_fixes/douban.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json

from oauthlib.common import to_unicode


def douban_compliance_fix(session):
def fix_token_type(r):
token = json.loads(r.text)
token.setdefault("token_type", "Bearer")
fixed_token = json.dumps(token)
r._content = to_unicode(fixed_token).encode("utf-8")
r._content = fixed_token.encode()
return r

session._client_default_token_placement = "query"
Expand Down
3 changes: 1 addition & 2 deletions requests_oauthlib/compliance_fixes/ebay.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from oauthlib.common import to_unicode


def ebay_compliance_fix(session):
Expand All @@ -13,7 +12,7 @@ def _compliance_fix(response):
if token.get("token_type") in ["Application Access Token", "User Access Token"]:
token["token_type"] = "Bearer"
fixed_token = json.dumps(token)
response._content = to_unicode(fixed_token).encode("utf-8")
response._content = fixed_token.encode()

return response

Expand Down
10 changes: 2 additions & 8 deletions requests_oauthlib/compliance_fixes/facebook.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from json import dumps

try:
from urlparse import parse_qsl
except ImportError:
from urllib.parse import parse_qsl

from oauthlib.common import to_unicode
from urllib.parse import parse_qsl


def facebook_compliance_fix(session):
Expand All @@ -26,7 +20,7 @@ def _compliance_fix(r):
if expires is not None:
token["expires_in"] = expires
token["token_type"] = "Bearer"
r._content = to_unicode(dumps(token)).encode("UTF-8")
r._content = dumps(token).encode()
return r

session.register_compliance_hook("access_token_response", _compliance_fix)
Expand Down
4 changes: 1 addition & 3 deletions requests_oauthlib/compliance_fixes/fitbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

from json import loads, dumps

from oauthlib.common import to_unicode


def fitbit_compliance_fix(session):
def _missing_error(r):
token = loads(r.text)
if "errors" in token:
# Set the error to the first one we have
token["error"] = token["errors"][0]["errorType"]
r._content = to_unicode(dumps(token)).encode("UTF-8")
r._content = dumps(token).encode()
return r

session.register_compliance_hook("access_token_response", _missing_error)
Expand Down
5 changes: 1 addition & 4 deletions requests_oauthlib/compliance_fixes/instagram.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
from urlparse import urlparse, parse_qs
except ImportError:
from urllib.parse import urlparse, parse_qs
from urllib.parse import urlparse, parse_qs

from oauthlib.common import add_params_to_uri

Expand Down
6 changes: 2 additions & 4 deletions requests_oauthlib/compliance_fixes/mailchimp.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import json

from oauthlib.common import to_unicode


def mailchimp_compliance_fix(session):
def _null_scope(r):
token = json.loads(r.text)
if "scope" in token and token["scope"] is None:
token.pop("scope")
r._content = to_unicode(json.dumps(token)).encode("utf-8")
r._content = json.dumps(token).encode()
return r

def _non_zero_expiration(r):
token = json.loads(r.text)
if "expires_in" in token and token["expires_in"] == 0:
token["expires_in"] = 3600
r._content = to_unicode(json.dumps(token)).encode("utf-8")
r._content = json.dumps(token).encode()
return r

session.register_compliance_hook("access_token_response", _null_scope)
Expand Down
4 changes: 1 addition & 3 deletions requests_oauthlib/compliance_fixes/plentymarkets.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from json import dumps, loads
import re

from oauthlib.common import to_unicode


def plentymarkets_compliance_fix(session):
def _to_snake_case(n):
Expand All @@ -22,7 +20,7 @@ def _compliance_fix(r):
for k, v in token.items():
fixed_token[_to_snake_case(k)] = v

r._content = to_unicode(dumps(fixed_token)).encode("UTF-8")
r._content = dumps(fixed_token).encode()
return r

session.register_compliance_hook("access_token_response", _compliance_fix)
Expand Down
5 changes: 1 addition & 4 deletions requests_oauthlib/compliance_fixes/slack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
from urlparse import urlparse, parse_qs
except ImportError:
from urllib.parse import urlparse, parse_qs
from urllib.parse import urlparse, parse_qs

from oauthlib.common import add_params_to_uri

Expand Down
4 changes: 1 addition & 3 deletions requests_oauthlib/compliance_fixes/weibo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from json import loads, dumps

from oauthlib.common import to_unicode


def weibo_compliance_fix(session):
def _missing_token_type(r):
token = loads(r.text)
token["token_type"] = "Bearer"
r._content = to_unicode(dumps(token)).encode("UTF-8")
r._content = dumps(token).encode()
return r

session._client.default_token_placement = "query"
Expand Down
13 changes: 4 additions & 9 deletions requests_oauthlib/oauth1_auth.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import logging

from oauthlib.common import extract_params
from oauthlib.oauth1 import Client, SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER
from oauthlib.oauth1 import SIGNATURE_TYPE_BODY
from requests.compat import is_py3
from requests.utils import to_native_string
from requests.auth import AuthBase

CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded"
CONTENT_TYPE_MULTI_PART = "multipart/form-data"

if is_py3:
unicode = str

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,7 +78,7 @@ def __call__(self, r):
or self.client.signature_type == SIGNATURE_TYPE_BODY
):
content_type = CONTENT_TYPE_FORM_URLENCODED
if not isinstance(content_type, unicode):
if not isinstance(content_type, str):
content_type = content_type.decode("utf-8")

is_form_encoded = CONTENT_TYPE_FORM_URLENCODED in content_type
Expand All @@ -96,17 +91,17 @@ def __call__(self, r):
if is_form_encoded:
r.headers["Content-Type"] = CONTENT_TYPE_FORM_URLENCODED
r.url, headers, r.body = self.client.sign(
unicode(r.url), unicode(r.method), r.body or "", r.headers
str(r.url), str(r.method), r.body or "", r.headers
)
elif self.force_include_body:
# To allow custom clients to work on non form encoded bodies.
r.url, headers, r.body = self.client.sign(
unicode(r.url), unicode(r.method), r.body or "", r.headers
str(r.url), str(r.method), r.body or "", r.headers
)
else:
# Omit body data in the signing of non form-encoded requests
r.url, headers, _ = self.client.sign(
unicode(r.url), unicode(r.method), None, r.headers
str(r.url), str(r.method), None, r.headers
)

r.prepare_headers(headers)
Expand Down
7 changes: 1 addition & 6 deletions requests_oauthlib/oauth1_session.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from __future__ import unicode_literals

try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
from urllib.parse import urlparse

import logging

Expand Down
1 change: 0 additions & 1 deletion requests_oauthlib/oauth2_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import unicode_literals
from oauthlib.oauth2 import WebApplicationClient, InsecureTransportError
from oauthlib.oauth2 import is_secure_transport
from requests.auth import AuthBase
Expand Down
2 changes: 0 additions & 2 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import unicode_literals

import logging

from oauthlib.common import generate_token, urldecode
Expand Down
6 changes: 1 addition & 5 deletions tests/test_compliance_fixes.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from __future__ import unicode_literals
from unittest import TestCase

import requests
import requests_mock
import time

try:
from urlparse import urlparse, parse_qs
except ImportError:
from urllib.parse import urlparse, parse_qs
from urllib.parse import urlparse, parse_qs

from oauthlib.oauth2.rfc6749.errors import InvalidGrantError
from requests_oauthlib import OAuth2Session
Expand Down
6 changes: 1 addition & 5 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import requests
import requests_oauthlib
import oauthlib
import os.path
from io import StringIO
import unittest

try:
from unittest import mock
except ImportError:
import mock
from unittest import mock


@mock.patch("oauthlib.oauth1.rfc5849.generate_timestamp")
Expand Down
33 changes: 11 additions & 22 deletions tests/test_oauth1_session.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
from __future__ import unicode_literals, print_function
import unittest
import sys
import requests
from io import StringIO
from unittest import mock

from oauthlib.oauth1 import SIGNATURE_TYPE_QUERY, SIGNATURE_TYPE_BODY
from oauthlib.oauth1 import SIGNATURE_RSA, SIGNATURE_PLAINTEXT
from requests_oauthlib import OAuth1Session

try:
from unittest import mock
except ImportError:
import mock

try:
import cryptography
except ImportError:
Expand All @@ -23,11 +17,6 @@
except ImportError:
jwt = None

if sys.version[0] == "3":
unicode_type = str
else:
unicode_type = unicode # noqa


TEST_RSA_KEY = (
"-----BEGIN RSA PRIVATE KEY-----\n"
Expand Down Expand Up @@ -165,17 +154,17 @@ def test_parse_response_url(self):
self.assertEqual(resp["oauth_token"], "foo")
self.assertEqual(resp["oauth_verifier"], "bar")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
self.assertIsInstance(k, str)
self.assertIsInstance(v, str)

def test_fetch_request_token(self):
auth = OAuth1Session("foo")
auth.send = self.fake_body("oauth_token=foo")
resp = auth.fetch_request_token("https://example.com/token")
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
self.assertIsInstance(k, str)
self.assertIsInstance(v, str)

def test_fetch_request_token_with_optional_arguments(self):
auth = OAuth1Session("foo")
Expand All @@ -185,17 +174,17 @@ def test_fetch_request_token_with_optional_arguments(self):
)
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
self.assertIsInstance(k, str)
self.assertIsInstance(v, str)

def test_fetch_access_token(self):
auth = OAuth1Session("foo", verifier="bar")
auth.send = self.fake_body("oauth_token=foo")
resp = auth.fetch_access_token("https://example.com/token")
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
self.assertIsInstance(k, str)
self.assertIsInstance(v, str)

def test_fetch_access_token_with_optional_arguments(self):
auth = OAuth1Session("foo", verifier="bar")
Expand All @@ -205,8 +194,8 @@ def test_fetch_access_token_with_optional_arguments(self):
)
self.assertEqual(resp["oauth_token"], "foo")
for k, v in resp.items():
self.assertIsInstance(k, unicode_type)
self.assertIsInstance(v, unicode_type)
self.assertIsInstance(k, str)
self.assertIsInstance(v, str)

def _test_fetch_access_token_raises_error(self, auth):
"""Assert that an error is being raised whenever there's no verifier
Expand Down
1 change: 0 additions & 1 deletion tests/test_oauth2_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import unicode_literals
import unittest

from oauthlib.oauth2 import WebApplicationClient, MobileApplicationClient
Expand Down
6 changes: 1 addition & 5 deletions tests/test_oauth2_session.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import unicode_literals
import json
import time
import tempfile
Expand All @@ -8,10 +7,7 @@
from copy import deepcopy
from unittest import TestCase

try:
from unittest import mock
except ImportError:
import mock
from unittest import mock

from oauthlib.common import urlencode
from oauthlib.oauth2 import TokenExpiredError, OAuth2Error
Expand Down

0 comments on commit eee74a2

Please sign in to comment.