Skip to main content

Python SDK for accessing the NetSuite SOAP webservice

Project description

netsuite-sdk-py

Netsuite-sdk-py is a Python SDK. It uses the SOAP client library zeep(https://python-zeep.readthedocs.io/en/master/) for accessing NetSuite resources via the NetSuite SOAP web service SuiteTalk(http://www.netsuite.com/portal/platform/developer/suitetalk.shtml), but hides the complexity from the SDK user.

Installation

$ pip install netsuite-sdk-py

Get Started

There are two ways to access a NetSuite account via web services:

  • Use token-based auth (TBA) (within each request). This is the mechanism supported by this SDK.
  • Use email, password, role and account id to login and start a session. This is not supported by this SDK

Token-based Auth

First, setup TBA credentials using environment variables.

# TBA credentials
export NS_ACCOUNT=xxxx
export NS_CONSUMER_KEY=xxxx
export NS_CONSUMER_SECRET=xxxx
export NS_TOKEN_KEY=xxxx
export NS_TOKEN_SECRET=xxxx

The following snippet shows how to use TBA to initialize the SDK.

import os
import itertools
import json
from netsuitesdk import NetSuiteConnection

def connect_tba():
    NS_ACCOUNT = os.getenv('NS_ACCOUNT')
    NS_CONSUMER_KEY = os.getenv('NS_CONSUMER_KEY')
    NS_CONSUMER_SECRET = os.getenv('NS_CONSUMER_SECRET')
    NS_TOKEN_KEY = os.getenv('NS_TOKEN_KEY')
    NS_TOKEN_SECRET = os.getenv('NS_TOKEN_SECRET')
    nc = NetSuiteConnection(
        account=NS_ACCOUNT,
        consumer_key=NS_CONSUMER_KEY,
        consumer_secret=NS_CONSUMER_SECRET,
        token_key=NS_TOKEN_KEY,
        token_secret=NS_TOKEN_SECRET
    )
    return nc

nc = connect_tba()

# Use get_all methods to get all objects of certain types
currencies = nc.currencies.get_all()
locations = nc.locations.get_all()
departments = nc.departments.get_all()
classifications = nc.classifications.get_all()
subsidiaries = nc.subsidiaries.get_all()
expense_categories = nc.expense_categories.get_all()
employees = nc.employees.get_all()
all_accounts = list(itertools.islice(nc.accounts.get_all_generator(), 100))
accounts = [a for a in all_accounts if a['acctType'] == '_expense']
vendor_bills = list(itertools.islice(nc.vendor_bills.get_all_generator(), 10))
vendors = list(itertools.islice(nc.vendors.get_all_generator(), 10))
vendor_payments = nc.vendor_payments.get_all()

data = {
  'accounts': accounts,
  'classifications': classifications,
  'departments': departments,
  'locations': locations,
  'currencies': currencies,
  'vendors': vendors,
  'vendor_bills': vendor_bills,
  'subsidiaries': subsidiaries,
  'expense_categories': expense_categories,
  'employees': employees,
  'vendor_payments': vendor_payments
}
with open('/tmp/netsuite.json', 'w') as oj:
	oj.write(json.dumps(data, default=str, indent=2))

# There are also generator methods to iterate over potentially large lists
for c in nc.currencies.get_all_generator():
    print(c)

# Get a specific object
nc.currencies.get(internalId='1')

# Post operation is only supported on vendor_bills, expense_reports, journal_entries and vendor_payments currently (see tests on how to construct vendor bill, expense report and journal entry)
vb = {...}
nc.vendor_bills.post(vb)

er = {...}
nc.expense_reports.post(er)

je = {...}
nc.journal_entries.post(je)

vp = {...}
nc.vendor_payments.post(vp)

### Upsert Files
file = open('receipt.pdf', 'rb').read()

created_folder = nc.folders.post(
    {
        "externalId": 'new-folder',
        "name": 'Receipts'
    }
)

uploaded_file = nc.files.post({
    "externalId": "receipt 1",
    "name": 'receipt.pdf',
    'content': file,
    'fileType': '_PDF',
    "folder": {
                "name": None,
                "internalId": 695,
                "externalId": 'new-folder',
                "type": "folder"
            }
    }
)

Remarks and possible errors regarding authentication

Note: NetSuite requires two-factor authentication (2FA) for all Administrator and other highly privileged roles in all NetSuite accounts. Instead, you can login with a non-highly privileged role or use token based authentication (TBA) with your requests. For TBA, see below.

If login fails, a NetSuiteLoginError is thrown.

For more information about NetSuite authentication, see: (https://docs.oracle.com/cloud/latest/netsuitecs_gs/NSATH/NSATH.pdf)

Integration Tests

To run integration tests, you will set both login and TBA credentials for an actual Netsuite account with the right permissions.

# TBA credentials
export NS_ACCOUNT=xxxx
export NS_CONSUMER_KEY=xxxx
export NS_CONSUMER_SECRET=xxxx
export NS_TOKEN_KEY=xxxx
export NS_TOKEN_SECRET=xxxx

python -m pytest test/integration

Code coverage

To get code coverage report, run this command:

python -m pytest --cov=netsuitesdk

<snipped output>
Name                            Stmts   Miss  Cover
---------------------------------------------------
netsuitesdk/__init__.py             5      0   100%
netsuitesdk/client.py             343    198    42%
netsuitesdk/constants.py            3      0   100%
netsuitesdk/exceptions.py          14      5    64%
netsuitesdk/netsuite_types.py       2      0   100%
netsuitesdk/utils.py               47     19    60%
---------------------------------------------------
TOTAL                             414    222    46%

To get an html report, run this command:

python -m pytest --cov=netsuitesdk --cov-report html:cov_html

We want to maintain code coverage of more than 95% for this project at all times.

Documentation

Documentation can be found in the docs/_build/html folder (open index.html) and soon in readthedocs. For contributors: to build the documentation (cd to /docs and) run make buildapi as well as make html

Contributions

We are actively accepting contributions. Please mail shwetabh.kumar@fylehq.com if you wish to collaborate on this.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netsuite-sdk-py-0.1.0.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

netsuite_sdk_py-0.1.0-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file netsuite-sdk-py-0.1.0.tar.gz.

File metadata

  • Download URL: netsuite-sdk-py-0.1.0.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for netsuite-sdk-py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0b3a793661ae1b7f3ac36cba5b4bb9807dcfe519bb41ed4370d1d7eace02f19b
MD5 e84fa87305d17e43074ed9b2df74de13
BLAKE2b-256 9010e5423a6f684f05f38e85eefe36bab40acb5f50e1e001a65fc9ecc607da6f

See more details on using hashes here.

File details

Details for the file netsuite_sdk_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: netsuite_sdk_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for netsuite_sdk_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8945d36747db7aeacefc9f1af49dbc87f5e5a3f26a10ff620f6d7d635dcce75
MD5 b1c0bafd7621e9f7d188f5bbd0b7fd4d
BLAKE2b-256 c1626a18ef5f98100a428884105888bfdfdf5fe08dd72c71329ac9d93c32e82d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page