Skip to content

Commit

Permalink
Implement HTTP Request Timeouts - Take 2 (#62)
Browse files Browse the repository at this point in the history
HTTP timeout implementation
  • Loading branch information
rmartin16 authored Sep 29, 2021
1 parent b573621 commit dc84a62
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 191 deletions.
64 changes: 32 additions & 32 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
# Override language selection by uncommenting this and choosing your languages
# with:
# languages: go, javascript, csharp, python, cpp, java

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
# Override language selection by uncommenting this and choosing your languages
# with:
# languages: go, javascript, csharp, python, cpp, java

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
32 changes: 16 additions & 16 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
23 changes: 9 additions & 14 deletions qbittorrentapi/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def is_logged_in(self):
"""Implements :meth:`~AuthAPIMixIn.is_logged_in`"""
return self._client.is_logged_in

def log_in(self, username=None, password=None):
def log_in(self, username=None, password=None, **kwargs):
"""Implements :meth:`~AuthAPIMixIn.auth_log_in`"""
return self._client.auth_log_in(username=username, password=password)
return self._client.auth_log_in(username=username, password=password, **kwargs)

def log_out(self):
def log_out(self, **kwargs):
"""Implements :meth:`~AuthAPIMixIn.auth_log_out`"""
return self._client.auth_log_out()
return self._client.auth_log_out(**kwargs)


class AuthAPIMixIn(Request):
Expand Down Expand Up @@ -89,21 +89,16 @@ def auth_log_in(self, username=None, password=None, **kwargs):
self._password = password or ""

self._initialize_context()
self._post(
_name=APINames.Authorization,
_method="login",
data={"username": self.username, "password": self._password},
**kwargs
)
creds = {"username": self.username, "password": self._password}
self._post(_name=APINames.Authorization, _method="login", data=creds, **kwargs)

if not self.is_logged_in:
logger.debug('Login failed for user "%s"' % self.username)
logger.debug('Login failed for user "%s"', self.username)
raise self._suppress_context(
LoginFailed('Login authorization failed for user "%s"' % self.username)
)
else:
logger.debug('Login successful for user "%s"' % self.username)
logger.debug("SID: %s" % self._SID)
logger.debug('Login successful for user "%s"', self.username)
logger.debug("SID: %s", self._SID)

@property
def _SID(self):
Expand Down
51 changes: 25 additions & 26 deletions qbittorrentapi/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,28 @@ def boring_method():
return aliased_class


def login_required(f):
def login_required(func):
"""
Ensure client is logged in before calling API methods.
"""

@wraps(f)
@wraps(func)
def wrapper(client, *args, **kwargs):
if not client.is_logged_in:
logger.debug("Not logged in...attempting login")
client.auth_log_in()
client.auth_log_in(requests_args=client._get_requests_args(**kwargs))
try:
return f(client, *args, **kwargs)
return func(client, *args, **kwargs)
except HTTP403Error:
logger.debug("Login may have expired...attempting new login")
client.auth_log_in()
client.auth_log_in(requests_args=client._get_requests_args(**kwargs))

return f(client, *args, **kwargs)
return func(client, *args, **kwargs)

return wrapper


def handle_hashes(f):
def handle_hashes(func):
"""
Normalize torrent hash arguments.
Expand All @@ -118,13 +118,13 @@ def handle_hashes(f):
arguments into either 'torrent_hash' or 'torrent_hashes'.
"""

@wraps(f)
@wraps(func)
def wrapper(client, *args, **kwargs):
if "torrent_hash" not in kwargs and "hash" in kwargs:
kwargs["torrent_hash"] = kwargs.pop("hash")
elif "torrent_hashes" not in kwargs and "hashes" in kwargs:
kwargs["torrent_hashes"] = kwargs.pop("hashes")
return f(client, *args, **kwargs)
return func(client, *args, **kwargs)

return wrapper

Expand All @@ -137,10 +137,10 @@ def response_text(response_class):
:return: Text of the response casted to the specified class
"""

def _inner(f):
@wraps(f)
def wrapper(obj, *args, **kwargs):
result = f(obj, *args, **kwargs)
def _inner(func):
@wraps(func)
def wrapper(client, *args, **kwargs):
result = func(client, *args, **kwargs)
if isinstance(result, response_class):
return result
try:
Expand All @@ -155,21 +155,20 @@ def wrapper(obj, *args, **kwargs):


def response_json(response_class):

"""
Return the JSON in the API response. JSON is parsed as instance of response_class.
:param response_class: class to parse the JSON in to
:return: JSON as the response class
"""

def _inner(f):
@wraps(f)
def wrapper(obj, *args, **kwargs):
simple_response = obj._SIMPLE_RESPONSES or kwargs.pop(
def _inner(func):
@wraps(func)
def wrapper(client, *args, **kwargs):
simple_response = client._SIMPLE_RESPONSES or kwargs.pop(
"SIMPLE_RESPONSES", kwargs.pop("SIMPLE_RESPONSE", False)
)
response = f(obj, *args, **kwargs)
response = func(client, *args, **kwargs)
try:
if isinstance(response, response_class):
return response
Expand All @@ -181,7 +180,7 @@ def wrapper(obj, *args, **kwargs):
result = loads(response.text)
if simple_response:
return result
return response_class(result, obj)
return response_class(result, client)
except Exception as e:
logger.debug("Exception during response parsing.", exc_info=True)
raise APIError("Exception during response parsing. Error: %s" % repr(e))
Expand Down Expand Up @@ -228,8 +227,8 @@ def endpoint_introduced(version_introduced, endpoint):
:param endpoint: API endpoint (e.g. /torrents/categories)
"""

def _inner(f):
@wraps(f)
def _inner(func):
@wraps(func)
def wrapper(client, *args, **kwargs):

# if the endpoint doesn't exist, return None or log an error / raise an Exception
Expand All @@ -243,7 +242,7 @@ def wrapper(client, *args, **kwargs):
return None

# send request to endpoint
return f(client, *args, **kwargs)
return func(client, *args, **kwargs)

return wrapper

Expand All @@ -258,8 +257,8 @@ def version_removed(version_obsoleted, endpoint):
:param endpoint: name of the removed endpoint
"""

def _inner(f):
@wraps(f)
def _inner(func):
@wraps(func)
def wrapper(client, *args, **kwargs):

# if the endpoint doesn't exist, return None or log an error / raise an Exception
Expand All @@ -273,7 +272,7 @@ def wrapper(client, *args, **kwargs):
return None

# send request to endpoint
return f(client, *args, **kwargs)
return func(client, *args, **kwargs)

return wrapper

Expand Down
Loading

0 comments on commit dc84a62

Please sign in to comment.