API

App Authorization

from shopify_trois import Credentials, Collection
from shopify_trois.models import *
from shopify_trois.engines.http import Json as Shopify

credentials = Credentials(
    api_key='your api key',
    scope=['read_content', 'write_orders'],
    secret='your api key secret'
)
shopify = Shopify(shop_name="your store name", credentials=credentials)

# Redirect the shop owner to the URL provided by `shopify.oauth_authorize_url()`
# print(shopify.oauth_authorize_url())
# Get the oauth temporary code and set it in the credentials object.

credentials.code = "oauth temporary code"

# fetch the access token
shopify.setup_access_token()

# credentials.oauth_access_token will contain the access token. It would be a good idea to save it somewhere.

Client Setup Once Authorized

credentials = Credentials(
    api_key='your api key',
    scope=['read_content'],
    secret='your app secret',
    oauth_access_token="your access token"
)

shopify = Shopify(shop_name="your store name", credentials=credentials)

# The client is now ready to communicate with Shopify
shopify.fetch(Shop).to_dict()

Working With Data

# Fetch the store information
shop = shopify.fetch(Shop)

# Set the shop as public
shop.public = True

# Calling changes() on a model instance will show the modified properties.
print(shop.changes())

webhook = Webhook()
webhook.address = "http://do-not-copy-this.com"
webhook.format = "json"
webhook.topic = "orders/create"

# Create the webhook
shopify.add(webhook)

# Get all the webhooks and iterates them
webhooks = shopify.index(Webhook)
for webhook in webhooks:
    # webhook is a Webhook instance, created by iterating the Collection
    print(webhook.to_dict())

Relationships

Shopify-Trois has basic entity relationship support. Some entities have been marked as subresources of another.

shopify.index(ProductVariant, parent_id=2)

variant = ProductVariant(id=123, product_id=2)
shopify.fetch(variant)

Metafields

Shopify-Trois exposes metafields as it do for relationships.

Note: The ProductVariant metafield is currently unsupported.

shopify.index(BlogMetafield, parent_id=234224)

metafield = BlogMetafield(id=2342, blog_id=3242424)
shopify.fetch(metafield)

Accessing Multiple Shops

Shopify-Trois can easily access multiple shops within the same thread.

from shopify_trois import Credentials
from shopify_trois.models import Shop
from shopify_trois.engines.http import Json as Shopify

credentials = {
    "a-store-name": Credentials(
        api_key='your-app-key',
        secret='your-app-secret',
        oauth_access_token='access-token',
        scope=['read_content']
    ),
    "another-store-name": Credentials(
        api_key='your-app-key',
        secret='your-app-secret',
        oauth_access_token='access-token',
        scope=['read_content']
    )
}

instances = [Shopify(shop_name=k, credentials=v) for k, v in credentials.items()]

stores = [instance.fetch(Shop) for instance in instances]

for store in stores:
    print(store.name)

Classes

class shopify_trois.engines.http.json.Json(shop_name, credentials, ignore_supported=False, ignore_model_properties=False)

The Json engine implements an HTTP transport using JSON with OAuth authentication.

Parameters:
  • shop_name – The name of the shopify store.
  • credentialsCredential
  • ignore_supported – When set to True, the engine will ignore the supported property of the models.
  • ignore_model_properties – When set to True, the engine will ignore the properties property of models when persisting them.
add(instance, auto_update=True)

Add the model instance to the store.

Parameters:
  • instance – The model instance being added.
  • auto_update – Auto-update the instance with the values from Shopify. When set to false, the raw JSON object will be returned.

example usage:

from shopify_trois.engines.http import Json as Shopify
from shopify_trois import Credentials
from shopify_trois.models import Blog

credentials = Credentials(...)
shopify = Shopify(store_name="test", credentials=credentials)

blog = Blog(title="Awesome")
shopify.add(blog)
authorize_app_url()

Generates the oauth authorization url.

count(model, **params)

Return the count of a model, filtered by parameters

example usage:

from shopify_trois.engines.http import Json as Shopify
from shopify_trois import Credentials
from shopify_trois.models import Blog

