Skip to content

Commit 2b360f9

Browse files
authored
Merge pull request #116 from sergioteula/clean-code
Clean code
2 parents 7ef4e5b + eb8f274 commit 2b360f9

29 files changed

+586
-300
lines changed

.github/workflows/codecov-upload.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

.github/workflows/python-publish.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
# This workflow will upload a Python Package using Twine when a release is created
2-
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3-
41
name: Upload Python Package
52

63
on:
74
release:
85
types: [created]
96

107
jobs:
11-
deploy:
128

9+
deploy:
1310
runs-on: ubuntu-latest
14-
1511
steps:
16-
- uses: actions/checkout@v2
12+
- name: Check out code
13+
uses: actions/checkout@v3
1714
- name: Set up Python
18-
uses: actions/setup-python@v2
15+
uses: actions/setup-python@v4
1916
with:
2017
python-version: '3.x'
2118
- name: Install dependencies
2219
run: |
2320
python -m pip install --upgrade pip
2421
pip install setuptools wheel twine
22+
- name: Update version number
23+
run: |
24+
sed -i 's/<PACKAGE_VERSION>/${{ github.ref_name }}/' setup.py
2525
- name: Build and publish
2626
env:
2727
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Amazon Product Advertising API 5.0 wrapper for Python
22

3-
A simple Python wrapper for the [last version of the Amazon Product Advertising API](https://webservices.amazon.com/paapi5/documentation/quick-start/using-sdk.html). This module allows interacting with Amazon using the official API in an easier way.
3+
A simple Python wrapper for the [last version of the Amazon Product Advertising
4+
API](https://webservices.amazon.com/paapi5/documentation/quick-start/using-sdk.html).
5+
This module allows interacting with Amazon using the official API in an easier way.
46

57
[![PyPI](https://img.shields.io/pypi/v/python-amazon-paapi?color=%231182C2&label=PyPI)](https://pypi.org/project/python-amazon-paapi/)
68
[![Python](https://img.shields.io/badge/Python->3.6-%23FFD140)](https://www.python.org/)
79
[![License](https://img.shields.io/badge/License-MIT-%23e83633)](https://github.com/sergioteula/python-amazon-paapi/blob/master/LICENSE)
810
[![Amazon API](https://img.shields.io/badge/Amazon%20API-5.0-%23FD9B15)](https://webservices.amazon.com/paapi5/documentation/)
9-
[![Codecov](https://img.shields.io/codecov/c/github/sergioteula/python-amazon-paapi?label=Coverage)](https://app.codecov.io/gh/sergioteula/python-amazon-paapi/)
11+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=sergioteula_python-amazon-paapi&metric=coverage)](https://sonarcloud.io/summary/new_code?id=sergioteula_python-amazon-paapi)
12+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=sergioteula_python-amazon-paapi&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=sergioteula_python-amazon-paapi)
1013
[![Support](https://img.shields.io/badge/Support-Good-brightgreen)](https://github.com/sergioteula/python-amazon-paapi/issues)
1114
[![PyPI - Downloads](https://img.shields.io/pypi/dm/python-amazon-paapi?label=Installs)](https://pypi.org/project/python-amazon-paapi/)
1215

13-
> If you are still using the old version, go [here](https://github.com/sergioteula/python-amazon-paapi/blob/master/amazon/README.md) for documentation or check our
14-
[migration guide](https://github.com/sergioteula/python-amazon-paapi/blob/master/docs/pages/migration-guide.md).
15-
1616
## Features
1717

1818
- Object oriented interface for simple usage.
@@ -92,7 +92,8 @@ asin = get_asin('https://www.amazon.com/dp/B01N5IB20Q')
9292

9393
**Throttling:**
9494

95-
Throttling value represents the wait time in seconds between API calls, being the default value 1 second. Use it to avoid reaching Amazon request limits.
95+
Throttling value represents the wait time in seconds between API calls, being the
96+
default value 1 second. Use it to avoid reaching Amazon request limits.
9697

9798
```python
9899
amazon = AmazonApi(KEY, SECRET, TAG, COUNTRY, throttling=4) # Makes 1 request every 4 seconds
@@ -101,12 +102,20 @@ amazon = AmazonApi(KEY, SECRET, TAG, COUNTRY, throttling=0) # No wait time betw
101102

102103
## Contribution
103104

104-
Activate githooks with:
105+
Creating pull requests for this repo is higly appreciated to add new features or solve
106+
bugs. To help during development process, githooks can be activated to run some scripts
107+
before pushing new commits. This will run checks for code format and tests, to ensure
108+
everything follows this repo guidelines. Use next command to activate them:
105109

106110
```
107111
git config core.hooksPath .githooks
108112
```
109113

114+
The same checks will also run on the repo with GitHub Actions to ensure all tests pass
115+
before merge.
116+
110117
## License
111118

112-
Copyright © 2021 Sergio Abad. See [license](https://github.com/sergioteula/python-amazon-paapi/blob/master/LICENSE) for details.
119+
Copyright © 2021 Sergio Abad. See
120+
[license](https://github.com/sergioteula/python-amazon-paapi/blob/master/LICENSE) for
121+
details.

amazon_paapi/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List, Union
88

99
from . import models
10-
from .errors import InvalidArgumentException
10+
from .errors import InvalidArgument
1111
from .helpers import arguments, requests
1212
from .helpers.generators import get_list_chunks
1313
from .helpers.items import sort_items
@@ -50,7 +50,7 @@ def __init__(
5050
self.region = models.regions.REGIONS[country]
5151
self.marketplace = "www.amazon." + models.regions.DOMAINS[country]
5252
except KeyError as error:
53-
raise InvalidArgumentException("Country code is not correct") from error
53+
raise InvalidArgument("Country code is not correct") from error
5454

5555
self.api = DefaultApi(key, secret, self._host, self.region)
5656

amazon_paapi/errors/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
from .exceptions import (
2-
AmazonException,
3-
ApiRequestException,
4-
AsinNotFoundException,
5-
AssociateValidationException,
6-
InvalidArgumentException,
7-
InvalidPartnerTagException,
8-
ItemsNotFoundException,
9-
MalformedRequestException,
10-
TooManyRequestsException,
2+
AmazonError,
3+
AsinNotFound,
4+
AssociateValidationError,
5+
InvalidArgument,
6+
InvalidPartnerTag,
7+
ItemsNotFound,
8+
MalformedRequest,
9+
RequestError,
10+
TooManyRequests,
1111
)
1212

1313
__all__ = [
14-
"AmazonException",
15-
"ApiRequestException",
16-
"AsinNotFoundException",
17-
"AssociateValidationException",
18-
"InvalidArgumentException",
19-
"InvalidPartnerTagException",
20-
"ItemsNotFoundException",
21-
"MalformedRequestException",
22-
"TooManyRequestsException",
14+
"AmazonError",
15+
"AsinNotFound",
16+
"AssociateValidationError",
17+
"InvalidArgument",
18+
"InvalidPartnerTag",
19+
"ItemsNotFound",
20+
"MalformedRequest",
21+
"RequestError",
22+
"TooManyRequests",
2323
]

amazon_paapi/errors/exceptions.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Custom exceptions module"""
22

33

4-
class AmazonException(Exception):
4+
class AmazonError(Exception):
55
"""Common base class for all Amazon API exceptions."""
66

77
def __init__(self, reason: str):
@@ -12,33 +12,33 @@ def __str__(self) -> str:
1212
return self.reason
1313

1414

15-
class InvalidArgumentException(AmazonException):
16-
"""Raised when arguments are not correct."""
15+
class AsinNotFound(AmazonError):
16+
"""Raised if the ASIN for an item is not found."""
1717

1818

19-
class AsinNotFoundException(AmazonException):
20-
"""Raised if the ASIN for an item is not found."""
19+
class AssociateValidationError(AmazonError):
20+
"""Raised when credentials are not valid for the selected country."""
2121

2222

23-
class ApiRequestException(AmazonException):
24-
"""Raised if the request to Amazon API fails"""
23+
class InvalidArgument(AmazonError):
24+
"""Raised when arguments are not correct."""
2525

2626

27-
class MalformedRequestException(AmazonException):
28-
"""Raised if the request for Amazon API is not correctly formed"""
27+
class InvalidPartnerTag(AmazonError):
28+
"""Raised if the partner tag is not present or invalid."""
2929

3030

31-
class ItemsNotFoundException(AmazonException):
32-
"""Raised if no items are found"""
31+
class ItemsNotFound(AmazonError):
32+
"""Raised if no items are found."""
3333

3434

35-
class TooManyRequestsException(AmazonException):
36-
"""Raised when requests limit is reached"""
35+
class MalformedRequest(AmazonError):
36+
"""Raised if the request for Amazon API is not correctly formed."""
3737

3838

39-
class InvalidPartnerTagException(AmazonException):
40-
"""Raised if the partner tag is not present or invalid"""
39+
class RequestError(AmazonError):
40+
"""Raised if the request to Amazon API fails."""
4141

4242

43-
class AssociateValidationException(AmazonException):
44-
"""Raised when credentials are not valid for the selected country"""
43+
class TooManyRequests(AmazonError):
44+
"""Raised when requests limit is reached."""

amazon_paapi/helpers/arguments.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
from typing import List, Union
55

6-
from ..errors import AsinNotFoundException, InvalidArgumentException
6+
from ..errors import InvalidArgument
77
from ..tools import get_asin
88

99

1010
def get_items_ids(items: Union[str, List[str]]) -> List[str]:
1111
if not isinstance(items, str) and not isinstance(items, List):
12-
raise InvalidArgumentException(
12+
raise InvalidArgument(
1313
"Invalid items argument, it should be a string or List of strings"
1414
)
1515

@@ -20,63 +20,60 @@ def get_items_ids(items: Union[str, List[str]]) -> List[str]:
2020
else:
2121
items_ids = [get_asin(x.strip()) for x in items]
2222

23-
if items_ids:
24-
return items_ids
25-
26-
raise AsinNotFoundException("No ASIN codes have been found.")
23+
return items_ids
2724

2825

2926
def check_search_args(**kwargs):
30-
_check_search_mandatory_args(**kwargs)
31-
_check_search_pagination_args(**kwargs)
27+
check_search_mandatory_args(**kwargs)
28+
check_search_pagination_args(**kwargs)
3229

3330

34-
def _check_search_mandatory_args(**kwargs):
31+
def check_search_mandatory_args(**kwargs):
3532
mandatory_args = [
36-
kwargs["keywords"],
37-
kwargs["actor"],
38-
kwargs["artist"],
39-
kwargs["author"],
40-
kwargs["brand"],
41-
kwargs["title"],
42-
kwargs["browse_node_id"],
43-
kwargs["search_index"],
33+
kwargs.get("keywords"),
34+
kwargs.get("actor"),
35+
kwargs.get("artist"),
36+
kwargs.get("author"),
37+
kwargs.get("brand"),
38+
kwargs.get("title"),
39+
kwargs.get("browse_node_id"),
40+
kwargs.get("search_index"),
4441
]
4542
if all(arg is None for arg in mandatory_args):
4643
error_message = (
4744
"At least one of the following args should be provided: keywords, actor,"
4845
" artist, author, brand, title, browse_node_id or search_index."
4946
)
50-
raise InvalidArgumentException(error_message)
47+
raise InvalidArgument(error_message)
5148

5249

53-
def _check_search_pagination_args(**kwargs):
50+
def check_search_pagination_args(**kwargs):
5451
error_message = "Args item_count and item_page should be integers between 1 and 10."
55-
pagination_args = [kwargs["item_count"], kwargs["item_page"]]
52+
pagination_args = [kwargs.get("item_count"), kwargs.get("item_page")]
5653
pagination_args = [arg for arg in pagination_args if arg]
5754

5855
if not all(isinstance(arg, int) for arg in pagination_args):
59-
raise InvalidArgumentException(error_message)
56+
raise InvalidArgument(error_message)
6057

6158
if not all(1 <= arg <= 10 for arg in pagination_args):
62-
raise InvalidArgumentException(error_message)
59+
raise InvalidArgument(error_message)
6360

6461

6562
def check_variations_args(**kwargs):
6663
error_message = (
6764
"Args variation_count and variation_page should be integers between 1 and 10."
6865
)
69-
variation_args = [kwargs["variation_count"], kwargs["variation_page"]]
66+
variation_args = [kwargs.get("variation_count"), kwargs.get("variation_page")]
7067
variation_args = [arg for arg in variation_args if arg]
7168

7269
if not all(isinstance(arg, int) for arg in variation_args):
73-
raise InvalidArgumentException(error_message)
70+
raise InvalidArgument(error_message)
7471

7572
if not all(1 <= arg <= 10 for arg in variation_args):
76-
raise InvalidArgumentException(error_message)
73+
raise InvalidArgument(error_message)
7774

7875

7976
def check_browse_nodes_args(**kwargs):
80-
if not isinstance(kwargs["browse_node_ids"], List):
77+
if not isinstance(kwargs.get("browse_node_ids"), List):
8178
error_message = "Argument browse_node_ids should be a List of strings."
82-
raise InvalidArgumentException(error_message)
79+
raise InvalidArgument(error_message)

0 commit comments

Comments
 (0)