From 3d65e56be54200c82a5b4ab2a4afbcdafbcb33eb Mon Sep 17 00:00:00 2001 From: Alicia Florez Date: Fri, 25 Nov 2022 09:49:09 +0100 Subject: [PATCH] Add importitemshippingstatus call --- AUTHORS.rst | 1 + CHANGES.rst | 7 +++ shiba/salesmanagement.py | 47 +++++++++++++++++++ shiba/shibaconnection.py | 6 +++ .../sample_importitemshippingstatus.xml | 12 +++++ tests/offline/test_salesmanagement.py | 31 +++++++++++- 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/offline/Assets/sample_importitemshippingstatus.xml diff --git a/AUTHORS.rst b/AUTHORS.rst index 64aa1d8..8e24847 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -15,3 +15,4 @@ Contributors * Quentin Vaudaine * Boguta Maxime * Nicolas Baccelli +* Alicia Florez diff --git a/CHANGES.rst b/CHANGES.rst index bda4b2e..fb09f0a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,13 @@ Shiba : Python API for PriceMinister WebServices ================================================ +Version 1.2.1 +------------------- + +Released on November 24th 2022 + +- Add importitemshippingstatus call + Version 1.2.0 ------------------- diff --git a/shiba/salesmanagement.py b/shiba/salesmanagement.py index 813dfe4..0ef4cc6 100644 --- a/shiba/salesmanagement.py +++ b/shiba/salesmanagement.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import xml.etree.ElementTree as ET from datetime import date from .shibaconnection import ShibaConnection @@ -147,6 +148,52 @@ def contact_user_about_item(self, itemid, content): obj = retrieve_obj_from_url(url) return obj + def import_item_shipping_status(self, items_list): + """Send tracking information for multiple orders, such as the transporter's name "transporter_name", + tracking number "tracking_number" and the optional tracking url "tracking_url". + This WebService email the "itemid" customer, including a link for package tracking. + + Check data to use: + https://global.fr.shopping.rakuten.com/developpeur/confirmer-lenvoi-des-commandes-importitemshippingstatus/ + + Example for items_list: [ + { # "suivi" ou "recommandé" shipment + "purchaseid": 429492423, + "itemid": 670873525, + "transporter": "Autre", + "trackingnumber": "LF046851565FR", + "trackingurl": "https://a1.asendiausa.com/tracking/?trackingnumber=LF046851565FR", + }, + { # "normal" shipment + "purchaseid": 432832715, + "itemid": 673690604, + }, + ] + + :param items_list: list of dict with data for each item. All the dicts need purchaseid and + itemid keys. transporter, trackingnumber and trackingurl are needed for tracked shipments. + """ + # Create the XML file to send + items = ET.Element("items") + for i in items_list: + # Check purchaseid and itemid + if not all(key in i for key in ["purchaseid", "itemid"]): + raise ShibaCallingError( + "Shiba code error: purchaseid and itemid are mandatory keys for each dict of items_list." + ) + item = ET.SubElement(items, "item") + ET.SubElement(item, "purchaseid").text = str(i["purchaseid"]) + ET.SubElement(item, "itemid").text = str(i["itemid"]) + ET.SubElement(item, "transporter").text = i.get("transporter", "") + ET.SubElement(item, "trackingnumber").text = i.get("trackingnumber", "Oui") + ET.SubElement(item, "trackingurl").text = i.get("trackingurl", "") + file = ET.tostring(items) + + inf = inf_constructor(self.connection, "importitemshippingstatus") + url = url_constructor(self.connection, inf) + obj = retrieve_obj_from_url(url, file) + return obj + def set_tracking_package_infos( self, itemid, transporter_name, tracking_number, tracking_url="" ): diff --git a/shiba/shibaconnection.py b/shiba/shibaconnection.py index b265e13..1a05b51 100644 --- a/shiba/shibaconnection.py +++ b/shiba/shibaconnection.py @@ -156,4 +156,10 @@ def __init__(self, login, pwd, sandbox=False): "login": self.login, "pwd": self.pwd, }, + "importitemshippingstatus": { + "cat": "sales_ws", + "version": "2016-05-09", + "login": self.login, + "pwd": self.pwd, + }, } diff --git a/tests/offline/Assets/sample_importitemshippingstatus.xml b/tests/offline/Assets/sample_importitemshippingstatus.xml new file mode 100644 index 0000000..abe17ef --- /dev/null +++ b/tests/offline/Assets/sample_importitemshippingstatus.xml @@ -0,0 +1,12 @@ + + + + + + 2016-05-09> + + + 2021-06-29 + OK + + \ No newline at end of file diff --git a/tests/offline/test_salesmanagement.py b/tests/offline/test_salesmanagement.py index 1eac575..53ef342 100644 --- a/tests/offline/test_salesmanagement.py +++ b/tests/offline/test_salesmanagement.py @@ -43,7 +43,6 @@ def test_accept_sale(monkeypatch): assert obj is not None - def test_refuse_sale(monkeypatch): """Only fail result, as refusing an actual sale is not simulable""" monkeypatch.setattr( @@ -183,6 +182,36 @@ def test_set_tracking_package_infos(monkeypatch): assert obj is None +def test_import_item_shipping_status(monkeypatch): + """import_item_shipping_status on multiple products. Testing internal error catching as well.""" + monkeypatch.setattr( + "requests.post", make_requests_get_mock("sample_importitemshippingstatus.xml") + ) + sales_management = SalesManagement( + ShibaConnection("test", "test", "https://ws.fr.shopping.rakuten.com") + ) + items_list = [ + { + "purchaseid": 123456, + "itemid": 123456789, + }, + ] + obj = sales_management.import_item_shipping_status(items_list=items_list) + assert obj.content.tag == "importresult" + + obj = None + try: + items_list = [ + { + "purchaseid": 123456, + }, + ] + obj = sales_management.import_item_shipping_status(items_list=items_list) + except ShibaCallingError: + pass + assert obj is None + + def test_confirm_preorder(monkeypatch): """confirm_preorder on an advert. Testing internal error catching as well.""" monkeypatch.setattr(