The clients
module
- class magento.clients.Client(domain, username, password, scope='', user_agent=None, token=None, log_level='INFO', login=True, **kwargs)[source]View on GitHub
Bases:
object
The class that handles all interaction with the API
- __init__(domain, username, password, scope='', user_agent=None, token=None, log_level='INFO', login=True, **kwargs)[source]View on GitHub
Initialize a Client
Important!
The Magento account you use to log in must be assigned a User Role that has the appropriate API resources included 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 any other desired resources are included- Parameters
domain (str) – domain name of the Magento store (ex.
domain.com
)username (str) – username of the Magento Admin account
password (str) – password of the Magento Admin account
scope (Optional[str]) – the store view scope to
search()
and make requests onuser_agent (Optional[str]) – the user agent to use in requests
log_level (str) – the logging level for logging to stdout
login (bool) – if
True
, callsauthenticate()
upon initializationkwargs – see below
…
- logger: MagentoLogger
The
MagentoLogger
for the domain/username combination
- classmethod new()[source]View on GitHub
Prompts for input to log in to the Magento API
- Return type
- classmethod load(pickle_bytes)[source]View on GitHub
Initialize a
Client
using a pickle bytestring fromto_pickle()
- Return type
- classmethod from_json(json_str)[source]View on GitHub
Initialize a
Client
from a JSON string of settings- Return type
- classmethod from_dict(d)[source]View on GitHub
Initialize a
Client
from a dictionary of settings- Return type
- url_for(endpoint, scope=None)[source]View on GitHub
Returns the appropriate request url for the given API endpoint and store scope
Example
# Generate the url for credit memo with id 7 >> api=Client("domain.com", "user", "password") >> api.url_for('creditmemo/7') "https://domain.com/rest/V1/creditmemo/7" # Generate the same url on the "en" store view >> api.url_for('creditmemo/7', scope='en') "https://domain.com/rest/en/V1/creditmemo/7"
- Parameters
endpoint (str) – the API endpoint
scope (Optional[str]) – the scope to generate the url for; uses the
Client.scope
if not provided
- Return type
- search(endpoint)[source]View on GitHub
Initializes and returns a
SearchQuery
corresponding to the specified endpointNote
Several endpoints have predefined
SearchQuery
andModel
subclassesIf a subclass hasn’t been defined for the
endpoint
yet, a generalSearchQuery
will be returned, which wraps theresult
withAPIResponse
- Parameters
endpoint (str) – a valid Magento API search endpoint
- Return type
- property orders: OrderSearch
Initializes an
OrderSearch
- property order_items: OrderItemSearch
Initializes an
OrderItemSearch
- property invoices: InvoiceSearch
Initializes an
InvoiceSearch
- property categories: CategorySearch
Initializes a
CategorySearch
- property products: ProductSearch
Initializes a
ProductSearch
- property product_attributes: ProductAttributeSearch
Initializes a
ProductAttributeSearch
- get(url)[source]View on GitHub
Sends an authorized
GET
request
- post(url, payload)[source]View on GitHub
Sends an authorized
POST
request
- put(url, payload)[source]View on GitHub
Sends an authorized
PUT
request
- delete(url)[source]View on GitHub
Sends an authorized
DELETE
request
- authenticate()[source]View on GitHub
Authenticates the
USER_CREDENTIALS
and retrieves an access token- Return type
- validate()[source]View on GitHub
Validates the
token
by sending an authorized request to a standard API endpoint- Return type
- request(method, url, payload=None)[source]View on GitHub
Sends an authorized API request. Used for all internal requests
- get_logger(log_file=None, stdout_level='INFO', log_requests=True)[source]View on GitHub
Retrieve a MagentoLogger for the current username/domain combination. Log files are DEBUG.
- Parameters
- Return type
- property headers: dict
Authorization headers for API requests
Automatically generates a
token
if needed
- to_pickle(validate=False)[source]View on GitHub
Serializes the Client to a pickle bytestring
- Parameters
validate (bool) – if
True
, validates thetoken
/USER_CREDENTIALS
before serializing- Return type
- to_json(validate=False)[source]View on GitHub
Serializes the Client to a JSON string
- Parameters
validate (bool) – if
True
, validates thetoken
/USER_CREDENTIALS
before serializing- Return type
- to_dict(validate=False)[source]View on GitHub
Serializes the Client to a dictionary
- view_config()[source]View on GitHub
Prints the Client configuration settings
- class magento.clients.Store(client)[source]View on GitHub
Bases:
object
Class containing store configurations and cached attribute lists
- __init__(client)[source]View on GitHub
Initialize a Store object
- property is_single_store: bool
Whether the store has a single store view (
default
) or multiple store views
- property active: APIResponse
Returns the store config corresponding to the current
scope
of theClient
- property configs: Optional[Union[APIResponse, List[APIResponse]]]
Returns a list of all store configurations
- property views: Optional[Union[APIResponse, List[APIResponse]]]
Returns a list of all store views
- property all_product_attributes: List[ProductAttribute]
A cached list of all product attributes
- property store_view_product_attributes: List[ProductAttribute]
A cached list of all product attributes with the
Store View
scope
- property website_product_attributes: List[ProductAttribute]
A cached list of all product attributes with the
Web Site
scope
- property global_product_attributes: List[ProductAttribute]
A cached list of all product attributes with the
Global
scope
- property website_attribute_codes: List[str]
The attribute codes of the
website_product_attributes
- filter_website_attrs(attribute_data)[source]View on GitHub
Filters a product attribute dict and returns a new one that contains only the website scope attributes
Website scoped attributes must be updated on the admin by making a second request on the
all
scopeThis method is called by
update_attributes()
andupdate_custom_attributes()
to see if the second request is needed
Example
The
price
attribute isWebsite
scope and themeta_title
attribute isStore View
scope>> attribute_data = {'price': 12, 'meta_title': 'My Product'} >> store.filter_website_attrs(attribute_data) {'price': 12}
- refresh()[source]View on GitHub
Clears all cached properties
- Return type