Skip to main content

Python Magento 2 REST API Client and Wrapper

Project description

MyMagento (my-magento)

A Python package to help simplify interaction with the Magento 2 REST API.


Why MyMagento?

After you authenticate() a Client, you'll never have to worry about formatting your Magento 2 REST API calls again - simply build your store's custom url_for() the API endpoint of your choice, then call request()

If an endpoint supports the searchCriteria interface, you can use a SearchQuery object to perform a search() — just add_criteria() , restrict_fields() , and execute() your query. It will parse() the response data, and, when possible, return the result wrapped in one of the Model or Entity classes


Installation

To install this package with pip:

pip install my-magento

Documentation

Please see the wiki for slightly more information


Sample Usage

As mentioned above, you can use a Client to login and make requests to any Magento 2 REST API endpoint

from magento import Client

api = Client('website.com','username', 'password')
endpoint = api.url_for("products/links/types", scope='')

response = api.request(endpoint)
print(url, response.json(), sep='\n')

Output:

2022-06-14 00:55:42 INFO   |[ MyMagento | website_username ]|:  Authenticating username on website.com...
2022-06-14 00:55:43 INFO   |[ MyMagento | website_username ]|:  Logged in to username

https://www.website.com/rest/V1/products/links/types
[{'code': 1, 'name': 'related'}, {'code': 5, 'name': 'crosssell'}, {'code': 4, 'name': 'upsell'}, {'code': 3, 'name': 'associated'}]

  • TIP: For detailed response data, search an endpoint by_id()

Getting Started

Configure the Magento Account

The Magento account you use must be assigned a User Role that has the appropriate API resources include in its Resource Access settings

This can be verified in Magento Admin by going to

System -> Permissions -> User Roles -> {Role} -> Role Resources -> Resource Access

and ensuring that Sales, Catalog, Customers, and all other desired resources are included


Initialize and Authenticate a Client

Authentication is very straightforward. For full details please see the client setup tutorial in the wiki

from magento import Client

api = Client('website.com','username', 'password', login=False)
api.authenticate()

You can also use Client.new() to login with input prompts

api = Client.new()
print('\nAccess Token: ', api.ACCESS_TOKEN)

Output:

Domain: >? website.com
Username: >? username
Password: >? password
User Agent: >? 
2022-06-14 00:55:42 INFO   |[ MyMagento | website_username ]|:  Authenticating username on website.com...
2022-06-14 00:55:43 INFO   |[ MyMagento | website_username ]|:  Logged in to username

Access Token:  eyJraWQiIxIiwiYWxnIjoiSFMyNTYifQ.eyJ1aWQiOjI3LCJ1dHlwaWQiOjIsImlhdCI6MTY1NTE4MjU0MywiZXhwIjoxNjU1MTg2MTQzfQ.AbtkboAG_5R6CTsHmZwu_DiINJ7BKQ0_5sqHGJqcJVk

⚠ 🔥 🥵 DISCLAIMER 😩 🩸 ⚠ ❗

This package (and README) is fr a work in progress, so raw data will be returned if no wrapper class exists for a given endpoint yet. It will always validate_result first though so you should be okay. I am just disclaiming the disclaim here.

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

my-magento-2.0.0b0.tar.gz (20.1 kB view hashes)

Uploaded Source

Built Distributions

my_magento-2.0.0b0-py3.10.egg (44.4 kB view hashes)

Uploaded Source

my_magento-2.0.0b0-py3-none-any.whl (20.1 kB view hashes)

Uploaded Python 3

Supported by

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