Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Merge session and retry #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[flake8]
per-file-ignores = __init__.py:F401
exclude = .git,__pycache__,build,dist,venv,WooCommerce.egg-info
max-line-length = 120
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: [3.6, 3.7, 3.8, 3.9, 3.10]
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ sample.py
.vscode/
env/
.pytest_cache/
venv/
.idea/
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.0.1] - 2022-09-17
### Added

- Added `session` creation to the API object
- Added `Retry` to the API object

## [3.0.0] - 2021-03-13
### Removed
- Removed support to legacy Python versions, now supports Python 3.6+.
Expand Down Expand Up @@ -77,7 +83,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial release.

[Unreleased]: https://github.com/woocommerce/wc-api-python/compare/3.0.0...HEAD
[3.0.0]: https://github.com/woocommerce/wc-api-python/compare/2.1.1...3.0.0
[2.1.1]: https://github.com/woocommerce/wc-api-python/compare/2.0.1...2.1.1
[2.1.0]: https://github.com/woocommerce/wc-api-python/compare/2.0.0...2.1.0
Expand Down
10 changes: 8 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
WooCommerce API - Python Client
WooCommerce API Unofficial - Python Client
===============================

**Having no feedback from the creator of wc-api-python, I have forked the project to add the session functionality of requests and the Retry functionality**

A Python wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API using this library.

.. image:: https://github.com/woocommerce/wc-api-python/actions/workflows/ci.yml/badge.svg?branch=trunk
Expand Down Expand Up @@ -62,7 +64,11 @@ Options
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
| ``oauth_timestamp`` | ``integer`` | no | Custom timestamp for requests made with oAuth1.0a |
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
| ``wp_api`` | ``bool`` | no | Set to ``False`` in order to use the legacy WooCommerce API (deprecated) |
| ``retries`` | ``int`` | no | Set to ``3`` in order to retry 3 times before exiting. |
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
| ``backoff_factor`` | ``float`` | no | Set to ``0.3``. Change how long the processes will sleep between failed requests (exponential). |
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
| ``status_forcelist`` | ``list`` | no | Set to ``[500, 502, 503, 504, 429]`` List of status on which we have to try again |
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+

Methods
Expand Down
4 changes: 0 additions & 4 deletions requirements-test.txt

This file was deleted.

1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.28.1
5 changes: 5 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-r requirements.txt
httmock==1.4.0
pytest==7.1.3
flake8==5.0.4
black==22.8.0
29 changes: 14 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
# -*- coding: utf-8 -*-
""" Setup module """

from setuptools import setup
import os
import re

from setuptools import setup

# Get version from __init__.py file
VERSION = ""
with open("woocommerce/__init__.py", "r") as fd:
VERSION = re.search(r"^__version__\s*=\s*['\"]([^\"]*)['\"]", fd.read(), re.MULTILINE).group(1)
VERSION = re.search(
r"^__version__\s*=\s*['\"]([^\"]*)['\"]", fd.read(), re.MULTILINE
).group(1)

if not VERSION:
raise RuntimeError("Cannot find version information")
Expand All @@ -26,18 +28,14 @@
version=VERSION,
description="A Python wrapper for the WooCommerce REST API",
long_description=README,
author="Claudio Sanches @ Automattic",
author="Claudio Sanches & Antoine C",
author_email="[email protected]",
url="https://github.com/woocommerce/wc-api-python",
license="MIT License",
packages=[
"woocommerce"
],
packages=["woocommerce"],
include_package_data=True,
platforms=['any'],
install_requires=[
"requests"
],
platforms=["any"],
install_requires=["requests"],
python_requires=">=3.6",
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand All @@ -49,12 +47,13 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Software Development :: Libraries :: Python Modules"
"Programming Language :: Python :: 3.10",
"Topic :: Software Development :: Libraries :: Python Modules",
],
keywords='woocommerce rest api',
keywords="woocommerce rest api",
project_urls={
'Documentation': 'https://woocommerce.github.io/woocommerce-rest-api-docs/?python#libraries-and-tools',
'Source': 'https://github.com/woocommerce/wc-api-python',
'Tracker': 'https://github.com/woocommerce/wc-api-python/issues',
"Documentation": "https://woocommerce.github.io/woocommerce-rest-api-docs/?python#libraries-and-tools",
"Source": "https://github.com/woocommerce/wc-api-python",
"Tracker": "https://github.com/woocommerce/wc-api-python/issues",
},
)
97 changes: 54 additions & 43 deletions test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
""" API Tests """
import unittest

from httmock import HTTMock, all_requests

import woocommerce
from woocommerce import oauth
from httmock import all_requests, HTTMock


class WooCommerceTestCase(unittest.TestCase):
Expand All @@ -14,39 +16,39 @@ def setUp(self):
self.api = woocommerce.API(
url="http://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
consumer_secret=self.consumer_secret,
)

def test_version(self):
""" Test default version """
"""Test default version"""
api = woocommerce.API(
url="https://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
consumer_secret=self.consumer_secret,
)

