Engines

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

Bases: shopify_trois.engines.http.oauth_engine.OAuthEngine

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)
extension = 'json'
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)
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())
mime = 'application/json; charset=utf-8'
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.
Fork me on GitHub