Code Monkey home page Code Monkey logo

pyshipstation's Introduction

Build Status Code style: black

Shipstation API Python Bindings

This package provides API bindings for interacting with ShipStation via Python.

Connecting to ShipStation

shipstation provides the class ShipStation to instantiate a new connection to ShipStation.

from shipstation.api import *

api_key = '[your key]'
api_secret = '[your secret]'

ss = ShipStation(key=api_key, secret=api_secret)

Sending Orders to ShipStation

Once you have a ShipStation object and a ShipStationOrder ready, you can send the order to the ShipStation API like so:

ss.add_order(...)
ss.submit_orders();

ShipStationOrder

Orders can be provided using the ShipStationOrder class.

The constructor accepts both order_key and order_number, but in practice only order_number is actually required. ShipStation will generate a unique key for you on submission.

id = '[a reference number]'
ss_order = ShipStationOrder(order_number=id)

Setting The Order Status

Order status is set using the set_status method.

ss_order.set_status('awaiting_shipment')

Setting Customer Details

Customer username and email can be set with the set_customer_deails method.

ss_order.set_customer_details(
    username='foobar',
    email='[email protected]'
)

Setting The Shipping and Billing Addresses

Addresses are represented as a ShipStationAddress instance, and set using the set_shipping_address or set_billing_address method.

shipping_address = ShipStationAddress(...)
billing_address = ShipStationAddress(...)

ss_order.set_shipping_address(shipping_address)
ss_order.set_billing_address(billing_address)

Setting The Package Dimensions and Weight

Package dimensions are represented as a ShipStationContainer instance, and set using the set_dimensions method.

container = ShipStationContainer(...)
ss_order.set_dimensions(container)

Adding Line Items

Line items are represented as a ShipStationItem instance, and are added individually using the add_item method.

item = ShipStationItem(...)
ss_order.add_item(item)

ShipStationWeight

This corresponds to the Weight model in ShipStation, and accepts units and value.

weight = ShipStationWeight(units='ounces', value=12)

ShipStationContainer

This corresponds to the Dimensions model in ShipStation.

Weight is represented as a ShipStationWeight instance and added via the set_weight method.

weight = ShipStationWeight(...)
ss_container = ShipStationContainer(
    units='inches',
    length=5,
    width=5,
    height=5
)
ss_container.set_weight(weight)

ShipStationItem

This corresponds to the Product model in ShipStation.

Weight is represented as a ShipStationWeight instance and added via the set_weight method.

weight = ShipStationWeight(...)
ss_item = ShipStationItem(
    sku='[your sku]',
    name='[item name]',
    image_url='[item image url]',
    quantity=1,
    unit_price=10
)
ss_item.set_weight(weight)

ShipStationCustomsItem

This corresponds to the CustomsItem model in ShipStation.

ss_customs_item = ShipStationCustomsItem(
    description='Satan\'s horns',
    quantity=1,
    value=Decimal('10'),
    harmonized_tariff_code='tariff code',
    country_of_origin='US'
)

ShipStationInternationalOptions

This corresponds to the InternationalOptions model in ShipStation.

customs_item objects are represented as ShipStationCustomsItem instances and added via the add_customs_item method.

customs_item = ShipStationCustomsItem(...)
ss_intl_options = ShipStationInternationalOptions(
    contents='merchandise',
    non_delivery='return_to_sender'
)
ss_intl_options.add_customs_item(customs_item)
ss_order.set_international_options(ss_intl_options)

contents can be one of:

  • merchandise
  • documents
  • gift
  • returned_goods
  • sample

delivery_options can be one of:

  • return_to_sender
  • treat_as_abandoned

ShipStationAddress

This corresponds to the Address model in ShipStation

ss_shipping_address = ShipStationAddress(
    name='[customer name]',
    street1='[street line 1]',
    street2='[street line 2]',
    street3='[street line 3]',
    city='[city]',
    state='[state]',
    postal_code=['zip code'],
    country='[two letter country code]'
)

Get existing ShipStation Orders

You can get existing orders from ShipStation with parameter filtering, and do what you wish with the Response object returned.

response = ss.fetch_orders()

The allowed filter list is:

    customer_name,
    item_keyword,
    create_date_start,
    create_date_end,
    modify_date_start,
    modify_date_end,
    order_date_start,
    order_date_end,
    order_number,
    order_status,
    payment_date_start,
    payment_date_end,
    store_id,
    sort_by,
    sort_dir,
    page,
    page_size.

Syntax for using a filter:

response = ss.fetch_orders(parameters_dict={'order_status': 'shipped', 'page': '2'})

The Response object has some handy methods and attributes. For example, you can get the output in a text form with response.text, or in JSON with response.json(). Please refer to (Requests' documentation)[https://2.python-requests.org/en/master/user/quickstart/#response-content] for more details.

pyshipstation's People

Contributors

agritheory avatar dnswrsrx avatar natecox avatar riveramj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyshipstation's Issues

Let's say I've received a payment for an existing order marked "awaiting_payment" in shipstation

What's the right way to move this order to "awaiting_shipment" --

Do I recreate the order on my side, with the same order_key, and then write it again, but this time with the status "awaiting_shipment"

or

Is there a way to "GET" the order object out of shipstation, say with the order_id, update it, and write it back to SS?

Let me know your thoughts on the best practice. Really appreciate your help here! Thanks.

get and post post methods do not return values in latest version

In [1]: import inspect
In [2]: from shipstation.api import *
In [3]: src = inspect.getsource(ShipStation)
In [4]: src
In [5]: import pprint
In [6]: pprint.PrettyPrinter(indent=4).pprint(src)

Excerpted:

 '\n'
 '    def get(self, endpoint="", payload=None):\n'
 '        url = "{}{}".format(self.url, endpoint)\n'
 '        r = requests.get(url, auth=(self.key, self.secret), params=payload)\n'
 '        if self.debug:\n'
 '            pprint.PrettyPrinter(indent=4).pprint(r.json())\n'
 '\n'
 '    def post(self, endpoint="", data=None):\n'
 '        url = "{}{}".format(self.url, endpoint)\n'
 '        headers = {"content-type": "application/json"}\n'
 '        r = requests.post(url, auth=(self.key, self.secret),\n'
 '                          data=data, headers=headers)\n'
 '        if self.debug:\n'
 '            pprint.PrettyPrinter(indent=4).pprint(r.json())\n'
 '\n'

pip list outputs: shipstation 0.1.3 which should have the fix for get here but not post. I am opening a PR to the develop branch, where you'll be able to merge in both changes to master.

Shipment Batch ID?

I think I know the answer to this -- but can you set shipment batch ID via the shipstation API, and if so, does your library support it? I think the answer is no but would love to hear back from you. Thanks nate!

More support/dev needed

ah man, why you stopped developing this API. It is really good staring point but it lacks lots of options.

are you expecting to do some work on this library?

Add put method to ShipStation class

A PUT request is required by the "update_store" api. Please add a put method to the ShipStation class so this API method can be added in the future.

Retry on 5xx server errors

After receiving a 504 GATEWAY_TIMEOUT from ShipStation's API, a retry with a backoff would improve stability.
Here a function that would solve this issue from this retries in requests guide:

def get_session(
    retries: int = 5,
    backoff_factor: float = 1,
    status_forcelist: list = [502, 503, 504],
) -> requests.Session:
    """Returns a requests session that retries on specific failures.
    also: https://stackoverflow.com/a/35636367/2469390
    https://www.coglib.com/~icordasc/blog/2014/12/retries-in-requests.html
    """
    session = requests.Session()
    retry = Retry(
        total=retries,
        read=retries,
        connect=retries,
        backoff_factor=backoff_factor,
        status_forcelist=status_forcelist,
    )
    adapter = HTTPAdapter(max_retries=retry)
    session.mount("http://", adapter)
    session.mount("https://", adapter)
    return session

Then I think ShipStation.get would change to:

    def get(self, endpoint="", payload=None):
        url = "{}{}".format(self.url, endpoint)
       session = get_session()
        r = session.get(url, auth=(self.key, self.secret), params=payload)
        if self.debug:
            pprint.PrettyPrinter(indent=4).pprint(r.json())

        return r

And ShipStation.post to:

    def post(self, endpoint="", data=None):
        url = "{}{}".format(self.url, endpoint)
        headers = {"content-type": "application/json"}
       session = get_session()
        r = session.post(url, auth=(self.key, self.secret),
                          data=data, headers=headers)
        if self.debug:
            pprint.PrettyPrinter(indent=4).pprint(r.json())

       return r

And similar for put()

Support querying existing orders from ShipStation

Currently, pyshipstation only supports pushing new orders via API and has no support for retrieving an existing list of orders. A new method should be added (ShipStation#fetch_orders?) to allow for this.

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Country is missing from the ShipStationAddress constructor

class ShipStationAddress(ShipStationBase):
def init(
self,
name=None,
company=None,
street1=None,
street2=None,
street3=None,
city=None,
state=None,
postal_code=None,
country=None,
phone=None,
residential=None,
):
self.name = name
self.company = company
self.street1 = street1
self.street2 = street2
self.street3 = street3
self.city = city
self.state = state
self.postal_code = postal_code
self.phone = phone
self.residential = residential

*** Need to add self.country = country

Please add ship_date to parameters!

I want to use this tool to count how many orders were shipped on a given date, but currently ship_date is not a parameter I can use.

I have been tinkering trying to add it myself, but haven't had any success so far :(

I don't think this is where feature requests go, but I couldn't find where else to put it so I apologize if this is the wrong spot!

Love the code though! I learned a lot snooping around, and have made something that works as a more in-depth "insights" page

Add timeout to ShipStation class

requests' default behavior is to not provide a timeout, which is not recommended, even in their own documentation. Please add a timeout attribute to the ShipStation class and a timeout parameter to the get and post methods of the ShipStation class.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.