self.assertEqual(api.version, "wc/v3")

def test_non_ssl(self):
""" Test non-ssl """
"""Test non-ssl"""
api = woocommerce.API(
url="http://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
consumer_secret=self.consumer_secret,
)
self.assertFalse(api.is_ssl)

def test_with_ssl(self):
""" Test ssl """
"""Test ssl"""
api = woocommerce.API(
url="https://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
consumer_secret=self.consumer_secret,
)
self.assertTrue(api.is_ssl, True)

def test_with_timeout(self):
""" Test timeout """
"""Test timeout"""
api = woocommerce.API(
url="https://woo.test",
consumer_key=self.consumer_key,
Expand All @@ -57,105 +59,114 @@ def test_with_timeout(self):

@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
"""URL Mock"""
return {"status_code": 200, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = api.get("products").status_code
self.assertEqual(status, 200)

def test_get(self):
""" Test GET requests """
"""Test GET requests"""

@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
"""URL Mock"""
return {"status_code": 200, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = self.api.get("products").status_code
self.assertEqual(status, 200)

def test_get_with_parameters(self):
""" Test GET requests w/ url params """
"""Test GET requests w/ url params"""

@all_requests
def woo_test_mock(*args, **kwargs):
return {'status_code': 200,
'content': 'OK'}
return {"status_code": 200, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = self.api.get("products", params={"per_page": 10, "page": 1, "offset": 0}).status_code
status = self.api.get(
"products", params={"per_page": 10, "page": 1, "offset": 0}
).status_code
self.assertEqual(status, 200)

def test_get_with_requests_kwargs(self):
""" Test GET requests w/ optional requests-module kwargs """
"""Test GET requests w/ optional requests-module kwargs"""

@all_requests
def woo_test_mock(*args, **kwargs):
return {'status_code': 200,
'content': 'OK'}
return {"status_code": 200, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = self.api.get("products", allow_redirects=True).status_code
self.assertEqual(status, 200)

def test_post(self):
""" Test POST requests """
"""Test POST requests"""

@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 201,
'content': 'OK'}
"""URL Mock"""
return {"status_code": 201, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = self.api.post("products", {}).status_code
self.assertEqual(status, 201)

def test_put(self):
""" Test PUT requests """
"""Test PUT requests"""

@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
"""URL Mock"""
return {"status_code": 200, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = self.api.put("products", {}).status_code
self.assertEqual(status, 200)

def test_delete(self):
""" Test DELETE requests """
"""Test DELETE requests"""

@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
"""URL Mock"""
return {"status_code": 200, "content": "OK"}

with HTTMock(woo_test_mock):
# call requests
status = self.api.delete("products").status_code
self.assertEqual(status, 200)

def test_oauth_sorted_params(self):
""" Test order of parameters for OAuth signature """
"""Test order of parameters for OAuth signature"""

def check_sorted(keys, expected):
params = oauth.OrderedDict()
for key in keys:
params[key] = ''
params[key] = ""

ordered = list(oauth.OAuth.sorted_params(params).keys())
self.assertEqual(ordered, expected)

check_sorted(['a', 'b'], ['a', 'b'])
check_sorted(['b', 'a'], ['a', 'b'])
check_sorted(['a', 'b[a]', 'b[b]', 'b[c]', 'c'], ['a', 'b[a]', 'b[b]', 'b[c]', 'c'])
check_sorted(['a', 'b[c]', 'b[a]', 'b[b]', 'c'], ['a', 'b[c]', 'b[a]', 'b[b]', 'c'])
check_sorted(['d', 'b[c]', 'b[a]', 'b[b]', 'c'], ['b[c]', 'b[a]', 'b[b]', 'c', 'd'])
check_sorted(['a1', 'b[c]', 'b[a]', 'b[b]', 'a2'], ['a1', 'a2', 'b[c]', 'b[a]', 'b[b]'])
check_sorted(["a", "b"], ["a", "b"])
check_sorted(["b", "a"], ["a", "b"])
check_sorted(
["a", "b[a]", "b[b]", "b[c]", "c"], ["a", "b[a]", "b[b]", "b[c]", "c"]
)
check_sorted(
["a", "b[c]", "b[a]", "b[b]", "c"], ["a", "b[c]", "b[a]", "b[b]", "c"]
)
check_sorted(
["d", "b[c]", "b[a]", "b[b]", "c"], ["b[c]", "b[a]", "b[b]", "c", "d"]
)
check_sorted(
["a1", "b[c]", "b[a]", "b[b]", "a2"], ["a1", "a2", "b[c]", "b[a]", "b[b]"]
)
4 changes: 2 additions & 2 deletions woocommerce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"""

__title__ = "woocommerce"
__version__ = "3.0.0"
__author__ = "Claudio Sanches @ Automattic"
__version__ = "3.0.1"
__author__ = "Claudio Sanches & Antoine C"
__license__ = "MIT"

from woocommerce.api import API
Loading