Home

Awesome

basic_shopify_api

Tests Coverage PyPi version

This library extends HTTPX and implements a read-to-use sync/async client for REST and GraphQL API calls to Shopify's API.

Support for:

Table of Contents

Installation

$ pip install basic-shopify-api

Requires Python 3.

Options

Options().

There are a variety of options to take advantage of.

You can simply create a new instance to use all default values if you're using public API mode.

Options available:

Example:

opts = Options()
opts.version = "unstable"
opts.mode = "private"

Session

Create a session to use with a client. Depending on if you're accessing the API public or privately, then you will need to fill different values.

Session(domain, key, password, secret).

For public access, you will need to fill in:

For private access, you will need to fill in:

Example:

from basic_shopify_api import Session
session = Session(domain="john-doe.myshopify.com", key="abc", password="123")

REST Usage

rest(method, path[, params, headers]).

REST Sync

Example:

from basic_shopify_api import Client

with Client(sess, opts) as client:
    shop = client.rest("get", "/admin/api/shop.json", {"fields": "name,email"})
    print(shop.response)
    print(shop.body["name"])

    # returns the following:
    # RestResult(
    #   response=The HTTPX response object,
    #   body=A dict of JSON response, or None if errors,
    #   errors=A dict of error response (if possible), or None for no errors, or the exception error,
    #   status=The HTTP status code,
    #   link=A RestLink object of next/previous pagination info,
    #   retries=Number of retires for the request
    # )

REST Async

Example:

from basic_shopify_api import AsyncClient

# ...

async with AsyncClient(sess, opts) as client:
    shop = await client.rest("get", "/admin/api/shop.json", {"fields": "name,email"})
    print(shop.response)
    print(shop.body["name"])

    # returns the following:
    # RestResult(
    #   response=The HTTPX response object,
    #   body=A dict of JSON response, or None if errors,
    #   errors=A dict of error response (if possible), or None for no errors, or the exception error,
    #   status=The HTTP status code,
    #   link=A RestLink object of next/previous pagination info,
    #   retries=Number of retires for the request
    # )

GraphQL Usage

graphql(query[, variables]).

GraphQL Sync

Example:

from basic_shopify_api import Client

# ...

with Client(sess, opts) as client:
    shop = client.graphql("{ shop { name } }")
    print(shop.response)
    print(shop.body["data"]["shop"]["name"])

    # returns the following:
    # ApiResult(
    #   response=The HTTPX response object,
    #   body=A dict of JSON response, or None if errors,
    #   errors=A dict of error response (if possible), or None for no errors, or the exception error,
    #   status=The HTTP status code,
    #   retries=Number of retires for the request,
    # )

GraphQL Async

Example:

from basic_shopify_api import AsyncClient

# ...

async with AsyncClient(sess, opts) as client:
    shop = await client.graphql("{ shop { name } }")
    print(shop.response)
    print(shop.body["data"]["name"])

    # returns the following:
    # ApiResult(
    #   response=The HTTPX response object,
    #   body=A dict of JSON response, or None if errors,
    #   errors=A dict of error response (if possible), or None for no errors, or the exception error,
    #   status=The HTTP status code,
    #   link=A RestLink object of next/previous pagination info,
    #   retries=Number of retires for the request
    # )

Pre/Post Actions

To register a pre or post action for REST or GraphQL, simply append it to your options setup.

from basic_shopify_api import Options, Client

def say_hello(inst):
    """inst is the current client instance, either Client or AsyncClient"""
    print("hello")

def say_world(inst, result):
    """
    inst is the current client instance, either Client or AsyncClient.
    result is either RestResult or GraphQLResult object.
    """
    print("world")

opts = Options()
opts.rest_pre_actions = [say_hello]
opts.rest_post_ations = [say_world]

sess = Session(domain="john-doe.myshopify.com", key="abc", password="134")

with Client(sess, opts) as client:
    shop = client.rest("get", "/admin/api/shop.json")
    print(shop)
    # Output: "hello" "world" <ApiResult>

Utilities

This will be expanding, but as of now there are utilities to help verify HMAC for 0Auth/URL, proxy requests, and webhook data.

0Auth/URL

from basic_shopify_api.utils import hmac_verify

params = request.args # some method to get a dict of query params
verified = hmac_verify("standard", "secret key", params)
print(f"Verified? {verified}")

Proxy

from basic_shopify_api.utils import hmac_verify

params = request.args # some method to get a dict of query params
verified = hmac_verify("proxy", "secret key", params)
print(f"Verified? {verified}")

Webhook

from basic_shopify_api.utils import hmac_verify

hmac_header = request.headers.get("x-shopify-hmac-sha256") # some method to get the HMAC header
params = request.get_data(as_text=True) # some method to get a JSON str of request data
verified = hmac_verify("webhook", "secret key", params, hmac_header)
print(f"Verified? {verified}")

Development

python -m venv env && source env/bin/activate

python -m pip install -r requirements.txt

Testing

make test.

For coverage reports, use make cover or make cover-html.

Documentation

See this Github page or view docs/.

License

This project is released under the MIT license.

Misc

Using PHP? Check out Basic-Shopify-API.