Skip to content
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

Shipment hasn't field "items" #41

Open
tytarenko opened this issue Feb 25, 2016 · 1 comment
Open

Shipment hasn't field "items" #41

tytarenko opened this issue Feb 25, 2016 · 1 comment

Comments

@tytarenko
Copy link

How get items of shipment using this lib?
Doc of shipment has point "items" https://developer.bigcommerce.com/api/objects/v2/shipment

order = client.Orders.get(1)
shipment = order.shipments()[0]
# or
shipments = client.OrderShipments.all(1)[0] # id of order

# it returns <function items>
items = shipments.items 

# it returns a list of fields without items
items = shipments.items()

# it returns KeyError: 'items'
shipment['items']

How can I get it using this lib?

@kcs-sales
Copy link

Same problem arisen here, i am also unable to get shipment items within order.

In python dict object has standard function, which is items and item function return attributes of dict along with values in the form of list of tuples.
For example:
dict.items**({"key":"val"})** function will return [(u'key', u'val')].

Now while creating object, it discard the attribute items, this is bug and has to fix or has to change its attribute name items to any other attribute.

Resolution to your problem is:
Go to base class in /bigcommerce/resource/base.py and open the file using any editor.
and insert a new line (I.e where i have written changed/newly added)



class Mapping(dict):
    """
    Mapping

    provides '.' access to dictionary keys
    """
    def __init__(self, mapping, *args, **kwargs):
        """
        Create a new mapping. Filters the mapping argument
        to remove any elements that are already methods on the
        object.

        For example, Orders retains its `coupons` method, instead
        of being replaced by the dict describing the coupons endpoint
        """
        filter_args = {k: mapping[k] for k in mapping if k not in dir(self)}
        filter_args['_items'] = mapping['items'] if  mapping.has_key("items") else [] #Changed/Newly Added
        self.__dict__ = self
        dict.__init__(self, filter_args, *args, **kwargs)

    def __str__(self):
        """
        Display as a normal dict, but filter out underscored items first
        """
        return str({k: self.__dict__[k] for k in self.__dict__ if not k.startswith("_")})

    def __repr__(self):
        return "<%s at %s, %s>" % (type(self).__name__, hex(id(self)), str(self))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants