diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..597ed9e --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 88 +extend-ignore = W503,E203,F401 +exclude = */sdk/* diff --git a/.githooks/pre-push b/.githooks/pre-push index 6b0950e..fde9ece 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -1,9 +1,10 @@ -#! /bin/bash +#! /bin/bash -e ROOT_DIR="$(git rev-parse --show-toplevel)" source "${ROOT_DIR}/scripts/helpers" ./scripts/check_isort ./scripts/check_black +./scripts/check_flake8 header "Proceeding with push" diff --git a/.github/workflows/lint-and-test.yml b/.github/workflows/lint-and-test.yml index d1b489c..eb1e14b 100644 --- a/.github/workflows/lint-and-test.yml +++ b/.github/workflows/lint-and-test.yml @@ -28,3 +28,15 @@ jobs: uses: actions/checkout@v2 - name: Check code format run: ./scripts/check_black + + flake8: + runs-on: ubuntu-latest + container: + image: sergioteula/pytools + volumes: + - ${{github.workspace}}:/code + steps: + - name: Check out code + uses: actions/checkout@v2 + - name: Check code errors + run: ./scripts/check_flake8 diff --git a/amazon_paapi/api.py b/amazon_paapi/api.py index f66e25a..530d696 100644 --- a/amazon_paapi/api.py +++ b/amazon_paapi/api.py @@ -22,8 +22,8 @@ class AmazonApi: secret (``str``): Your API secret. tag (``str``): Your affiliate tracking id, used to create the affiliate link. country (``models.Country``): Country code for your affiliate account. - throttling (``float``, optional): Wait time in seconds between API calls. Use it to avoid - reaching Amazon limits. Defaults to 1 second. + throttling (``float``, optional): Wait time in seconds between API calls. Use it + to avoid reaching Amazon limits. Defaults to 1 second. Raises: ``InvalidArgumentException`` @@ -67,19 +67,21 @@ def get_items( """Get items information from Amazon. Args: - items (``str`` | ``list[str]``): One or more items, using ASIN or product URL. Items - in string format should be separated by commas. - condition (``models.Condition``, optional): Filters offers by condition type. - Defaults to Any. - merchant (``models.Merchant``, optional): Filters search results to return items having - at least one offer sold by target merchant. Defaults to All. - currency_of_preference (``str``, optional): Currency of preference in which the prices - information should be returned. Expected currency code format is ISO 4217. - languages_of_preference (``list[str]``, optional): Languages in order of preference in - which the item information should be returned. - include_unavailable (``bool``, optional): The returned list includes not available - items. Not available items have the ASIN and item_info equals None. Defaults to False. - kwargs (``dict``, optional): Any other arguments to be passed to the Amazon API. + items (``str`` | ``list[str]``): One or more items, using ASIN or product + URL. Items in string format should be separated by commas. + condition (``models.Condition``, optional): Filters offers by condition + type. Defaults to Any. + merchant (``models.Merchant``, optional): Filters search results to return + items having at least an offer sold by target merchant. Defaults to All. + currency_of_preference (``str``, optional): Currency of preference in which + the prices information should be returned. Expected currency code format + is ISO 4217. + languages_of_preference (``list[str]``, optional): Languages in order of + preference in which the item information should be returned. + include_unavailable (``bool``, optional): The returned list includes not + available items. Not available items have the ASIN and item_info equals + None. Defaults to False. + kwargs (``dict``, optional): Other arguments to be passed to the Amazon API. Returns: ``list[models.Item]``: A list of items with Amazon information. @@ -136,47 +138,54 @@ def search_items( sort_by: models.SortBy = None, **kwargs ) -> models.SearchResult: - """Searches for items on Amazon based on a search query. At least one of the following - parameters should be specified: ``keywords``, ``actor``, ``artist``, ``author``, - ``brand``, ``title``, ``browse_node_id`` or ``search_index``. + """Searches for items on Amazon based on a search query. At least one of the + following parameters should be specified: ``keywords``, ``actor``, ``artist``, + ``author``, ``brand``, ``title``, ``browse_node_id`` or ``search_index``. Args: - item_count (``int``, optional): Number of items returned. Should be between 1 and 10. - Defaults to 10. - item_page (``int``, optional): The specific page of items to be returned from the available - results. Should be between 1 and 10. Defaults to 1. + item_count (``int``, optional): Number of items returned. Should be between + 1 and 10. Defaults to 10. + item_page (``int``, optional): The specific page of items to be returned + from the available results. Should be between 1 and 10. Defaults to 1. actor (``str``, optional): Actor name associated with the item. artist (``str``, optional): Artist name associated with the item. author (``str``, optional): Author name associated with the item. brand (``str``, optional): Brand name associated with the item. keywords (``str``, optional): A word or phrase that describes an item. title (``str``, optional): Title associated with the item. - availability (``models.Availability``, optional): Filters available items on Amazon. - Defaults to Available. - browse_node_id (``str``, optional): A unique ID assigned by Amazon that identifies a product - category or subcategory. - condition (``models.Condition``, optional): Filters offers by condition type. Defaults to Any. - currency_of_preference (``str``, optional): Currency of preference in which the prices - information should be returned. Expected currency code format is ISO 4217. - delivery_flags (``list[str]``): Filters items which satisfy a certain delivery program. - languages_of_preference (``list[str]``, optional): Languages in order of preference in - which the item information should be returned. - merchant (``models.Merchant``, optional): Filters search results to return items having - at least one offer sold by target merchant. Defaults to All. - max_price (``int``, optional): Filters search results to items with at least one offer price - below the specified value. Prices appear in lowest currency denomination. - For example, $31.41 should be passed as 3141 or 28.00€ should be 2800. - min_price (``int``, optional): Filters search results to items with at least one offer price - above the specified value. Prices appear in lowest currency denomination. - For example, $31.41 should be passed as 3141 or 28.00€ should be 2800. - min_saving_percent (``int``, optional): Filters search results to items with at least one - offer having saving percentage above the specified value. Value should be - positive integer less than 100. - min_reviews_rating (``int``, optional): Filters search results to items with customer review - ratings above specified value. Value should be positive integer less than 5. - search_index (``str``, optional): Indicates the product category to search. Defaults to All. + availability (``models.Availability``, optional): Filters available items on + Amazon. Defaults to Available. + browse_node_id (``str``, optional): A unique ID assigned by Amazon that + identifies a product category or subcategory. + condition (``models.Condition``, optional): Filters offers by condition + type. Defaults to Any. + currency_of_preference (``str``, optional): Currency of preference in which + the prices information should be returned. Expected currency code format + is ISO 4217. + delivery_flags (``list[str]``): Filters items which satisfy a certain + delivery program. + languages_of_preference (``list[str]``, optional): Languages in order of + preference in which the item information should be returned. + merchant (``models.Merchant``, optional): Filters search results to return + items having at least an offer sold by target merchant. Defaults to All. + max_price (``int``, optional): Filters search results to items with at least + one offer price below the specified value. Prices appear in lowest + currency denomination. For example, $31.41 should be passed as 3141 or + 28.00€ should be 2800. + min_price (``int``, optional): Filters search results to items with at least + one offer price above the specified value. Prices appear in lowest + currency denomination. For example, $31.41 should be passed as 3141 or + 28.00€ should be 2800. + min_saving_percent (``int``, optional): Filters search results to items with + at least one offer having saving percentage above the specified value. + Value should be positive integer less than 100. + min_reviews_rating (``int``, optional): Filters search results to items with + customer review ratings above specified value. Value should be positive + integer less than 5. + search_index (``str``, optional): Indicates the product category to search. + Defaults to All. sort_by (``models.SortBy``, optional): The way in which items are sorted. - kwargs (``dict``, optional): Any other arguments to be passed to the Amazon API. + kwargs (``dict``, optional): Other arguments to be passed to the Amazon API. Returns: ``models.SearchResult``: The search result containing the list of items. @@ -235,21 +244,24 @@ def get_variations( Args: asin (``str``): One item, using ASIN or product URL. - variation_count (``int``, optional): Number of items returned. Should be between 1 and 10. - Defaults to 10. - variation_page (``int``, optional): The specific page of items to be returned from the available - results. Should be between 1 and 10. Defaults to 1. - condition (``models.Condition``, optional): Filters offers by condition type. Defaults to Any. - currency_of_preference (``str``, optional): Currency of preference in which the prices - information should be returned. Expected currency code format is ISO 4217. - languages_of_preference (``list[str]``, optional): Languages in order of preference in - which the item information should be returned. - merchant (``models.Merchant``, optional): Filters search results to return items having - at least one offer sold by target merchant. Defaults to All. - kwargs (``dict``, optional): Any other arguments to be passed to the Amazon API. + variation_count (``int``, optional): Number of items returned. Should be + between 1 and 10. Defaults to 10. + variation_page (``int``, optional): The specific page of items to be + returned from the available results. Should be between 1 and 10. + Defaults to 1. + condition (``models.Condition``, optional): Filters offers by condition + type. Defaults to Any. + currency_of_preference (``str``, optional): Currency of preference in which + the prices information should be returned. Expected currency code format + is ISO 4217. + languages_of_preference (``list[str]``, optional): Languages in order of + preference in which the item information should be returned. + merchant (``models.Merchant``, optional): Filters search results to return + items having at least an offer sold by target merchant. Defaults to All. + kwargs (``dict``, optional): Other arguments to be passed to the Amazon API. Returns: - ``models.VariationsResult``: The variations result containing the list of items. + ``models.VariationsResult``: Variations result containing the items list. Raises: ``InvalidArgumentException`` @@ -283,14 +295,16 @@ def get_browse_nodes( languages_of_preference: List[str] = None, **kwargs ) -> List[models.BrowseNode]: - """Returns the specified browse node's information like name, children and ancestors. + """Returns the specified browse node's information like name, children and + ancestors. Args: - browse_node_ids (``list[str]``): List of browse node ids. A browse node id is a unique - ID assigned by Amazon that identifies a product category/sub-category. - languages_of_preference (``list[str]``, optional): Languages in order of preference in - which the item information should be returned. - kwargs (``dict``, optional): Any other arguments to be passed to the Amazon API. + browse_node_ids (``list[str]``): List of browse node ids. A browse node id + is a unique ID assigned by Amazon that identifies a product + category/sub-category. + languages_of_preference (``list[str]``, optional): Languages in order of + preference in which the item information should be returned. + kwargs (``dict``, optional): Other arguments to be passed to the Amazon API. Returns: ``list[models.BrowseNode]``: A list of browse nodes. diff --git a/amazon_paapi/errors/__init__.py b/amazon_paapi/errors/__init__.py index 281c8b3..78e6860 100644 --- a/amazon_paapi/errors/__init__.py +++ b/amazon_paapi/errors/__init__.py @@ -1 +1,23 @@ -from .exceptions import * +from .exceptions import ( + AmazonException, + ApiRequestException, + AsinNotFoundException, + AssociateValidationException, + InvalidArgumentException, + InvalidPartnerTagException, + ItemsNotFoundException, + MalformedRequestException, + TooManyRequestsException, +) + +__all__ = [ + "AmazonException", + "ApiRequestException", + "AsinNotFoundException", + "AssociateValidationException", + "InvalidArgumentException", + "InvalidPartnerTagException", + "ItemsNotFoundException", + "MalformedRequestException", + "TooManyRequestsException", +] diff --git a/amazon_paapi/helpers/requests.py b/amazon_paapi/helpers/requests.py index a9b9c9b..aa4e9ee 100644 --- a/amazon_paapi/helpers/requests.py +++ b/amazon_paapi/helpers/requests.py @@ -50,7 +50,7 @@ def get_items_response(amazon_api, request: GetItemsRequest) -> List[Item]: except ApiException as e: _manage_response_exceptions(e) - if response.items_result == None: + if response.items_result is None: raise ItemsNotFoundException("No items have been found") return response.items_result.items @@ -77,7 +77,7 @@ def get_search_items_response(amazon_api, request: SearchItemsRequest) -> Search except ApiException as e: _manage_response_exceptions(e) - if response.search_result == None: + if response.search_result is None: raise ItemsNotFoundException("No items have been found") return response.search_result @@ -106,7 +106,7 @@ def get_variations_response( except ApiException as e: _manage_response_exceptions(e) - if response.variations_result == None: + if response.variations_result is None: raise ItemsNotFoundException("No variation items have been found") return response.variations_result @@ -135,7 +135,7 @@ def get_browse_nodes_response( except ApiException as e: _manage_response_exceptions(e) - if response.browse_nodes_result == None: + if response.browse_nodes_result is None: raise ItemsNotFoundException("No browse nodes have been found") return response.browse_nodes_result.browse_nodes diff --git a/scripts/check_flake8 b/scripts/check_flake8 new file mode 100755 index 0000000..1a117a1 --- /dev/null +++ b/scripts/check_flake8 @@ -0,0 +1,23 @@ +#! /bin/bash + +ROOT_DIR="$(git rev-parse --show-toplevel)" +source "${ROOT_DIR}/scripts/helpers" + +header "Checking code errors with flake8" + +if [ -n "$(check_if_installed docker)" ]; then + docker run -v "${PWD}:/code" sergioteula/pytools flake8 --color always . +elif [ -n "$(check_if_installed flake8)" ]; then + flake8 --color always . +else + error "flake8 is not installed" + exit 1 +fi + +EXIT_CODE="$?" +if [ "$EXIT_CODE" = "0" ]; then + success "Code analysis with flake8 is correct" +else + error "There are errors detected by flake8" + exit 1 +fi