API Documentation¶
This part of the documentation covers the interfaces used to develop with amazon-orders
.
Main Interface¶
- class amazonorders.orders.AmazonOrders(amazon_session, debug=None, output_dir=None)[source]¶
Bases:
object
Using an authenticated
AmazonSession
, can be used to query Amazon for Order details and history.-
amazon_session:
AmazonSession
¶ The AmazonSession to use for requests.
- output_dir¶
The directory where any output files will be produced, defaults to
conf.DEFAULT_OUTPUT_DIR
.
-
amazon_session:
Session Management¶
- class amazonorders.session.IODefault[source]¶
Bases:
object
Handles input/output from the application. By default, this uses console commands, but this class exists so that it can be overriden when constructing an
AmazonSession
if input/output should be handled another way.
- class amazonorders.session.AmazonSession(username, password, debug=False, max_auth_attempts=10, cookie_jar_path=None, io=<amazonorders.session.IODefault object>, output_dir=None)[source]¶
Bases:
object
An interface for interacting with Amazon and authenticating an underlying
requests.Session
. Utilizing this class means session data is maintained between requests. Session data is also persisted after each request, meaning it will also be maintained between separate instantiations of the class or application.To get started, call the
login
function.-
debug:
bool
¶ Set logger
DEBUG
, send output tostderr
, and write an HTML file for requests made on the session.
-
max_auth_attempts:
int
¶ Will continue in
login
’s auth flow this many times (successes and failures).
-
cookie_jar_path:
str
¶ The path to persist session cookies, defaults to
conf.DEFAULT_COOKIE_JAR_PATH
.
- output_dir¶
The directory where any output files will be produced, defaults to
conf.DEFAULT_OUTPUT_DIR
.
-
last_response_parsed:
Optional
[Tag
]¶ A parsed representation of the last response executed on the Session.
- request(method, url, **kwargs)[source]¶
Execute the request against Amazon with base headers, parsing and storing the response and persisting response cookies.
- Parameters:
method (
str
) – The request method to execute.url (
str
) – The URL to executemethod
on.kwargs (
Any
) – Remainingkwargs
will be passed torequests.request
.
- Return type:
- Returns:
The Response from the executed request.
- get(url, **kwargs)[source]¶
Perform a GET request.
- Parameters:
url (
str
) – The URL to GET on.kwargs (
Any
) – Remainingkwargs
will be passed toAmazonSession.request
.
- Returns:
The Response from the executed GET request.
- post(url, **kwargs)[source]¶
Perform a POST request.
- Parameters:
url – The URL to POST on.
kwargs (
Any
) – Remainingkwargs
will be passed toAmazonSession.request
.
- Return type:
- Returns:
The Response from the executed POST request.
- login()[source]¶
Execute an Amazon login process. This will include the sign-in page, and may also include Captcha challenges and OTP pages (of 2FA authentication is enabled on your account).
If successful,
is_authenticated
will be set toTrue
.Session cookies are persisted, and if existing session data is found during this auth flow, it will be skipped entirely and flagged as authenticated.
- Return type:
-
debug:
- class amazonorders.forms.AuthForm(selector, error_selector='div#auth-error-message-box', critical=False)[source]¶
Bases:
ABC
The base class of an authentication
<form>
that can be submitted.The base implementation will attempt to auto-solve Captcha. If this fails, it will use the default image view to show the Captcha prompt, and it will also pass the image URL to
prompt
asimg_url
.- selector: str¶
The CSS selector for the
<form>
.
- error_selector: str¶
The CSS selector for the error div when form submission fails.
- critical: bool¶
If
critical
, form submission failures will raiseAmazonOrdersAuthError
.
- amazon_session¶
The
AmazonSession
on which to submit the form.
- form: Optional[Tag]¶
The selected
<form>
.
- data: Optional[Dict[Any]]¶
The
<form>
data that will be submitted.
- select_form(amazon_session, parsed)[source]¶
Using the
selector
defined on this instance, select the<form>
for the givenTag
.- Parameters:
amazon_session – The
AmazonSession
on which to submit the form.parsed (
Tag
) – TheTag
from which to select the<form>
.
- Return type:
- Returns:
Whether the
<form>
selection was successful.
- class amazonorders.forms.SignInForm(selector="form[name='signIn']", solution_attr_key='email')[source]¶
Bases:
AuthForm
- class amazonorders.forms.MfaDeviceSelectForm(selector='form#auth-select-device-form', solution_attr_key='otpDeviceContext')[source]¶
Bases:
AuthForm
This will first echo the
<form>
device choices, then it will pass the list of choices toprompt
aschoices
. The value passed toprompt
will be alist
of thevalue
from each ofinput
tag.
- class amazonorders.forms.MfaForm(selector='form#auth-mfa-form', solution_attr_key='otpCode')[source]¶
Bases:
AuthForm
Entities¶
- class amazonorders.entity.parsable.Parsable(parsed)[source]¶
Bases:
object
A base class that contains a parsed representation of the entity, and can be extended to be made up of the entities fields utilizing the helper methods.
-
parsed:
Tag
¶ Parsed HTML data that can be used to populate the fields of the entity.
- safe_parse(parse_function, **kwargs)[source]¶
Execute the given parse function on a field, handling any common parse exceptions and passing them as warnings to the logger, suppressing them as exceptions.
- simple_parse(selector, link=False, return_type=None, text_contains=None, required=False)[source]¶
Will attempt to extract the text value of the given CSS selector(s) for a field, and is suitable for most basic functionality on a well-formed page.
The
selector
can be either astr
or alist
. If alist
is given, each selector in the list will be tried.- Parameters:
selector (
Union
[str
,list
]) – The CSS selector(s) for the field.link (
bool
) – If a link, the value ofsrc
orhref
will be returned.return_type (
Optional
[Type
]) – Specifyint
orfloat
to return a value other thanstr
.text_contains (
Optional
[str
]) – Only select the field if this value is found in its text content.required (
bool
) – If required, an exception will be thrown instead of returningNone
.
- Return type:
- Returns:
The cleaned up return value from the parsed
selector
.
- safe_simple_parse(selector, **kwargs)[source]¶
A helper function that uses
simple_parse
as theparse_function()
passed tosafe_parse
.- Parameters:
selector (
Union
[str
,list
]) – The selector to pass tosimple_parse
.kwargs – The
kwargs
will be passed toparse_function
.
- Return type:
- Returns:
The return value from
simple_parse
.
-
parsed:
- class amazonorders.entity.order.Order(parsed, full_details=False, clone=None)[source]¶
Bases:
Parsable
An Amazon Order.
-
payment_method_last_4:
Optional
[str
]¶ The Order payment method’s last 4 digits. Only populated when
full_details
isTrue
.
-
shipping_total:
Optional
[float
]¶ The Order shipping total. Only populated when
full_details
isTrue
.
-
subscription_discount:
Optional
[float
]¶ The Order Subscribe & Save discount. Only populated when
full_details
isTrue
.
-
total_before_tax:
Optional
[float
]¶ The Order total before tax. Only populated when
full_details
isTrue
.
-
payment_method_last_4:
- class amazonorders.entity.recipient.Recipient(parsed)[source]¶
Bases:
Parsable
The person receiving an Amazon
Order
.
Exceptions¶
- exception amazonorders.exception.AmazonOrdersError[source]¶
Bases:
Exception
Raised when a general
amazon-orders
error has occurred.
- exception amazonorders.exception.AmazonOrdersAuthError[source]¶
Bases:
AmazonOrdersError
Raised when an
amazon-orders
authentication error has occurred.
- exception amazonorders.exception.AmazonOrderEntityError[source]¶
Bases:
AmazonOrdersError
Raised when an
amazon-orders
entity parsing error has occurred.