Distinguish fetch and download, see #5

This commit is contained in:
Lucas Verney 2016-10-07 12:29:12 -04:00
parent e35a2fe083
commit ee677ab6d2
5 changed files with 107 additions and 18 deletions

View File

@ -101,6 +101,15 @@ map should have at the following three keys:
required by the associated Weboob backend.
* `id` should be a unique string of your choice, to uniquely identify this run
of the specified module with the specified set of parameters.
* `actions` is an optional list of actions to perform. It should contains two
keys, `fetch` and `download`. For each key, you can either pass `true` to
completely handle the actions, or a map of capabilities associated to list
of contents to fetch.
Typically, you can pass `"fetch": { "CapDocument": ["bills"]}` to fetch only
bills from the `CapDocuments` capability. You can also pass
`"download": { "CapDocument": ["someID"] }` to download a specific id (which
can be either type of fields in the `CapDocument` capability).
If not provided, the default is to fetch only, and do not download anything.
## Output JSON file

View File

@ -1,4 +1,5 @@
"""
Main script for this module
"""
from __future__ import print_function
@ -63,7 +64,11 @@ def main_fetch(used_modules):
# Fetch data and merge them with the ones from other
# capabilities
fetched_data[module["id"]].update(
fetching_function(backend)
fetching_function(
backend,
# If no actions specified, fetch but don't download
module.get("actions", {"fetch": True, "download": False})
)
)
except AttributeError:
# In case the converter does not exist on our side

View File

@ -6,26 +6,34 @@ from base import clean_object
from weboob.capabilities.bill import Bill
def to_cozy(document):
def fetch_subscriptions(document):
"""
Export a CapDocument object to a JSON-serializable dict, to pass it to Cozy
instance.
Fetch the list of subscriptions
Args:
document: The CapDocument object to handle.
Returns: A JSON-serializable dict for the input object.
Returns: A list of subscriptions
"""
# Get the BASEURL to generate absolute URLs
base_url = document.browser.BASEURL
# Fetch the list of subscriptions
try:
subscriptions = list(document.iter_subscription())
except NotImplementedError:
subscriptions = None
return subscriptions
# Fetch and clean the list of bills
# Bills are formatted final documents emitted by the third party (typically
# monthly bills for a phone service provider)
def fetch_documents(document, subscriptions):
"""
Fetch and clean the list of bills
Bills are formatted final documents emitted by the third party (typically
monthly bills for a phone service provider)
Documents are more general and can be contracts, terms etc.
Args:
document: The CapDocument object to handle.
subscriptions: A list of subscriptions for the CapDocument object.
Returns: A tuple of cleaned list of documents and bills.
"""
try:
assert subscriptions
raw_documents = {
@ -53,11 +61,22 @@ def to_cozy(document):
except (NotImplementedError, AssertionError):
documents = None
bills = None
return documents, bills
# Fetch and clean the list of details of the subscription (detailed
# consumption)
# Details are aggregated billing counts (typically aggregated counts by
# communication type for a phone service provider)
def fetch_details(document, subscriptions):
"""
Fetch and clean the list of details of the subscription (detailed
consumption)
Details are aggregated billing counts (typically aggregated counts by
communication type for a phone service provider)
Args:
document: The CapDocument object to handle.
subscriptions: A list of subscriptions for the CapDocument object.
Returns: A cleaned list of detailed bills.
"""
try:
assert subscriptions
detailed_bills = {
@ -69,10 +88,21 @@ def to_cozy(document):
}
except (NotImplementedError, AssertionError):
detailed_bills = None
return detailed_bills
# Fetch and clean the list of history bills
# History bills are detailed bills for any event that resulted in a bill
# (typically any communication for a phone service provider)
def fetch_history(document, subscriptions):
"""
Fetch and clean the list of history bills
History bills are detailed bills for any event that resulted in a bill
(typically any communication for a phone service provider)
Args:
document: The CapDocument object to handle.
subscriptions: A list of subscriptions for the CapDocument object.
Returns: A cleaned list of history bills.
"""
try:
assert subscriptions
history_bills = {
@ -85,6 +115,40 @@ def to_cozy(document):
}
except (NotImplementedError, AssertionError):
history_bills = None
return history_bills
def to_cozy(document, actions={"fetch": True, "download": False}):
"""
Export a CapDocument object to a JSON-serializable dict, to pass it to Cozy
instance.
Args:
document: The CapDocument object to handle.
actions: A dict describing what should be fetched (see README.md).
Returns: A JSON-serializable dict for the input object.
"""
# Get the BASEURL to generate absolute URLs
base_url = document.browser.BASEURL
if actions["fetch"] is True or "CapDocument" in actions["fetch"]:
subscriptions = fetch_subscriptions(document)
if actions["fetch"] is True or "documents" in actions["fetch"]["CapDocument"]:
documents, bills = fetch_documents(document, subscriptions)
else:
documents, bills = None, None
if actions["fetch"] is True or "detailed_bills" in actions["fetch"]["CapDocument"]:
detailed_bills = fetch_details(document, subscriptions)
else:
detailed_bills = None
if actions["fetch"] is True or "history_bills" in actions["fetch"]["CapDocument"]:
history_bills = fetch_history(document, subscriptions)
else:
history_bills = None
# Return a formatted dict with all the infos
return {

View File

@ -7,6 +7,7 @@ This capability is used for modules that have billing support.
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| subscriptions | List of subscriptions (contracts) | Subscription |
| bills | Map of bills for each subscription. Bills are final document produced by the third party. | Bill |
| documents | Map of documents for each subscription. Documents are final document produced by the third party, but not bills (typically contract, terms, etc). | Document |
| history_bills | Map of history bills for each subscription. History bills are detailed counts for any event resulting in a transaction (typically any communication for a phone service provider) | Detail |
| detailed_bills | Map of detailed bills for each subscription. Detailed bills are aggregated counts by facturation type (typically voice and texts for a phone service provider) | Detail |

View File

@ -26,6 +26,16 @@ def fetch_view():
return pretty_json(cozyweboob(params))
@post("/download")
def download_view():
"""
Download from weboob modules.
"""
params = request.forms.get("params")
response.content_type = "application/json"
# TODO return pretty_json(proxy.download(params))
@route("/list")
def list_view():
"""