pymyob


Namepymyob JSON
Version 1.2.23 PyPI version JSON
download
home_pagehttps://github.com/uptick/pymyob
SummaryA Python API around MYOB's AccountRight API.
upload_time2023-09-12 04:10:37
maintainer
docs_urlNone
authorJarek Głowacki
requires_python
licenseBSD
keywords myob
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyMYOB

[![PyPI version](https://badge.fury.io/py/pymyob.svg)](https://pypi.org/project/pymyob)
[![versions](https://img.shields.io/pypi/pyversions/pymyob.svg)](https://pypi.org/project/pymyob)
[![Downloads](https://static.pepy.tech/badge/pymyob/month)](https://pepy.tech/project/pymyob)
[![Test](https://github.com/uptick/pymyob/workflows/Test/badge.svg)](https://github.com/uptick/pymyob/actions?query=workflow%3ATest)
[![Lint](https://github.com/uptick/pymyob/workflows/Lint/badge.svg)](https://github.com/uptick/pymyob/actions?query=workflow%3ALint)

A Python API around the [MYOB Business API](https://developer.myob.com/api/myob-business-api/v2/) (formerly AccountRight Live, and New Essentials).

## Pre-getting started

Register for API Keys with MYOB. You'll find detailed instructions [here](http://developer.myob.com/api/accountright/api-overview/getting-started/).

## Getting started

Install:

```
pip install pymyob
```

Create a `PartnerCredentials` instance and provide the Key, Secret and Redirect Uri as you've set up in MYOB:

```
from myob.credentials import PartnerCredentials

cred = PartnerCredentials(
    consumer_key=<Key>,
    consumer_secret=<Secret>,
    callback_uri=<Redirect Uri>,
)
```

Cache `cred.state` somewhere. You'll use this to rebuild the `PartnerCredentials` instance later.
This object includes a datetime object, so if your cache does not serialise datetime objects, you'll need to find an alternative, such as pickling and saving to a binary database column.

Redirect the user to `cred.url`. There, they will need to log in to MYOB and authorise partnership with your app<sup id="a1">[1](#f1)</sup>. Once they do, they'll be redirected to the Redirect Uri you supplied.

At the url they're redirected to, rebuild the `PartnerCredentials` then pick the verifier out of the request and use it to verify the credentials.

```
from myob.credentials import PartnerCredentials

def myob_authorisation_complete_view(request):
    verifier = request.GET.get('code', None)
    if verifier:
        state = <cached_state_from_earlier>
        if state:
            cred = PartnerCredentials(**state)
            cred.verify(verifier)
            if cred.verified:
                messages.success(request, 'OAuth verification successful.')
            else:
                messages.error(request, 'OAuth verification failed: verifier invalid.')
        else:
            messages.error(request, 'OAuth verification failed: nothing to verify.')
    else:
        messages.error(request, 'OAuth verification failed: no verifier received.')
```

Save `cred.state` once more, but this time you want it in persistent storage. So plonk it somewhere in your database.

With your application partnered with MYOB, you can now create a `Myob` instance, supplying the verified credentials:

```
from myob import Myob
from myob.credentials import PartnerCredentials

cred = PartnerCredentials(**<persistently_saved_state_from_verified_credentials>)
myob = Myob(cred)
```

You're almost there! MYOB has this thing called company files. Even though you've authorised against a user now, you need to collect a further set of credentials for getting into the company file.

```
companyfiles = myob.companyfiles.all()

# Each company file has the following attrs:
comp.id  # Company Id
comp.name  # Company Name
comp.data  # Remaining data as a raw dict.
```
Tip: the companyfiles object specifies all supported managers (that is, endpoints).

Render a dropdown for your user to let them select which of the company files they wish to use. Usually there will only be one against their account, but best to check.
If additional authentication against the company file is needed (ie when the company file account isn't tied via SSO to a my.myob account), prompt them for the username and password for that company file and save this as follows:

```
cred.authenticate_companyfile(<company_id>, <username>, <password>)
```

Save the new `cred.state` back to your persistent storage.

Now you can access stuff!

```
from myob import Myob
from myob.credentials import PartnerCredentials

cred = PartnerCredentials(**<persistently_saved_state_from_verified_credentials>)
myob = Myob(cred)

# Obtain list of company files. Here you will also find their IDs, which you'll need to retrieve a given company file later.
company_files = myob.companyfiles.all()

# Obtain a specific company file. Use `call=False` to just prep it for calling other endpoints without actually making a call yet at this stage.
comp = myob.companyfiles.get(<company_id>, call=False)

# Obtain a list of customers (two ways to go about this).
customers = comp.contacts.all(Type='Customer')
customers = comp.contacts.customer()

# Obtain a list of sale invoices (two ways to go about this).
invoices = comp.invoices.all(InvoiceType='Item', orderby='Number desc')
invoices = comp.invoices.item(orderby='Number desc')

# Create an invoice.
comp.invoices.post_item(data=data)

# Obtain a specific invoice.
invoice = comp.invoices.get_item(uid=<invoice_uid>)

# Download PDF for a specific invoice.
invoice_pdf = comp.invoices.get_item(uid=<invoice_uid>, headers={'Accept': 'application/pdf'})

# Obtain a list of tax codes.
taxcodes = comp.general_ledger.taxcode()

# Obtain a list of inventory items.
inventory = comp.inventory.item()

# Use endswith, startswith, or substringof filters
search_text = 'Acme'
customers = comp.contacts.customer(raw_filter=f"substringof('{search_text}', CompanyName)")
```

If you don't know what you're looking for, the reprs of most objects (eg. `myob`, `comp`, `comp.invoices` above) will yield info on what managers/methods are available.
Each method corresponds to one API call to MYOB.

Note that not all endpoints are covered here yet; we've just been adding them on an as-needed basis. If there's a particular endpoint you'd like added, please feel free to throw it into the endpoints.py file and open up a PR. All contributions are welcome and will be reviewed promptly. :)

##

<a name="f1">1</a>: Your users can review their partner authorisations at https://secure.myob.com/. [↩](#a1)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/uptick/pymyob",
    "name": "pymyob",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "myob",
    "author": "Jarek G\u0142owacki",
    "author_email": "jarekwg@gmail.com",
    "download_url": "",
    "platform": null,
    "description": "# PyMYOB\n\n[![PyPI version](https://badge.fury.io/py/pymyob.svg)](https://pypi.org/project/pymyob)\n[![versions](https://img.shields.io/pypi/pyversions/pymyob.svg)](https://pypi.org/project/pymyob)\n[![Downloads](https://static.pepy.tech/badge/pymyob/month)](https://pepy.tech/project/pymyob)\n[![Test](https://github.com/uptick/pymyob/workflows/Test/badge.svg)](https://github.com/uptick/pymyob/actions?query=workflow%3ATest)\n[![Lint](https://github.com/uptick/pymyob/workflows/Lint/badge.svg)](https://github.com/uptick/pymyob/actions?query=workflow%3ALint)\n\nA Python API around the [MYOB Business API](https://developer.myob.com/api/myob-business-api/v2/) (formerly AccountRight Live, and New Essentials).\n\n## Pre-getting started\n\nRegister for API Keys with MYOB. You'll find detailed instructions [here](http://developer.myob.com/api/accountright/api-overview/getting-started/).\n\n## Getting started\n\nInstall:\n\n```\npip install pymyob\n```\n\nCreate a `PartnerCredentials` instance and provide the Key, Secret and Redirect Uri as you've set up in MYOB:\n\n```\nfrom myob.credentials import PartnerCredentials\n\ncred = PartnerCredentials(\n    consumer_key=<Key>,\n    consumer_secret=<Secret>,\n    callback_uri=<Redirect Uri>,\n)\n```\n\nCache `cred.state` somewhere. You'll use this to rebuild the `PartnerCredentials` instance later.\nThis object includes a datetime object, so if your cache does not serialise datetime objects, you'll need to find an alternative, such as pickling and saving to a binary database column.\n\nRedirect the user to `cred.url`. There, they will need to log in to MYOB and authorise partnership with your app<sup id=\"a1\">[1](#f1)</sup>. Once they do, they'll be redirected to the Redirect Uri you supplied.\n\nAt the url they're redirected to, rebuild the `PartnerCredentials` then pick the verifier out of the request and use it to verify the credentials.\n\n```\nfrom myob.credentials import PartnerCredentials\n\ndef myob_authorisation_complete_view(request):\n    verifier = request.GET.get('code', None)\n    if verifier:\n        state = <cached_state_from_earlier>\n        if state:\n            cred = PartnerCredentials(**state)\n            cred.verify(verifier)\n            if cred.verified:\n                messages.success(request, 'OAuth verification successful.')\n            else:\n                messages.error(request, 'OAuth verification failed: verifier invalid.')\n        else:\n            messages.error(request, 'OAuth verification failed: nothing to verify.')\n    else:\n        messages.error(request, 'OAuth verification failed: no verifier received.')\n```\n\nSave `cred.state` once more, but this time you want it in persistent storage. So plonk it somewhere in your database.\n\nWith your application partnered with MYOB, you can now create a `Myob` instance, supplying the verified credentials:\n\n```\nfrom myob import Myob\nfrom myob.credentials import PartnerCredentials\n\ncred = PartnerCredentials(**<persistently_saved_state_from_verified_credentials>)\nmyob = Myob(cred)\n```\n\nYou're almost there! MYOB has this thing called company files. Even though you've authorised against a user now, you need to collect a further set of credentials for getting into the company file.\n\n```\ncompanyfiles = myob.companyfiles.all()\n\n# Each company file has the following attrs:\ncomp.id  # Company Id\ncomp.name  # Company Name\ncomp.data  # Remaining data as a raw dict.\n```\nTip: the companyfiles object specifies all supported managers (that is, endpoints).\n\nRender a dropdown for your user to let them select which of the company files they wish to use. Usually there will only be one against their account, but best to check.\nIf additional authentication against the company file is needed (ie when the company file account isn't tied via SSO to a my.myob account), prompt them for the username and password for that company file and save this as follows:\n\n```\ncred.authenticate_companyfile(<company_id>, <username>, <password>)\n```\n\nSave the new `cred.state` back to your persistent storage.\n\nNow you can access stuff!\n\n```\nfrom myob import Myob\nfrom myob.credentials import PartnerCredentials\n\ncred = PartnerCredentials(**<persistently_saved_state_from_verified_credentials>)\nmyob = Myob(cred)\n\n# Obtain list of company files. Here you will also find their IDs, which you'll need to retrieve a given company file later.\ncompany_files = myob.companyfiles.all()\n\n# Obtain a specific company file. Use `call=False` to just prep it for calling other endpoints without actually making a call yet at this stage.\ncomp = myob.companyfiles.get(<company_id>, call=False)\n\n# Obtain a list of customers (two ways to go about this).\ncustomers = comp.contacts.all(Type='Customer')\ncustomers = comp.contacts.customer()\n\n# Obtain a list of sale invoices (two ways to go about this).\ninvoices = comp.invoices.all(InvoiceType='Item', orderby='Number desc')\ninvoices = comp.invoices.item(orderby='Number desc')\n\n# Create an invoice.\ncomp.invoices.post_item(data=data)\n\n# Obtain a specific invoice.\ninvoice = comp.invoices.get_item(uid=<invoice_uid>)\n\n# Download PDF for a specific invoice.\ninvoice_pdf = comp.invoices.get_item(uid=<invoice_uid>, headers={'Accept': 'application/pdf'})\n\n# Obtain a list of tax codes.\ntaxcodes = comp.general_ledger.taxcode()\n\n# Obtain a list of inventory items.\ninventory = comp.inventory.item()\n\n# Use endswith, startswith, or substringof filters\nsearch_text = 'Acme'\ncustomers = comp.contacts.customer(raw_filter=f\"substringof('{search_text}', CompanyName)\")\n```\n\nIf you don't know what you're looking for, the reprs of most objects (eg. `myob`, `comp`, `comp.invoices` above) will yield info on what managers/methods are available.\nEach method corresponds to one API call to MYOB.\n\nNote that not all endpoints are covered here yet; we've just been adding them on an as-needed basis. If there's a particular endpoint you'd like added, please feel free to throw it into the endpoints.py file and open up a PR. All contributions are welcome and will be reviewed promptly. :)\n\n##\n\n<a name=\"f1\">1</a>: Your users can review their partner authorisations at https://secure.myob.com/. [\u21a9](#a1)\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "A Python API around MYOB's AccountRight API.",
    "version": "1.2.23",
    "project_urls": {
        "Homepage": "https://github.com/uptick/pymyob"
    },
    "split_keywords": [
        "myob"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9abc4d0bf3977446211c1fac0cf27285c139f1f4ca74da0785a5baffa51f4a19",
                "md5": "bf72757d1dbccfba7187c17a30f02b9c",
                "sha256": "66ade91a4d4a15167bafcb4360f0ff182c2c7413d56cd45c6e3ce95011391868"
            },
            "downloads": -1,
            "filename": "pymyob-1.2.23-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bf72757d1dbccfba7187c17a30f02b9c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 12553,
            "upload_time": "2023-09-12T04:10:37",
            "upload_time_iso_8601": "2023-09-12T04:10:37.245928Z",
            "url": "https://files.pythonhosted.org/packages/9a/bc/4d0bf3977446211c1fac0cf27285c139f1f4ca74da0785a5baffa51f4a19/pymyob-1.2.23-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-12 04:10:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "uptick",
    "github_project": "pymyob",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pymyob"
}
        
Elapsed time: 0.11281s