credentials = Credentials(...)
shopify = Shopify(store_name="test", credentials=credentials)

blogs = shopify.count(Blog)
custom_post(instance, action, data=None)

Executes a custom post method on an instance.

Parameters:
  • instance – The model instance being modified.
  • action – The action to be executed.
delete(instance, **params)

Delete a model instance.

An InvalidRequestException will be raised if the instance does not yet exists.

Parameters:
  • instance – The model instance to remove.
  • params – Query parameters.

example usage:

from shopify_trois.engines.http import Json as Shopify
from shopify_trois import Credentials
from shopify_trois.models import Blog

credentials = Credentials(...)
shopify = Shopify(store_name="test", credentials=credentials)

blog = Blog(id=3)
shopify.delete(blog)
fetch(model, primary_key=None, auto_instance=True, **params)

Get a specific model instance by primary key.

Parameters:
  • Model – The class being queried.
  • primary_key – The primary key value of the instance.
  • auto_instance – Automatically create an instance instead of returning a json object.
  • params – Query parameters.

example usage:

from shopify_trois.engines.http import Json as Shopify
from shopify_trois import Credentials
from shopify_trois.models import Blog

credentials = Credentials(...)
shopify = Shopify(store_name="test", credentials=credentials)

blog = shopify.fetch(Blog, 2)
ignore_supported = None

When set to True, ignore checking for supported actions on models.

index(model, auto_instance=True, **params)

Fetch the index for a given model and supplied parameters.

Parameters:
  • model – The model being queried.
  • auto_instance – Automatically create a Collection instance instead of returning a json object.
  • params – Query parameters (see shopify documentation)

example usage:

from shopify_trois.engines.http import Json as Shopify
from shopify_trois import Credentials
from shopify_trois.models import Blog

credentials = Credentials(...)
shopify = Shopify(store_name="test", credentials=credentials)

blogs = shopify.index(Blog)
for blog in blogs:
    print(blog.to_dict())
oauth_access_token()

Fetch the OAuth access token from shopify.

setup_access_token() should be used.

oauth_access_token_url()

Generate the OAuth access token url.

oauth_authorize_url(redirect_to=None)

Generates the oauth authorize url.

Parameters:redirect_to – URL shopify will redirect to once authorized.
setup_access_token()

Utility function wrapping getting the oauth token and setting the credential values.

sync_access_token()

Utility method to sync the credentials access token with the HTTP session.

update(instance, auto_update=True)

Update a model instance.

An InvalidRequestException will be raised if the instance has not been marked as existing.

Parameters:
  • instance – The model instance being updated.
  • auto_update – Auto-update the instance with the values from Shopify. When set to False, the raw JSON object will be returned.

example usage:

from shopify_trois.engines.http import Json as Shopify
from shopify_trois import Credentials
from shopify_trois.models import Blog

credentials = Credentials(...)
shopify = Shopify(store_name="test", credentials=credentials)

blog = shopify.fetch(Blog, 4)
blog.commentable = False
shopify.update(blog)
url_for_request(req)

Generates the url for the provided request.

Parameters:req – See Request
verify_signature(query_parameters)

Verify the signature provided with the query parameters.

http://docs.shopify.com/api/tutorials/oauth

example usage:

from shopify_trois import Credentials
from shopify_trois.engines import Json as Shopify
from urllib.parse import parse_qsl

credentials = Credentials(
    api_key='your_api_key',
    scope=['read_orders'],
    secret='your_app_secret'
)

shopify = Shopify(shop_name="your_store_name", credentials=                    credentials)

query_parameters = parse_qsl("code=238420989938cb70a609f6ece2e2586b&shop=yourstore.myshopify.com&timestamp=1373382939&signature=6fb122e33c21851c465345b8cb97245e")

if not shopify.verify_signature(query_parameters):
    raise Exception("invalid signature")

credentials.code = dict(query_parameters).get('code')

shopify.setup_access_token()
Returns:Returns True if the signature is valid.
class shopify_trois.credentials.Credentials(api_key='', secret='', scope='', code='', oauth_access_token=None)
class shopify_trois.collection.Collection(model, data)

Indices and tables

Fork me on GitHub