diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..80676bc --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +per-file-ignores = __init__.py:F401 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..82f9239 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions +name: Run CI +on: + push: + branches: [trunk] + pull_request: + branches: [trunk] +jobs: + build: + name: Build + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.6, 3.7, 3.8, 3.9] + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -r requirements-test.txt + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..d9562ac --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,28 @@ +# This workflow will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries +name: Publish package to PyPI +on: + release: + types: [created] +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + - 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/* diff --git a/.gitignore b/.gitignore index b542cee..2b50e6d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ __pycache__ build/ dist/ *.egg-info/ -run.py -run3.py +sample.py .vscode/ +env/ +.pytest_cache/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a8155d6..0000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -language: python -python: - - "2.7" - - "3.5" - - "3.6" - - "nightly" -# command to install dependencies -install: - - pip install . - - pip install -r requirements-test.txt -# command to run tests -script: nosetests diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d08df1d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,94 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [3.0.0] - 2021-03-13 +### Removed +- Removed support to legacy Python versions, now supports Python 3.6+. +- Removed ordereddict package dependency. +### Added +- Added support for Python 3.8 and Python 3.9. +- Added option to set custom `user_agent`. +### Changed +- Updated default "User-Agent" to `WooCommerce-Python-REST-API/3.0.0`. +- Updated Request library to 2.25.1. +### Fixed +- Fixed Basic Auth in Python 3.8. + +## [2.1.1] - 2019-07-22 +### Changed +- Updated Request library to 2.22.0. +- Updated examples. + +## [2.1.0] - 2019-01-15 +### Changed +- Uses WP REST API by default, need to set `wp_api` as `False` in order to use the legacy WooCommerce API. +- Updated default REST API version to `wc/v3`. + +## [2.0.0] - 2019-01-15 +### Added +- Added support for custom timestamps in oAuth1.0a requests with `oauth_timestamp`. +- Allow pass custom arguments to "Requests" library.. +### Changed +- Updated "Requests" library to version 2.20.0. + +## [1.2.1] - 2016-12-14 +### Fixed +- Fixed use of `content-type` to fix issues with WordPress 4.7. + +## [1.2.0] - 2016-06-22 +### Added +- Added option `query_string_auth` to allow Basic Auth as query strings. + +## [1.1.1] - 2016-06-03 +### Fixed +- Fixed oAuth signature for WP REST API. + +## [1.1.0] - 2016-05-09 +### Added +- Added support for WP REST API. +- Added method to handle HTTP OPTIONS requests. + +## [1.0.5] - 2015-12-07 +### Fixed +- Fixed oAuth filters sorting. + +## [1.0.4] - 2015-09-25 +### Added +- Adds `timeout` argument for `API` class. + +## [1.0.3] - 2015-08-07 +### Changed +- Forced utf-8 encoding on `API.__request()` to avoid `UnicodeDecodeError`. + +## [1.0.2] - 2015-08-05 +### Fixed +- Fixed handler for query strings. + +## [1.0.1] - 2015-07-13 +### Fixed +- Fixed support for Python 2.6. + +## [1.0.0] - 2015-07-12 +### 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 +[2.0.0]: https://github.com/woocommerce/wc-api-python/compare/1.2.1...2.0.0 +[1.2.1]: https://github.com/woocommerce/wc-api-python/compare/1.2.0...1.2.1 +[1.2.0]: https://github.com/woocommerce/wc-api-python/compare/1.1.1...1.2.0 +[1.1.1]: https://github.com/woocommerce/wc-api-python/compare/1.1.0...1.1.1 +[1.1.0]: https://github.com/woocommerce/wc-api-python/compare/1.0.5...1.1.0 +[1.0.5]: https://github.com/woocommerce/wc-api-python/compare/1.0.4...1.0.5 +[1.0.4]: https://github.com/woocommerce/wc-api-python/compare/1.0.3...1.0.4 +[1.0.3]: https://github.com/woocommerce/wc-api-python/compare/1.0.2...1.0.3 +[1.0.2]: https://github.com/woocommerce/wc-api-python/compare/1.0.1...1.0.2 +[1.0.1]: https://github.com/woocommerce/wc-api-python/compare/1.0.0...1.0.1 +[1.0.0]: https://github.com/woocommerce/wc-api-python/releases/tag/1.0.0 diff --git a/LICENSE.txt b/LICENSE.txt index e6768bc..73248e1 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019, Automattic (https://woocommerce.com/) +Copyright (c) 2021, Automattic (https://automattic.com/) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rst b/README.rst index 8d30583..38d507b 100644 --- a/README.rst +++ b/README.rst @@ -3,8 +3,8 @@ WooCommerce API - Python Client A Python wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API using this library. -.. image:: https://secure.travis-ci.org/woocommerce/wc-api-python.svg - :target: http://travis-ci.org/woocommerce/wc-api-python +.. image:: https://github.com/woocommerce/wc-api-python/actions/workflows/ci.yml/badge.svg?branch=trunk + :target: https://github.com/woocommerce/wc-api-python/actions/workflows/ci.yml .. image:: https://img.shields.io/pypi/v/woocommerce.svg :target: https://pypi.python.org/pypi/WooCommerce @@ -58,6 +58,8 @@ Options +-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+ | ``query_string_auth`` | ``bool`` | no | Force Basic Authentication as query string when ``True`` and using under HTTPS, default is ``False`` | +-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+ +| ``user_agent`` | ``string`` | no | Set a custom User-Agent, default is ``WooCommerce-Python-REST-API/3.0.0`` | ++-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+ | ``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) | @@ -146,72 +148,4 @@ Request with `params` example Changelog --------- -2.1.1 - 2019/07/22 -~~~~~~~~~~~~~~~~~~ - -- Updated Request library to 2.22.0. -- Updated examples. - -2.1.0 - 2019/01/15 -~~~~~~~~~~~~~~~~~~ - -- Uses WP REST API by default, need to force ``wp_api`` as ``False`` in order to use the legacy WooCommerce API. -- Updated default REST API version to ``wc/v3``. - -2.0.0 - 2019/01/15 -~~~~~~~~~~~~~~~~~~ - -- Updated "Requests" library to version 2.20.0. -- Added support for custom timestamps in oAuth1.0a requests with ``oauth_timestamp``. -- Allow pass custom arguments to "Requests" library. - -1.2.1 - 2016/12/14 -~~~~~~~~~~~~~~~~~~ - -- Fixed WordPress 4.7 compatibility. - -1.2.0 - 2016/06/22 -~~~~~~~~~~~~~~~~~~ - -- Added option ``query_string_auth`` to allow Basic Auth as query strings. - -1.1.1 - 2016/06/03 -~~~~~~~~~~~~~~~~~~ - -- Fixed oAuth signature for WP REST API. - -1.1.0 - 2016/05/09 -~~~~~~~~~~~~~~~~~~ - -- Added support for WP REST API. -- Added method to do HTTP OPTIONS requests. - -1.0.5 - 2015/12/07 -~~~~~~~~~~~~~~~~~~ - -- Fixed oAuth filters sorting. - -1.0.4 - 2015/09/25 -~~~~~~~~~~~~~~~~~~ - -- Implemented ``timeout`` argument for ``API`` class. - -1.0.3 - 2015/08/07 -~~~~~~~~~~~~~~~~~~ - -- Forced utf-8 encoding on ``API.__request()`` to avoid ``UnicodeDecodeError`` - -1.0.2 - 2015/08/05 -~~~~~~~~~~~~~~~~~~ - -- Fixed handler for query strings - -1.0.1 - 2015/07/13 -~~~~~~~~~~~~~~~~~~ - -- Fixed support for Python 2.6 - -1.0.1 - 2015/07/12 -~~~~~~~~~~~~~~~~~~ - -- Initial version +See `CHANGELOG.md `_. diff --git a/requirements-test.txt b/requirements-test.txt index 00b7a73..950b2c5 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,3 +1,4 @@ -r requirements.txt -httmock==1.3.0 -nose==1.3.7 +httmock==1.4.0 +pytest==6.2.2 +flake8==3.8.4 diff --git a/requirements.txt b/requirements.txt index 93cf612..9d84d35 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -requests==2.22.0 -ordereddict==1.1 +requests==2.25.1 diff --git a/setup.py b/setup.py index 04115ad..9dae917 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,7 @@ description="A Python wrapper for the WooCommerce REST API", long_description=README, author="Claudio Sanches @ Automattic", + author_email="claudio+pypi@automattic.com", url="https://github.com/woocommerce/wc-api-python", license="MIT License", packages=[ @@ -35,19 +36,25 @@ include_package_data=True, platforms=['any'], install_requires=[ - "requests", - "ordereddict" + "requests" ], + python_requires=">=3.6", classifiers=[ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Natural Language :: English", "License :: OSI Approved :: MIT License", "Programming Language :: Python", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", "Topic :: Software Development :: Libraries :: Python Modules" ], + 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', + }, ) diff --git a/tests.py b/test_api.py similarity index 100% rename from tests.py rename to test_api.py diff --git a/woocommerce/__init__.py b/woocommerce/__init__.py index b1d6ec8..15edcc8 100644 --- a/woocommerce/__init__.py +++ b/woocommerce/__init__.py @@ -10,7 +10,7 @@ """ __title__ = "woocommerce" -__version__ = "2.1.1" +__version__ = "3.0.0" __author__ = "Claudio Sanches @ Automattic" __license__ = "MIT" diff --git a/woocommerce/api.py b/woocommerce/api.py index f0f4206..a97c901 100644 --- a/woocommerce/api.py +++ b/woocommerce/api.py @@ -5,7 +5,7 @@ """ __title__ = "woocommerce-api" -__version__ = "2.1.1" +__version__ = "3.0.0" __author__ = "Claudio Sanches @ Automattic" __license__ = "MIT" @@ -13,11 +13,8 @@ from json import dumps as jsonencode from time import time from woocommerce.oauth import OAuth - -try: - from urllib.parse import urlencode -except ImportError: - from urllib import urlencode +from requests.auth import HTTPBasicAuth +from urllib.parse import urlencode class API(object): @@ -33,6 +30,7 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs): self.timeout = kwargs.get("timeout", 5) self.verify_ssl = kwargs.get("verify_ssl", True) self.query_string_auth = kwargs.get("query_string_auth", False) + self.user_agent = kwargs.get("user_agent", f"WooCommerce-Python-REST-API/{__version__}") def __is_ssl(self): """ Check if url use HTTPS """ @@ -44,12 +42,12 @@ def __get_url(self, endpoint): api = "wc-api" if url.endswith("/") is False: - url = "%s/" % url + url = f"{url}/" if self.wp_api: api = "wp-json" - return "%s%s/%s/%s" % (url, api, self.version, endpoint) + return f"{url}{api}/{self.version}/{endpoint}" def __get_oauth_url(self, url, method, **kwargs): """ Generate oAuth1.0a URL """ @@ -71,12 +69,12 @@ def __request(self, method, endpoint, data, params=None, **kwargs): url = self.__get_url(endpoint) auth = None headers = { - "user-agent": "WooCommerce API Client-Python/%s" % __version__, + "user-agent": f"{self.user_agent}", "accept": "application/json" } if self.is_ssl is True and self.query_string_auth is False: - auth = (self.consumer_key, self.consumer_secret) + auth = HTTPBasicAuth(self.consumer_key, self.consumer_secret) elif self.is_ssl is True and self.query_string_auth is True: params.update({ "consumer_key": self.consumer_key, @@ -84,7 +82,7 @@ def __request(self, method, endpoint, data, params=None, **kwargs): }) else: encoded_params = urlencode(params) - url = "%s?%s" % (url, encoded_params) + url = f"{url}?{encoded_params}" url = self.__get_oauth_url(url, method, **kwargs) if data is not None: @@ -122,4 +120,3 @@ def delete(self, endpoint, **kwargs): def options(self, endpoint, **kwargs): """ OPTIONS requests """ return self.__request("OPTIONS", endpoint, None, **kwargs) - diff --git a/woocommerce/oauth.py b/woocommerce/oauth.py index 6b5538e..62557c0 100644 --- a/woocommerce/oauth.py +++ b/woocommerce/oauth.py @@ -5,7 +5,7 @@ """ __title__ = "woocommerce-oauth" -__version__ = "2.1.1" +__version__ = "3.0.0" __author__ = "Claudio Sanches @ Automattic" __license__ = "MIT" @@ -14,17 +14,8 @@ from hmac import new as HMAC from hashlib import sha1, sha256 from base64 import b64encode - -try: - from urllib.parse import urlencode, quote, unquote, parse_qsl, urlparse -except ImportError: - from urllib import urlencode, quote, unquote - from urlparse import parse_qsl, urlparse - -try: - from collections import OrderedDict -except ImportError: - from ordereddict import OrderedDict +from collections import OrderedDict +from urllib.parse import urlencode, quote, unquote, parse_qsl, urlparse class OAuth(object): @@ -57,7 +48,7 @@ def get_oauth_url(self): query_string = urlencode(params) - return "%s?%s" % (url, query_string) + return f"{url}?{query_string}" def generate_oauth_signature(self, params, url): """ Generate OAuth Signature """ @@ -71,7 +62,7 @@ def generate_oauth_signature(self, params, url): for key, value in params.items()] query_string = "%26".join(query_params) - string_to_sign = "%s&%s&%s" % (self.method, base_request_uri, query_string) + string_to_sign = f"{self.method}&{base_request_uri}&{query_string}" consumer_secret = str(self.consumer_secret) if self.version not in ["v1", "v2"]: