GithubHelp home page GithubHelp logo

netpedro-com / usps-tools Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 14 KB

Python implementation of USPS API Tools https://www.usps.com/business/web-tools-apis/documentation-updates.htm

License: MIT License

Python 100.00%
usps python

usps-tools's Introduction

Python USPS API Tools

This project is a Python implementation of USPS API Tools.

Implemented in this library:

Only a portion of the USPS API has been implemented. Contributions are welcome.

Installation

Download these sources into your project, then:

pip install ./usps-tools

Or install directly into your project (note to update the tag reference, in this case 0.1.0):

pip install git+https://github.com/pedrovagner/[email protected]#egg=usps-tools

Usage

import usps_tools as usps

usps_user_id = '<user id here>'

International freight:

# Create a package
package = usps.Package(
    unique_id=1,
    weight=1,
    width=1,
    length=1,
    height=1,
    value_of_contents=1,
    country='Brazil',
    origin_zip='33337',
)

# Convert to dict
package_dict = package.to_dict()

# Creating a package from dict (and validate)
try:
    package = usps.Package.get_instance(package_dict)
except usps.ValidationError as e:
    print(e.origin.messages)

try:
    package_services = usps.PriceCalculator.get_package_services(usps_user_id, package)
except usps.ConnectionFail as e:
    # Catch only connection error
    print(e)
    raise e
except usps.Timeout as e:
    # Catch only timeout connection error
    print(e)
    raise e
except usps.XmlLoadError as e:
    # Catch only xml parse error
    print(e)
    raise e
except usps.XmlResponseError as e:
    # Catch only xml response error
    print(e)
    raise e
except usps.UspsToolsException as e:
    # Catch any error
    print(e)
    raise e

if package_services.error_message:
    print(package_services.error_message)
    print(package_services.error_dict)

for service in package_services.services:
    print(service.name, service.rate)

Domestic freight:

# Create a package
package = usps.Package(
    unique_id=1,
    weight=1,
    width=1,
    length=1,
    height=1,
    value_of_contents=1,
    country='United States',
    zip_origination='33337',
    zip_destination='19716',
)

try:
    package_services = usps.PriceCalculator.get_package_services(usps_user_id, package)
except usps.UspsToolsException as e:
    # Catch any error
    print(e)
    raise e

if package_services.error_message:
    print(package_services.error_message)
    print(package_services.error_dict)

for service in package_services.services:
    print(service.name, service.rate)

Translation

Update translation files (development purposes only):

pip install Babel

python setup.py extract_messages
# Execute when creating a new language translation directory
# python setup.py init_catalog --locale pt_BR
python setup.py update_catalog
python setup.py compile_catalog

Contribution

Any contribution are welcome. This project follows Semantic Versioning.

License

See the LICENSE file for license rights and limitations (MIT).

usps-tools's People

Watchers

netpedro.com avatar

Forkers

agawronski

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.