-
Notifications
You must be signed in to change notification settings - Fork 4
magento.clients
Adam Korn edited this page Jun 16, 2022
·
6 revisions
Module containing the main Client
class (for interacting with the API) as well as AuthenticationError
(for handling authentication errors
This was literally taken from going like
help(client)
I wish I was joking but I am not. Read if you want (or don't)
class Client
__init__(self, domain, username, password, user_agent=None, token=None, login=True):
"""The class that handles all interaction with the API"""
:param domain: 'str',
:param username: 'str',
:param password: 'str',
:param user_agent: 'str' = None,
:param token: 'str' = None,
:param login 'bool' = True
NOTES:
-
domain
is in the form"domain.com"
- Use
login=False
to avoid authenticating credentials
authenticate(self) -> bool
"""Request access token from the authentication endpoint."""
request(self, url: str) -> requests.Response
"""Sends a request with the access token. Used for all internal requests"""
search(self, endpoint: str) -> magento.search.SearchQuery
"""Initializes and returns a SearchQuery object corresponding to the specified endpoint"""
url_for(self, endpoint: str) -> magento.search.SearchQuery
"""Returns the base API request url for the specified endpoint"""
validate(self)
"""Sends an authorized request to a standard API endpoint"""
@classmethod
new(cls) -> cls
"""Prompts for input to log in to the API. Returns a client after validating the token"""
@property
orders
"""Returns a magento.search.OrderSearch object"""
@property
products
"""Returns a magento.search.ProductSearch object"""
@property
categories
"""Returns a magento.search.CategorySearch object"""
@property
invoices
"""Returns a magento.search.InvoiceSearch object"""
@property
headers
"""Authorization headers for API requests. Validates/reauthenticates the token before returning"""
@property
token
"""Returns or generates access token"""
class AuthenticationError(Exception):
def __init__(self, client: Client, msg=None, response: requests.Response = None):
-
AuthenticationError
- exception class written specifically for token authentication errors- Should be flexible enough to handle most bad API responses though
- Accepts a
Client
andresponse
as parameters-
Client
is used solely for its logger -
response
is the response that led to the exception being raised.
-
- If a
response
is provided,parse()
adds to the exception/log message from the response body - Once a exception message is built, it is logged using the
Client
'sMagentoLogger
before being raised