Skip to content

Commit

Permalink
Merge pull request #28 from b0lle/master
Browse files Browse the repository at this point in the history
Added serializer for django rest framework
  • Loading branch information
sergioteula authored Jul 15, 2020
2 parents 641a4fa + d24ce46 commit 951341a
Show file tree
Hide file tree
Showing 3 changed files with 8,005 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,34 @@ Throttling value must be `greater than 0` or `False` to disable it. This value t
amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY, throttling=0.5) # Max one request every two seconds
amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY, throttling=False) # Unlimited requests per second

**API support**
We provide a serializer for django rest framework, which speeds up your api
implementation.

from amazon.paapi import AmazonAPI
from amazon.serializers import AmazonProductSerializer
from rest_framework import serializers
amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY)

product = amazon.get_product('B01N5IB20Q')
serialized_product = AmazonProductSerializer(product)
serialized.data # this prints serialized product

if you like to serialize a list of products:

products = amazon.search_products(item_count=40, keywords='Harry')
serialized_products = AmazonProductSerializer(products, many=True)
serialized_products.data

For more Information how to work with serializers see the documentation of
[django rest framework](https://www.django-rest-framework.org/api-guide/serializers/)


Changelog
-------------
Version 3.2.1 (unreleased)
- Added serializer class for django rest framework
- Added serialized data for your convenience
Version 3.2.0
- Added new method for getting browse nodes information.
- Removed the 10 pages limit on search_products and get_variations methods.
Expand Down
156 changes: 156 additions & 0 deletions amazon/serializer/dango_rest_framework.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
from rest_framework import serializers


class _AmazonValueUnitSerializer(serializers.Serializer):
value = serializers.FloatField()
unit = serializers.CharField()


class _AmazonDimensionsSerializer(serializers.Serializer):
height = _AmazonValueUnitSerializer()
length = _AmazonValueUnitSerializer()
width = _AmazonValueUnitSerializer()


class _AmazonProductSerializer(serializers.Serializer):
features = serializers.ListField()
languages = serializers.ListField()
formats = serializers.ListField()
pages_count = serializers.ListField()
color = serializers.CharField()
unit_count = serializers.IntegerField()
size = serializers.CharField()
dimensions = _AmazonDimensionsSerializer()
weight = _AmazonValueUnitSerializer()


class _AmazonContributorsSerializer(serializers.Serializer):
name = serializers.CharField()
role = serializers.CharField()


class _AmazonExternalIdsSerializer(serializers.Serializer):
ean = serializers.ListField()
isbn = serializers.ListField()
upc = serializers.ListField()


class _AmazonInfoSerializer(serializers.Serializer):
contributors = _AmazonContributorsSerializer(many=True)
manufacturer = serializers.CharField()
brand = serializers.CharField()
model = serializers.CharField()
part_number = serializers.CharField()
product_group = serializers.CharField()
binding = serializers.CharField()
is_adult = serializers.BooleanField()
audience_rating = serializers.CharField()
edition = serializers.CharField()
warranty = serializers.CharField()
publication_date = serializers.CharField()
release_date = serializers.CharField()
external_ids = _AmazonExternalIdsSerializer()


class _AmazonImageVariantsSerializer(serializers.Serializer):
small = serializers.ListField()
medium = serializers.ListField()
large = serializers.ListField()


class _AmazonImagesSerializer(serializers.Serializer):
small = serializers.CharField()
medium = serializers.CharField()
large = serializers.CharField()
cropped = _AmazonImageVariantsSerializer()
variants = _AmazonImageVariantsSerializer()


class _AmazonTradeInSerializer(serializers.Serializer):
elegible = serializers.BooleanField()
price = serializers.FloatField()
currency = serializers.CharField()


class _AmazonSavingsSerializer(serializers.Serializer):
value = serializers.FloatField()
currency = serializers.CharField()
per_unit = serializers.FloatField()
display = serializers.CharField()
percentage = serializers.FloatField()


class _AmazonPriceSerializer(serializers.Serializer):
value = serializers.FloatField()
currency = serializers.CharField()
per_unit = serializers.FloatField()
display = serializers.CharField()
savings = _AmazonSavingsSerializer()


class _AmazonPvpSerializer(serializers.Serializer):
value = serializers.FloatField()
currency = serializers.CharField()
per_unit = serializers.FloatField()
display = serializers.CharField()


class _AmazonAvailabilitySerializer(serializers.Serializer):
max_order_quantity = serializers.IntegerField()
min_order_quantity = serializers.IntegerField()
type = serializers.CharField()
message = serializers.CharField()


class _AmazonConditionSerializer(serializers.Serializer):
condition = serializers.CharField()
condition_display = serializers.CharField()
sub_condition = serializers.CharField()
sub_condition_display = serializers.CharField()


class _AmazonMerchantSerializer(serializers.Serializer):
default_shipping_country = serializers.CharField()
merchant_id = serializers.CharField()
name = serializers.CharField()


class _AmazonOtherSerializer(serializers.Serializer):
buybox_winner = serializers.BooleanField()
loyalty_points = serializers.IntegerField()
amazon_fulfilled = serializers.BooleanField()
free_shipping_eligible = serializers.BooleanField()
prime_eligible = serializers.BooleanField()
prime_exclusive = serializers.BooleanField()
prime_pantry = serializers.BooleanField()
violates_map = serializers.BooleanField()
offer_id = serializers.CharField()


class _AmazonPricesSerializer(serializers.Serializer):
price = _AmazonPriceSerializer()
pvp = _AmazonPvpSerializer()
availability = _AmazonAvailabilitySerializer()
condition = _AmazonConditionSerializer()
merchant = _AmazonMerchantSerializer()
other = _AmazonOtherSerializer()


class _AmazonOffersSummarySerializer(serializers.Serializer):
highest_price = _AmazonPvpSerializer()
lowest_price = _AmazonPvpSerializer()
condition = _AmazonConditionSerializer()
offer_count = serializers.IntegerField()


class AmazonProductSerializer(serializers.Serializer):
asin = serializers.CharField(max_length=10)
parent_asin = serializers.CharField(max_length=10)
title = serializers.CharField()
url = serializers.CharField()
product = _AmazonProductSerializer()
info = _AmazonInfoSerializer()
images = _AmazonImagesSerializer()
trade_in = _AmazonTradeInSerializer()
prices = _AmazonPricesSerializer()
offers_summary = _AmazonOffersSummarySerializer(many=True)
Loading

0 comments on commit 951341a

Please sign in to comment.