Python Magento 2 REST API Wrapper
Project description
A Python package that wraps and extends the Magento 2 REST API
About MyMagento
📝 What’s MyMagento? |
---|
MyMagento is a highly interconnected package that wraps and extends the Magento 2 REST API, providing a more intuitive and user-friendly interface to access and update your store |
MyMagento simplifies interaction with the Magento 2 REST API
If you’ve worked with the Magento 2 API, you’ll know that not all endpoints are created equally.
MyMagento aims to streamline your workflow by simplifying a variety of commonly needed API operations.
Main Components
💻 The Client |
---|
|
🔍 The SearchQuery and Subclasses |
---|
|
🧠 The Model Subclasses |
---|
|
Available Endpoints
MyMagento is compatible with every API endpoint
Endpoints are wrapped with a Model and SearchQuery subclass as follows:
Endpoint |
Client Shortcut |
SearchQuery Subclass |
Model Subclass |
---|---|---|---|
orders |
Client.orders |
||
orders/items |
Client.order_items |
||
invoices |
Client.invoices |
||
products |
Client.products |
||
products/attributes |
Client.product_attributes |
||
categories |
Client.categories |
||
endpoint |
Client.search("endpoint") |
⚙ Installing MyMagento
To install using pip:
pip install my-magento
Please note that MyMagento requires Python >= 3.10
…
📚 Documentation
Full documentation can be found on ReadTheDocs
QuickStart: Login with MyMagento
MyMagento uses the Client class to handle all interactions with the API.
💡 Tip |
---|
See Get a Magento 2 REST API Token With MyMagento for full details on generating an access token |
Setting the Login Credentials
To generate an ACCESS_TOKEN you’ll need to authenticate() your USER_CREDENTIALS
Creating a Client requires a domain, username, and password at minimum.
>>> domain = 'website.com'
>>> username ='username'
>>> password = 'password'
If you’re using a local installation of Magento you’ll need to set local=True. Your domain should look like this:
>>> domain = '127.0.0.1/path/to/magento'
…
Getting a Client
Option 1: Initialize a Client Directly
from magento import Client
>>> api = Client(domain, username, password, **kwargs)
Option 2: Call get_api()
import magento
>>> api = magento.get_api(**kwargs)
get_api() takes the same keyword arguments as the Client
If the domain, username, or password are missing, it will attempt to use the following environment variables:
import os
os.environ['MAGENTO_DOMAIN'] = domain
os.environ['MAGENTO_USERNAME']= username
os.environ['MAGENTO_PASSWORD']= password
…
Getting an ACCESS_TOKEN
Unless you specify login=False, the Client will automatically call authenticate() once initialized
>>> api.authenticate()
|[ MyMagento | website_username ]|: Authenticating username on website.com...
|[ MyMagento | website_username ]|: Logged in to username
Performing a search()
The search() method lets you execute() a query on any API endpoint
It creates a SearchQuery for the endpoint, allowing you to retrieve data for
An individual item (ex. by_id())
A list of items (ex. by_list())
Any search criteria you desire (see Building Custom Search Queries)
📚 From the Docs… |
||
---|---|---|
|
Example: search() an endpoint by_id()
# Query the "invoices" endpoint (also: api.invoices)
>>> api.search("invoices").by_id(1)
<Magento Invoice: "#000000001"> for <Magento Order: "#000000001" placed on 2022-11-01 03:27:33>
Example: search() an endpoint by_list()
# Retrieve invoices from a list of invoice ids
>>> ids = list(range(1,101))
>>> api.invoices.by_list("entity_id", ids)
[<Magento Invoice: "#000000001"> for <Magento Order: "#000000001" placed on 2022-11-01 03:27:33>, ...]
Search Results: The Model Classes
The result of any SearchQuery will be parsed and wrapped by a Model class in the magento.models subpackage.
These classes make the API response data easier to work with.
They also provide endpoint-specific methods to update store data and search for related items.
Example: Retrieving every Order containing a Product
Let’s retrieve a Product using by_sku()
>>> product = api.products.by_sku("24-MB01")
We can search for orders containing this product as follows:
# Using the Product itself
>>> product.get_orders()
[<Magento Order: "#000000003" placed on 2022-12-21 08:09:33>, ... ]
# Using an OrderSearch
>>> api.orders.by_product(product)
>>> api.orders.by_product_id(product.id)
>>> api.orders.by_sku(product.sku)
[<Magento Order: "#000000003" placed on 2022-12-21 08:09:33>, ... ]
Example: Updating the Thumbnail MediaEntry of a Product
# Update product thumbnail label on specific store view
>>> product.thumbnail.set_alt_text("bonjour", scope="FR")
>>> print(product.thumbnail)
<MediaEntry 3417 for <Magento Product: 24-MB01>: bonjour>
💡 Tip Set the Store Scope |
---|
If you have multiple store views, a store_code can be specified when retrieving/updating data
|
Building Custom Search Queries
In addition to the predefined methods, you can also build your own queries
Simply add_criteria(), restrict_fields(), and execute() the search
The since() and until() methods allow you to further filter your query by date
📄 Example: Retrieve Orders Over $50 Placed Since the Start of 2023 |
---|
|
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for my_magento-2.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79fec6cd03524eb327ac69d01b84d7751b8ff237728d49af2ba47b7329cbdfe6 |
|
MD5 | c2c0da618acd87efcaa471e3e4666aa2 |
|
BLAKE2b-256 | af32d171d0c0f8ea649b40d0dd323518d65ec860208ef9d07d44e06c2ae8840c |