Skip to main content

A simple wrapper for the Lightspeed API

Project description

PLAW - Python Lightspeed API Wrapper

This module provides a pythonic interface to the Lightspeed API.

This is an incomplete project - I'm building another tool that depends on the Lightspeed API so I built this little wrapper to make it easier.

I've only implemented interfaces to the endpoints I'm using - Account, Shop, Employee, and EmployeeHours. It's possible I'll come back later and implement more, but not guaranteed.

I'd accept any contribution with open arms - just do me a favor and make sure you add tests.

Installation

pip install plaw

Usage

The Plaw class contains all attributes and methods necessary to interact with the API. Most users would probably instantiate a new object like so:

>>> from plaw import Plaw

>>> client_id = # your client id from lightspeed
>>> client_secret = # your client secret from lightspeed

>>> api = Plaw(client_id, client_secret)

To make any real use of the API, you need to get a temp access code from Lightspeed in order to receive an access and refresh token. The temp code expires after 30 seconds, so it's likely this would take place in e.g. a Django view - but regardless:

>>> code = # temp access code
>>> api.get_tokens(code)
>>> api.fetch_account_id()

Once you have this information you can make requests through the interfaces.

Pagination

Pagination is sort of built-in to the interface. These interfaces are going to return a generator object, with each item in the generator being a page of results from your query. This makes requesting the next page of results very simple:

>>> all_employee_hours = api.employee_hours()
>>> next(all_employee_hours) # first page
{
    "@attributes": {
        "count": "4252",
        "offset": "0",
        "limit": "100"
    },
    "EmployeeHours": [
        {
            "employeeHoursID": "2",
            "checkIn": "2014-02-19T23:08:08+00:00",
            "checkOut": "2014-02-19T23:08:22+00:00",
            "employeeID": "4",
            "shopID": "1"
        },
    ...

>>> next(all_employee_hours) # second page
{
    "@attributes": {
        "count": "4252",
        "offset": "100",
        "limit": "100"
    },
    "EmployeeHours": [
        {
            "employeeHoursID": "202",
            "checkIn": "2014-05-24T16:40:00+00:00",
            "checkOut": "2014-05-25T00:10:27+00:00",
            "employeeID": "2",
            "shopID": "1"
        },
    ...

etc.

Parameters

Each interface takes an optional params argument that expects a dictionary of query string parameters.

>>> params = {
...   'shopID': '1'
... }

>>> ny_store = api.shop(params)
>>> next(ny_store)
{
    "@attributes": {
        "count": "1",
        "offset": "0",
        "limit": "100"
    },
    "Shop": {
        "shopID": "1",
        "name": "New York Storefront",
    ...

Timestamps

Pass in all timestamps as datetime objects - this library will convert them to iso format for you. The Lightspeed API actually accepts timezone encoded timestamps, but will always return UTC.

Query Operators

The default operator for any query is '=', but you can specify others to make your searches more powerful. If your intention is the = operator, just put the value in the params dict like the Parameters section above. If you'd like to make use of other operators, put the operators in a list with the values you're operating on, like so:

>>> from datetime import datetime
>>> import pytz

>>> jan_first = pytz.timezone('America/Boise').localize(datetime(2021, 1, 1), is_dst=None)
>>> feb_first = pytz.timezone('America/Boise').localize(datetime(2021, 2, 1), is_dst=None)

>>> params = {
...     'checkIn': ['><', jan_first, feb_first]
... }

>>> all_january_shifts = api.employee_hours(params)
>>> next(all_january_shifts)
{
    "@attributes": {
        "count": "116",
        "offset": "0",
        "limit": "100"
    },
    "EmployeeHours": [
        {
            "employeeHoursID": "4453",
            "checkIn": "2021-01-02T16:31:30+00:00",
            "checkOut": "2021-01-02T23:53:41+00:00",
            "employeeID": "55",
            "shopID": "1"
        },
    ...

Relations

You're always welcome to load relations manually in your params. The Employee endpoint has an option load_contact that, when True, loads the Contact relation.

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

plaw-0.1.2.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

plaw-0.1.2-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file plaw-0.1.2.tar.gz.

File metadata

  • Download URL: plaw-0.1.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for plaw-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f2d3508d4973f383cb997c0c7903d3dcd551c39d050366a3dffc8747fef55dc7
MD5 7efc6f1833d257a8f744214c3bb7d4cf
BLAKE2b-256 0816a73ab2ca737dc9cb4c33990230809d579803120b2d2a83e210f9fda4c717

See more details on using hashes here.

File details

Details for the file plaw-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: plaw-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for plaw-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 14574c8ad9c55d21fc523fcd81e44caab604840f8d2d9a043aeea79633b7e94d
MD5 fa2052089321e1472eb14ab3baa78b2f
BLAKE2b-256 e97b52456889557f04b02e44bf250399fc2396a02679813c79070ad6b8c08c22

See more details on using hashes here.

Supported by

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